summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapContext.java
blob: e5e2ebcb5d274c7854ccd7fbdf09de9b24b332d7 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.evaluation;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A context backed by a Map
 *
 * @author bratseth
 */
public class MapContext extends Context {

    private Map<String,Value> bindings=new HashMap<>();

    private boolean frozen = false;

    public MapContext() {
    }

    /**
     * Freezes this.
     * Returns this for convenience.
     */
    public MapContext freeze() {
        if ( ! frozen)
            bindings = Collections.unmodifiableMap(bindings);
        return this;
    }

    /**
     * Creates a map context from a map.
     * The ownership of the map is transferred to this - it cannot be further modified by the caller.
     * All the Values of the map will be frozen.
     */
    public MapContext(Map<String,Value> bindings) {
        this.bindings = bindings;
        for (Value boundValue : bindings.values())
            boundValue.freeze();
    }

    /**
     * Returns the value of a key. 0 is returned if the given key is not bound in this.
     */
    @Override
    public Value get(String key) {
        Value value=bindings.get(key);
        if (value==null) return DoubleValue.zero;
        return value;
    }

    /**
     * Sets the value of a key.
     * The value is frozen by this.
     *
     * @since 5.1.5
     */
    @Override
    public void put(String key,Value value) {
        bindings.put(key,value.freeze());
    }

    /** Returns an immutable view of the bindings of this. */
    public Map<String,Value> bindings() {
        if (frozen) return bindings;
        return Collections.unmodifiableMap(bindings);
    }
    
    /** Returns a new, modifiable context containing all the bindings of this */
    public MapContext thawedCopy() { return new MapContext(new HashMap<>(bindings)); }

    /** Returns an unmodifiable map of the names of this */
    @Override
    public Set<String> names() {
        if (frozen) return bindings.keySet();
        return Collections.unmodifiableMap(bindings).keySet();
    }

    @Override
    public String toString() {
        return "a map context [" + bindings.size() + " bindings]";
    }

    /**
     * A convenience constructor which returns a map context from a string on the form
     * <code>name1:value1, name2:value2 ...</code>.
     * Extra spaces are allowed anywhere. Any other deviation from the syntax causes an exception to be thrown.
     */
    public static MapContext fromString(String contextString) {
        MapContext mapContext = new MapContext();
        for (String keyValueString : contextString.split(",")) {
            String[] strings = keyValueString.trim().split(":");
            mapContext.put(strings[0].trim(), Value.parse(strings[1].trim()));
        }
        return mapContext;
    }

}