Skip to content

Commit 421d7ba

Browse files
authored
Merge pull request #40 from melniknow/fix/redundant_storage_of_operations
Removed the redundant storage of operations
2 parents f18c726 + 582abac commit 421d7ba

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
import java.util.function.Function;
1313

1414
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<>();
1717
private JsonLogicEvaluator evaluator;
1818

1919
public JsonLogic() {
20-
this.expressions = new ArrayList<>();
21-
this.parseCache = new ConcurrentHashMap<>();
22-
2320
// Add default operations
2421
addOperation(MathExpression.ADD);
2522
addOperation(MathExpression.SUBTRACT);
@@ -72,7 +69,7 @@ public String key() {
7269
}
7370

7471
public JsonLogic addOperation(JsonLogicExpression expression) {
75-
expressions.add(expression);
72+
expressions.put(expression.key(), expression);
7673
evaluator = null;
7774

7875
return this;

src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluator.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
import java.util.*;
77

88
public class JsonLogicEvaluator {
9-
private final Map<String, JsonLogicExpression> expressions = new HashMap<>();
9+
private final Map<String, JsonLogicExpression> expressions;
1010

1111
public JsonLogicEvaluator(Collection<JsonLogicExpression> expressions) {
12+
this.expressions = new HashMap<>();
13+
1214
for (JsonLogicExpression expression : expressions) {
1315
this.expressions.put(expression.key(), expression);
1416
}
1517
}
1618

19+
public JsonLogicEvaluator(Map<String, JsonLogicExpression> expressions) {
20+
this.expressions = Collections.unmodifiableMap(expressions);
21+
}
22+
1723
public Object evaluate(JsonLogicNode node, Object data) throws JsonLogicEvaluationException {
1824
switch (node.getType()) {
1925
case PRIMITIVE: return evaluate((JsonLogicPrimitive) node);
@@ -139,4 +145,4 @@ public static Object transform(Object value) {
139145

140146
return value;
141147
}
142-
}
148+
}

0 commit comments

Comments
 (0)