aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/pagetemplates/engine/Resolution.java
blob: 739ea32f2d1eaee3f044de57ed69e8ef0d290ea2 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.pagetemplates.engine;

import com.yahoo.search.pagetemplates.model.Choice;
import com.yahoo.search.pagetemplates.model.MapChoice;
import com.yahoo.search.pagetemplates.model.PageElement;

import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * A resolution of choices within a template.
 *
 * @author bratseth
 */
public class Resolution {

    /** A record of choices made as choice → alternative index (id) */
    private Map<Choice,Integer> choiceResolutions=new IdentityHashMap<>();

    /** A of map choices made as choice → mapping */
    private Map<MapChoice,Map<String,List<PageElement>>> mapChoiceResolutions=
            new IdentityHashMap<>();

    public void addChoiceResolution(Choice choice,int alternativeIndex) {
        choiceResolutions.put(choice,alternativeIndex);
    }

    public void addMapChoiceResolution(MapChoice choice, Map<String,List<PageElement>> mapping) {
        mapChoiceResolutions.put(choice,mapping);
    }

    /**
     * Returns the resolution of a choice.
     *
     * @return the (0-base) index of the choice made. If the given choice has exactly one alternative,
     *         0 is always returned (whether or not the choice has been attempted resolved).
     * @throws IllegalArgumentException if the choice is empty, or if it has multiple alternatives but have not
     *         been resolved in this
     */
    public int getResolution(Choice choice) {
        if (choice.alternatives().size() == 1) return 0;
        if (choice.isEmpty()) throw new IllegalArgumentException("Cannot return a resolution of empty " + choice);
        Integer resolution = choiceResolutions.get(choice);
        if (resolution == null) throw new IllegalArgumentException(this + " has no resolution of " + choice);
        return resolution;
    }

    /**
     * Returns the resolution of a map choice.
     *
     * @return the chosen mapping - entries from placeholder id to the values to use at the location of that placeholder
     * @throws IllegalArgumentException if this choice has not been resolved in this
     */
    public Map<String,List<PageElement>> getResolution(MapChoice choice) {
        Map<String,List<PageElement>> resolution=mapChoiceResolutions.get(choice);
        if (resolution==null) throw new IllegalArgumentException(this + " has no resolution of " + choice);
        return resolution;
    }

    @Override
    public String toString() {
        return "a resolution of " + choiceResolutions.size() + " choices";
    }

}