-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathLayeredEvaluationContext.java
More file actions
315 lines (273 loc) · 9.74 KB
/
LayeredEvaluationContext.java
File metadata and controls
315 lines (273 loc) · 9.74 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package dev.openfeature.sdk;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* LayeredEvaluationContext implements EvaluationContext by layering multiple contexts:
* API-level, Transaction-level, Client-level, Invocation-level, and Hook-level.
* The contexts are checked in that order for values, with Hook-level having the highest precedence.
*/
public class LayeredEvaluationContext implements EvaluationContext {
private final EvaluationContext apiContext;
private final EvaluationContext transactionContext;
private final EvaluationContext clientContext;
private final EvaluationContext invocationContext;
private ArrayList<EvaluationContext> hookContexts;
private String targetingKey;
private Set<String> keySet = null;
// Lazily computed resolved attribute map for this layered context.
// This must be invalidated whenever the underlying layers change.
private Map<String, Value> cachedMap;
/**
* Constructor for LayeredEvaluationContext.
*/
public LayeredEvaluationContext(
EvaluationContext apiContext,
EvaluationContext transactionContext,
EvaluationContext clientContext,
EvaluationContext invocationContext) {
this.apiContext = apiContext;
this.transactionContext = transactionContext;
this.clientContext = clientContext;
this.invocationContext = invocationContext;
if (invocationContext != null && invocationContext.getTargetingKey() != null) {
this.targetingKey = invocationContext.getTargetingKey();
} else if (clientContext != null && clientContext.getTargetingKey() != null) {
this.targetingKey = clientContext.getTargetingKey();
} else if (transactionContext != null && transactionContext.getTargetingKey() != null) {
this.targetingKey = transactionContext.getTargetingKey();
} else if (apiContext != null && apiContext.getTargetingKey() != null) {
this.targetingKey = apiContext.getTargetingKey();
} else {
this.targetingKey = null;
}
}
public static LayeredEvaluationContext empty() {
return new LayeredEvaluationContext(null, null, null, null);
}
@Override
public String getTargetingKey() {
return targetingKey;
}
@Override
public EvaluationContext merge(EvaluationContext overridingContext) {
var merged = new LayeredEvaluationContext(apiContext, transactionContext, clientContext, invocationContext);
if (this.hookContexts == null) {
merged.hookContexts = new ArrayList<>(1);
} else {
merged.hookContexts = new ArrayList<>(this.hookContexts.size() + 1);
merged.hookContexts.addAll(this.hookContexts);
}
merged.hookContexts.add(overridingContext);
var otherTargetingKey = overridingContext.getTargetingKey();
if (otherTargetingKey != null) {
merged.targetingKey = otherTargetingKey;
}
return merged;
}
@Override
public boolean isEmpty() {
return (invocationContext == null || invocationContext.isEmpty())
&& (clientContext == null || clientContext.isEmpty())
&& (transactionContext == null || transactionContext.isEmpty())
&& (apiContext == null || apiContext.isEmpty())
&& areHookContextsEmpty();
}
private boolean areHookContextsEmpty() {
if (hookContexts == null || hookContexts.isEmpty()) {
return true;
}
for (int i = 0; i < hookContexts.size(); i++) {
var current = hookContexts.get(i);
if (!current.isEmpty()) {
return false;
}
}
return true;
}
@Override
public Set<String> keySet() {
return Collections.unmodifiableSet(ensureKeySet());
}
private Set<String> ensureKeySet() {
if (this.keySet != null) {
return this.keySet;
}
var keys = new HashSet<String>();
if (hookContexts != null) {
for (int i = 0; i < hookContexts.size(); i++) {
var current = hookContexts.get(i);
keys.addAll(current.keySet());
}
}
if (invocationContext != null) {
keys.addAll(invocationContext.keySet());
}
if (clientContext != null) {
keys.addAll(clientContext.keySet());
}
if (transactionContext != null) {
keys.addAll(transactionContext.keySet());
}
if (apiContext != null) {
keys.addAll(apiContext.keySet());
}
this.keySet = keys;
return keys;
}
private Value getFromContext(EvaluationContext context, String key) {
if (context != null) {
return context.getValue(key);
}
return null;
}
private Value getFromContext(ArrayList<EvaluationContext> context, String key) {
if (context == null) {
return null;
}
for (int i = context.size() - 1; i >= 0; i--) {
var current = context.get(i);
var value = getFromContext(current, key);
if (value != null) {
return value;
}
}
return null;
}
@Override
public Value getValue(String key) {
var hookValue = getFromContext(hookContexts, key);
if (hookValue != null) {
return hookValue;
}
var invocationValue = getFromContext(invocationContext, key);
if (invocationValue != null) {
return invocationValue;
}
var clientValue = getFromContext(clientContext, key);
if (clientValue != null) {
return clientValue;
}
var transactionValue = getFromContext(transactionContext, key);
if (transactionValue != null) {
return transactionValue;
}
return getFromContext(apiContext, key);
}
private Map<String, Value> getResolvedMap() {
if (cachedMap != null) {
return cachedMap;
}
if (keySet != null && keySet.isEmpty()) {
cachedMap = Collections.emptyMap();
return cachedMap;
}
HashMap<String, Value> map;
if (keySet != null) {
// use helper to size the map based on expected entries
map = HashMapUtils.forEntries(keySet.size());
} else {
map = new HashMap<>();
}
if (apiContext != null) {
map.putAll(apiContext.asMap());
}
if (transactionContext != null) {
map.putAll(transactionContext.asMap());
}
if (clientContext != null) {
map.putAll(clientContext.asMap());
}
if (invocationContext != null) {
map.putAll(invocationContext.asMap());
}
if (hookContexts != null) {
for (int i = 0; i < hookContexts.size(); i++) {
EvaluationContext hookContext = hookContexts.get(i);
map.putAll(hookContext.asMap());
}
}
cachedMap = Collections.unmodifiableMap(map);
return cachedMap;
}
@Override
public Map<String, Value> asMap() {
// Return a defensive copy so callers can't mutate our cached map.
return new HashMap<>(getResolvedMap());
}
@Override
public Map<String, Value> asUnmodifiableMap() {
if (keySet != null && keySet.isEmpty()) {
return Collections.emptyMap();
}
return getResolvedMap();
}
@Override
public Map<String, Object> asObjectMap() {
if (keySet != null && keySet.isEmpty()) {
return new HashMap<>(0);
}
HashMap<String, Object> map;
if (keySet != null) {
// use helper to size the map based on expected entries
map = HashMapUtils.forEntries(keySet.size());
} else {
map = new HashMap<>();
}
if (apiContext != null) {
map.putAll(apiContext.asObjectMap());
}
if (transactionContext != null) {
map.putAll(transactionContext.asObjectMap());
}
if (clientContext != null) {
map.putAll(clientContext.asObjectMap());
}
if (invocationContext != null) {
map.putAll(invocationContext.asObjectMap());
}
if (hookContexts != null) {
for (int i = 0; i < hookContexts.size(); i++) {
EvaluationContext hookContext = hookContexts.get(i);
map.putAll(hookContext.asObjectMap());
}
}
return map;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof EvaluationContext)) {
return false;
}
EvaluationContext that = (EvaluationContext) o;
if (that instanceof LayeredEvaluationContext) {
return this.getResolvedMap().equals(((LayeredEvaluationContext) that).getResolvedMap());
}
return this.getResolvedMap().equals(that.asUnmodifiableMap());
}
@Override
public int hashCode() {
return getResolvedMap().hashCode();
}
void putHookContext(EvaluationContext context) {
if (context == null || context.isEmpty()) {
return;
}
var targetingKey = context.getTargetingKey();
if (targetingKey != null) {
this.targetingKey = targetingKey;
}
if (this.hookContexts == null) {
this.hookContexts = new ArrayList<>();
}
this.hookContexts.add(context);
this.keySet = null;
this.cachedMap = null;
}
}