File tree Expand file tree Collapse file tree 2 files changed +11
-8
lines changed
src/main/java/io/github/jamsesso/jsonlogic Expand file tree Collapse file tree 2 files changed +11
-8
lines changed Original file line number Diff line number Diff line change 12
12
import java .util .function .Function ;
13
13
14
14
public final class JsonLogic {
15
- private final List < JsonLogicExpression > expressions ;
16
- private final Map <String , JsonLogicNode > parseCache ;
15
+ private final Map < String , JsonLogicNode > parseCache = new ConcurrentHashMap <>() ;
16
+ private final Map <String , JsonLogicExpression > expressions = new ConcurrentHashMap <>() ;
17
17
private JsonLogicEvaluator evaluator ;
18
18
19
19
public JsonLogic () {
20
- this .expressions = new ArrayList <>();
21
- this .parseCache = new ConcurrentHashMap <>();
22
-
23
20
// Add default operations
24
21
addOperation (MathExpression .ADD );
25
22
addOperation (MathExpression .SUBTRACT );
@@ -72,7 +69,7 @@ public String key() {
72
69
}
73
70
74
71
public JsonLogic addOperation (JsonLogicExpression expression ) {
75
- expressions .add ( expression );
72
+ expressions .put ( expression . key (), expression );
76
73
evaluator = null ;
77
74
78
75
return this ;
Original file line number Diff line number Diff line change 6
6
import java .util .*;
7
7
8
8
public class JsonLogicEvaluator {
9
- private final Map <String , JsonLogicExpression > expressions = new HashMap <>() ;
9
+ private final Map <String , JsonLogicExpression > expressions ;
10
10
11
11
public JsonLogicEvaluator (Collection <JsonLogicExpression > expressions ) {
12
+ this .expressions = new HashMap <>();
13
+
12
14
for (JsonLogicExpression expression : expressions ) {
13
15
this .expressions .put (expression .key (), expression );
14
16
}
15
17
}
16
18
19
+ public JsonLogicEvaluator (Map <String , JsonLogicExpression > expressions ) {
20
+ this .expressions = Collections .unmodifiableMap (expressions );
21
+ }
22
+
17
23
public Object evaluate (JsonLogicNode node , Object data ) throws JsonLogicEvaluationException {
18
24
switch (node .getType ()) {
19
25
case PRIMITIVE : return evaluate ((JsonLogicPrimitive ) node );
@@ -139,4 +145,4 @@ public static Object transform(Object value) {
139
145
140
146
return value ;
141
147
}
142
- }
148
+ }
You can’t perform that action at this time.
0 commit comments