aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/pagetemplates/PlaceholderMappingVisitor.java
blob: de19e8ab7471ac547b8d6bb6ddeb5fc9b15a831f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.pagetemplates;

import com.yahoo.search.pagetemplates.model.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Creates a map from placeholder id to the choice providing its value
 * for all placeholder values visited.
 * <p>
 * This visitor will throw an IllegalArgumentException if the same placeholder id
 * is referenced by two choices.
 *
 * @author bratseth
 */
class PlaceholderMappingVisitor extends PageTemplateVisitor {

    private Map<String, MapChoice> placeholderIdToChoice=new LinkedHashMap<>();

    public @Override void visit(MapChoice mapChoice) {
        List<String> placeholderIds=mapChoice.placeholderIds();
        for (String placeholderId : placeholderIds) {
            MapChoice existingChoice=placeholderIdToChoice.put(placeholderId,mapChoice);
            if (existingChoice!=null)
                throw new IllegalArgumentException("placeholder id '" + placeholderId + "' is referenced by both " +
                        mapChoice + " and " + existingChoice + ": Only one reference is allowed");
        }
    }

    public Map<String, MapChoice> getMap() { return placeholderIdToChoice; }

}