-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ExpressionEvalMap.java
370 lines (290 loc) · 9.66 KB
/
ExpressionEvalMap.java
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.expression;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* <p>
* An immutable {@link AbstractMap} implementation that wraps a {@code Map<String, Object>},
* where values must be instances of {@link String} or {@link Expression},
* and evaluates an {@code expression} for the provided {@code key} from the underlying
* {@code original} Map.
* </p>
* <p>
* Any mutating operations ({@link #put(String, Object)}, {@link #remove(Object)} etc.)
* are not allowed on instances of this class. Mutation can be performed on underlying Map
* if it supports it.
* </p>
* <p>
* A {@link ExpressionEvalMapBuilder} must be used to instantiate this class
* via its {@link #from(Map)} method:
* </p>
* <pre class="code">
* {@code
*ExpressionEvalMap evalMap = ExpressionEvalMap
* .from(expressions)
* .usingCallback(new EvaluationCallback() {
* Object evaluate(Expression expression) {
* // return some expression evaluation
* }
* })
* .build();
*}
* </pre>
* <p>
* Thread-safety depends on the original underlying Map.
* Objects of this class are not serializable.
* </p>
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 3.0
*/
public final class ExpressionEvalMap extends AbstractMap<String, Object> {
public static final EvaluationCallback SIMPLE_CALLBACK = Expression::getValue;
private final Map<String, ?> original;
private final EvaluationCallback evaluationCallback;
private ExpressionEvalMap(Map<String, ?> original, EvaluationCallback evaluationCallback) {
this.original = original;
this.evaluationCallback = evaluationCallback;
}
/**
* Gets the {@code value}({@link Expression}) for the provided {@code key}
* from {@link #original} and returns the result of evaluation using {@link #evaluationCallback}.
*/
@Override
@Nullable
public Object get(Object key) {
Object value = this.original.get(key);
if (value != null) {
Expression expression;
if (value instanceof Expression) {
expression = (Expression) value;
}
else if (value instanceof String) {
expression = new LiteralExpression((String) value);
}
else {
throw new IllegalArgumentException("Values must be "
+ "'java.lang.String' or 'org.springframework.expression.Expression'; the value type for key "
+ key + " is : " + value.getClass());
}
return this.evaluationCallback.evaluate(expression);
}
return null;
}
@Override
public Set<Map.Entry<String, Object>> entrySet() {
return this.original.keySet()
.stream()
.map((key) -> new SimpleImmutableEntry<>(key, get(key)))
.collect(Collectors.toSet());
}
@Override
public Collection<Object> values() {
return this.original.values()
.stream()
.map(this::get)
.collect(Collectors.toList());
}
@Override
public boolean containsKey(Object key) {
return this.original.containsKey(key);
}
@Override
public Set<String> keySet() {
return this.original.keySet();
}
@Override
public boolean isEmpty() {
return this.original.isEmpty();
}
@Override
public int size() {
return this.original.size();
}
@Override
public boolean equals(Object o) {
return this.original.equals(o);
}
@Override
public int hashCode() {
return this.original.hashCode();
}
@Override
public String toString() {
return this.original.toString();
}
@Override
public Object put(String key, Object value) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map<? extends String, ?> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
@Override
public Object remove(Object key) {
throw new UnsupportedOperationException();
}
public static ExpressionEvalMapBuilder from(Map<String, ?> expressions) {
Assert.notNull(expressions, "'expressions' must not be null.");
return new ExpressionEvalMapBuilder(expressions);
}
/**
* Implementations of this interface can be provided to build 'on demand {@link #get(Object)} logic'
* for {@link ExpressionEvalMap}.
*/
@FunctionalInterface
public interface EvaluationCallback {
@Nullable
Object evaluate(Expression expression);
}
/**
* The {@link EvaluationCallback} implementation which evaluates an expression using
* the provided {@code context}, {@code root} and {@code returnType} variables.
*/
public static class ComponentsEvaluationCallback implements EvaluationCallback {
@Nullable
private final EvaluationContext context;
@Nullable
private final Object root;
private final boolean rootExplicitlySet;
@Nullable
private final Class<?> returnType;
public ComponentsEvaluationCallback(@Nullable EvaluationContext context, @Nullable Object root,
boolean rootExplicitlySet, @Nullable Class<?> returnType) {
this.context = context;
this.root = root;
this.rootExplicitlySet = rootExplicitlySet;
this.returnType = returnType;
}
@Override
public Object evaluate(Expression expression) {
if (this.context != null) {
if (this.rootExplicitlySet) {
return expression.getValue(this.context, this.root, this.returnType);
}
else {
return expression.getValue(this.context, this.returnType);
}
}
return expression.getValue(this.root, this.returnType);
}
}
/**
* The builder class to instantiate {@link ExpressionEvalMap}.
*/
public static final class ExpressionEvalMapBuilder {
private final Map<String, ?> expressions;
private EvaluationCallback evaluationCallback;
@Nullable
private EvaluationContext context;
@Nullable
private Object root;
private boolean rootExplicitlySet;
@Nullable
private Class<?> returnType;
private final ExpressionEvalMapComponentsBuilder evalMapComponentsBuilder =
new ExpressionEvalMapComponentsBuilderImpl();
private final ExpressionEvalMapFinalBuilder finalBuilder = new ExpressionEvalMapFinalBuilderImpl();
private ExpressionEvalMapBuilder(Map<String, ?> expressions) {
this.expressions = expressions;
}
public ExpressionEvalMapFinalBuilder usingCallback(EvaluationCallback callback) {
this.evaluationCallback = callback;
return this.finalBuilder;
}
public ExpressionEvalMapFinalBuilder usingSimpleCallback() {
return this.usingCallback(SIMPLE_CALLBACK);
}
public ExpressionEvalMapComponentsBuilder usingEvaluationContext(EvaluationContext context) {
this.context = context;
return this.evalMapComponentsBuilder;
}
public ExpressionEvalMapComponentsBuilder withRoot(@Nullable Object root) {
this.root = root;
this.rootExplicitlySet = true;
return this.evalMapComponentsBuilder;
}
public ExpressionEvalMapComponentsBuilder withReturnType(Class<?> returnType) {
this.returnType = returnType;
return this.evalMapComponentsBuilder;
}
private class ExpressionEvalMapFinalBuilderImpl implements ExpressionEvalMapFinalBuilder {
ExpressionEvalMapFinalBuilderImpl() {
super();
}
@Override
public ExpressionEvalMap build() {
if (ExpressionEvalMapBuilder.this.evaluationCallback != null) {
return new ExpressionEvalMap(ExpressionEvalMapBuilder.this.expressions,
ExpressionEvalMapBuilder.this.evaluationCallback);
}
else {
return new ExpressionEvalMap(ExpressionEvalMapBuilder.this.expressions,
new ComponentsEvaluationCallback(ExpressionEvalMapBuilder.this.context,
ExpressionEvalMapBuilder.this.root, ExpressionEvalMapBuilder.this.rootExplicitlySet,
ExpressionEvalMapBuilder.this.returnType));
}
}
}
private class ExpressionEvalMapComponentsBuilderImpl extends ExpressionEvalMapFinalBuilderImpl
implements ExpressionEvalMapComponentsBuilder {
ExpressionEvalMapComponentsBuilderImpl() {
super();
}
@Override
public ExpressionEvalMapComponentsBuilder usingEvaluationContext(EvaluationContext context) {
return ExpressionEvalMapBuilder.this.usingEvaluationContext(context);
}
@Override
public ExpressionEvalMapComponentsBuilder withRoot(@Nullable Object root) {
return ExpressionEvalMapBuilder.this.withRoot(root);
}
@Override
public ExpressionEvalMapComponentsBuilder withReturnType(Class<?> returnType) {
return ExpressionEvalMapBuilder.this.withReturnType(returnType);
}
}
}
@FunctionalInterface
public interface ExpressionEvalMapFinalBuilder {
ExpressionEvalMap build();
}
public interface ExpressionEvalMapComponentsBuilder extends ExpressionEvalMapFinalBuilder {
ExpressionEvalMapComponentsBuilder usingEvaluationContext(EvaluationContext context);
ExpressionEvalMapComponentsBuilder withRoot(@Nullable Object root);
ExpressionEvalMapComponentsBuilder withReturnType(Class<?> returnType);
}
}