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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
// 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.rule;
import com.google.common.collect.ImmutableMap;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Context needed to serialize an expression to a string. This has the lifetime of a single serialization
*
* @author bratseth
*/
public class SerializationContext {
/** Expression functions indexed by name */
private final ImmutableMap<String, ExpressionFunction> functions;
/** A cache of already serialized expressions indexed by name */
private final Map<String, String> serializedFunctions;
/** Mapping from argument names to the expressions they resolve to */
public final Map<String, String> bindings = new HashMap<>();
/** Create a context for a single serialization task */
public SerializationContext() {
this(Collections.emptyList());
}
/** Create a context for a single serialization task */
public SerializationContext(Collection<ExpressionFunction> functions) {
this(functions, Collections.emptyMap(), new LinkedHashMap<>());
}
/** Create a context for a single serialization task */
public SerializationContext(Map<String, ExpressionFunction> functions) {
this(functions.values());
}
/** Create a context for a single serialization task */
public SerializationContext(List<ExpressionFunction> functions, Map<String, String> bindings) {
this(functions, bindings, new LinkedHashMap<>());
}
/**
* Create a context for a single serialization task
*
* @param functions the functions of this
* @param bindings the arguments of this
* @param serializedFunctions a cache of serializedFunctions - the ownership of this map
* is <b>transferred</b> to this and will be modified in it
*/
public SerializationContext(Collection<ExpressionFunction> functions, Map<String, String> bindings,
Map<String, String> serializedFunctions) {
this(toMap(functions), bindings, serializedFunctions);
}
private static ImmutableMap<String, ExpressionFunction> toMap(Collection<ExpressionFunction> list) {
ImmutableMap.Builder<String,ExpressionFunction> mapBuilder = new ImmutableMap.Builder<>();
for (ExpressionFunction function : list)
mapBuilder.put(function.getName(), function);
return mapBuilder.build();
}
/**
* Create a context for a single serialization task
*
* @param functions the functions of this
* @param bindings the arguments of this
* @param serializedFunctions a cache of serializedFunctions - the ownership of this map
* is <b>transferred</b> to this and will be modified in it
*/
public SerializationContext(ImmutableMap<String,ExpressionFunction> functions, Map<String, String> bindings,
Map<String, String> serializedFunctions) {
this.functions = functions;
this.serializedFunctions = serializedFunctions;
if (bindings != null)
this.bindings.putAll(bindings);
}
/**
* Returns a function or null if it isn't defined in this context
*/
public ExpressionFunction getFunction(String name) { return functions.get(name); }
/** Adds the serialization of a function */
public void addFunctionSerialization(String name, String expressionString) {
serializedFunctions.put(name, expressionString);
}
/** Returns the existing serialization of a function, or null if none */
public String getFunctionSerialization(String name) {
return serializedFunctions.get(name);
}
/**
* Returns the resolution of an argument, or null if it isn't defined in this context
*/
public String getBinding(String name) { return bindings.get(name); }
/**
* Returns a new context which shares the functions and serialized function map with this but has different
* arguments.
*/
public SerializationContext createBinding(Map<String, String> arguments) {
return new SerializationContext(this.functions, arguments, this.serializedFunctions);
}
public Map<String, String> serializedFunctions() { return serializedFunctions; }
}
|