aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionReferenceContext.java
blob: 96f0fa033d6f6336ab7f1fcca2888fa89f3555c2 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.rule;

import com.yahoo.searchlib.rankingexpression.ExpressionFunction;

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

/**
 * The context of a function invocation.
 *
 * @author bratseth
 */
public class FunctionReferenceContext {

    /** Expression functions indexed by name */
    private final Map<String, ExpressionFunction> functions;

    /** Mapping from argument names to the expressions they resolve to */
    private final Map<String, String> bindings = new HashMap<>();

    /** Create a context for a single serialization task */
    public FunctionReferenceContext() {
        this(Collections.emptyList());
    }

    /** Create a context for a single serialization task */
    public FunctionReferenceContext(Collection<ExpressionFunction> functions) {
        this(toMap(functions), Collections.emptyMap());
    }

    public FunctionReferenceContext(Collection<ExpressionFunction> functions, Map<String, String> bindings) {
        this(toMap(functions), bindings);
    }

    /** Create a context for a single serialization task */
    public FunctionReferenceContext(Map<String, ExpressionFunction> functions) {
        this(functions, null);
    }

    /** Create a context for a single serialization task */
    public FunctionReferenceContext(Map<String, ExpressionFunction> functions, Map<String, String> bindings) {
        this.functions = Map.copyOf(functions);
        if (bindings != null)
            this.bindings.putAll(bindings);
    }

    private static Map<String, ExpressionFunction> toMap(Collection<ExpressionFunction> list) {
        Map<String, ExpressionFunction> mapBuilder = new HashMap<>();
        for (ExpressionFunction function : list)
            mapBuilder.put(function.getName(), function);
        return Map.copyOf(mapBuilder);
    }

    /** Returns a function or null if it isn't defined in this context */
    public ExpressionFunction getFunction(String name) { return functions.get(name); }

    protected Map<String, ExpressionFunction> getFunctions() { return functions; }

    /** Returns the resolution of an identifier, or null if it isn't defined in this context */
    public String getBinding(String name) { return bindings.get(name); }

    /** Returns a new context with the bindings replaced by the given bindings */
    public FunctionReferenceContext withBindings(Map<String, String> bindings) {
        return new FunctionReferenceContext(this.functions, bindings);
    }

    /** Returns a fresh context without bindings */
    public FunctionReferenceContext withoutBindings() {
        return new FunctionReferenceContext(this.functions);
    }

}