Skip to content

Commit 9f8b543

Browse files
committed
replace pair for evaluated node
1 parent 9f65eec commit 9f8b543

14 files changed

Lines changed: 101 additions & 72 deletions

src/main/java/com/github/sidhant92/boolparser/application/ArithmeticExpressionEvaluator.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import java.util.Map;
77
import java.util.Optional;
88
import java.util.stream.Collectors;
9-
import org.apache.commons.lang3.tuple.Pair;
109
import com.github.sidhant92.boolparser.constant.ContainerDataType;
1110
import com.github.sidhant92.boolparser.constant.DataType;
1211
import com.github.sidhant92.boolparser.constant.Operator;
12+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1313
import com.github.sidhant92.boolparser.domain.UnaryNode;
1414
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1515
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
@@ -62,46 +62,46 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
6262
case ARITHMETIC_FUNCTION:
6363
return evaluateArithmeticFunctionToken((ArithmeticFunctionNode) node, data);
6464
case UNARY:
65-
return evaluateStringToken((UnaryNode) node, data);
65+
return evaluateUnaryToken((UnaryNode) node, data);
6666
default:
6767
log.error("unsupported token {}", node.getTokenType());
6868
throw new UnsupportedToken();
6969
}
7070
}
7171

72-
private Object evaluateStringToken(final UnaryNode unaryNode, final Map<String, Object> data) {
72+
private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, Object> data) {
7373
return unaryNode.getDataType() == DataType.STRING ? ValueUtils.getValueFromMap(unaryNode.getValue().toString(), data)
7474
.orElse(unaryNode.getValue()) : unaryNode.getValue();
7575
}
7676

77-
private Pair<Object, DataType> evaluateArithmeticLeafToken(final ArithmeticLeafNode arithmeticLeafNode, final Map<String, Object> data) {
77+
private EvaluatedNode evaluateArithmeticLeafToken(final ArithmeticLeafNode arithmeticLeafNode, final Map<String, Object> data) {
7878
final Optional<Object> fetchedValue = arithmeticLeafNode.getDataType() != DataType.STRING ? Optional.of(
7979
arithmeticLeafNode.getOperand()) : ValueUtils.getValueFromMap(arithmeticLeafNode.getOperand().toString(), data);
8080
return fetchedValue
81-
.map(o -> Pair.of(o, ValueUtils.getDataType(o)))
82-
.orElseGet(() -> Pair.of(arithmeticLeafNode.getOperand(), arithmeticLeafNode.getDataType()));
81+
.map(o -> EvaluatedNode.builder().value(o).dataType(ValueUtils.getDataType(o)).build())
82+
.orElseGet(() -> EvaluatedNode.builder().value(arithmeticLeafNode.getOperand()).dataType(arithmeticLeafNode.getDataType()).build());
8383
}
8484

8585
private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmeticUnaryNode, final Map<String, Object> data) {
8686
final Object resolvedValue = evaluateToken(arithmeticUnaryNode.getOperand(), data);
87-
if (resolvedValue instanceof Pair) {
88-
final Pair<Object, DataType> pair = (Pair<Object, DataType>) resolvedValue;
89-
return operatorService.evaluateArithmeticOperator(pair.getLeft(), pair.getRight(), null, null, Operator.UNARY,
87+
if (resolvedValue instanceof EvaluatedNode) {
88+
final EvaluatedNode evaluatedNode = (EvaluatedNode) resolvedValue;
89+
return operatorService.evaluateArithmeticOperator(evaluatedNode.getValue(), evaluatedNode.getDataType(), null, null, Operator.UNARY,
9090
ContainerDataType.PRIMITIVE);
9191
}
9292
final DataType dataType = ValueUtils.getDataType(resolvedValue);
9393
return operatorService.evaluateArithmeticOperator(resolvedValue, dataType, null, null, Operator.UNARY, ContainerDataType.PRIMITIVE);
9494
}
9595

9696
private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arithmeticFunctionNode, final Map<String, Object> data) {
97-
final List<Pair<Object, DataType>> resolvedValues = arithmeticFunctionNode.getItems()
97+
final List<EvaluatedNode> resolvedValues = arithmeticFunctionNode.getItems()
9898
.stream()
9999
.map(item -> evaluateArithmeticLeafToken(item, data))
100100
.collect(Collectors.toList());
101-
final List<Pair<Object, DataType>> flattenedValues = new ArrayList<>();
101+
final List<EvaluatedNode> flattenedValues = new ArrayList<>();
102102
resolvedValues.forEach(value -> {
103-
if (value.getKey() instanceof Collection) {
104-
((Collection<?>) value.getKey()).forEach(v -> flattenedValues.add(Pair.of(v, ValueUtils.getDataType(v))));
103+
if (value.getValue() instanceof Collection) {
104+
((Collection<?>) value.getValue()).forEach(v -> flattenedValues.add(EvaluatedNode.builder().value(v).dataType(ValueUtils.getDataType(v)).build()));
105105
} else {
106106
flattenedValues.add(value);
107107
}
@@ -112,20 +112,20 @@ private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arit
112112
private Object evaluateArithmeticToken(final ArithmeticNode arithmeticNode, final Map<String, Object> data) {
113113
final Object leftValue = evaluateToken(arithmeticNode.getLeft(), data);
114114
final Object rightValue = evaluateToken(arithmeticNode.getRight(), data);
115-
if (leftValue instanceof Pair && rightValue instanceof Pair) {
116-
final Pair<Object, DataType> leftPair = (Pair<Object, DataType>) leftValue;
117-
final Pair<Object, DataType> rightPair = (Pair<Object, DataType>) rightValue;
118-
return operatorService.evaluateArithmeticOperator(leftPair.getLeft(), leftPair.getRight(), rightPair.getLeft(), rightPair.getRight(),
115+
if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode) {
116+
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;
117+
final EvaluatedNode rightPair = (EvaluatedNode) rightValue;
118+
return operatorService.evaluateArithmeticOperator(leftPair.getValue(), leftPair.getDataType(), rightPair.getValue(), rightPair.getDataType(),
119119
arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
120-
} else if (leftValue instanceof Pair) {
121-
final Pair<Object, DataType> leftPair = (Pair<Object, DataType>) leftValue;
120+
} else if (leftValue instanceof EvaluatedNode) {
121+
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;
122122
final DataType rightDataType = ValueUtils.getDataType(rightValue);
123-
return operatorService.evaluateArithmeticOperator(leftPair.getLeft(), leftPair.getRight(), rightValue, rightDataType,
123+
return operatorService.evaluateArithmeticOperator(leftPair.getValue(), leftPair.getDataType(), rightValue, rightDataType,
124124
arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
125-
} else if (rightValue instanceof Pair) {
126-
final Pair<Object, DataType> rightPair = (Pair<Object, DataType>) rightValue;
125+
} else if (rightValue instanceof EvaluatedNode) {
126+
final EvaluatedNode rightPair = (EvaluatedNode) rightValue;
127127
final DataType leftDataType = ValueUtils.getDataType(leftValue);
128-
return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, rightPair.getLeft(), rightPair.getRight(),
128+
return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, rightPair.getValue(), rightPair.getDataType(),
129129
arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
130130
} else {
131131
final DataType leftDataType = ValueUtils.getDataType(leftValue);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.sidhant92.boolparser.domain;
2+
3+
import com.github.sidhant92.boolparser.constant.DataType;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@AllArgsConstructor
10+
@Getter
11+
@Setter
12+
@Builder
13+
public class EvaluatedNode {
14+
private final DataType dataType;
15+
16+
private final Object value;
17+
}

src/main/java/com/github/sidhant92/boolparser/function/FunctionEvaluatorService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.github.sidhant92.boolparser.constant.ContainerDataType;
66
import com.github.sidhant92.boolparser.constant.DataType;
77
import com.github.sidhant92.boolparser.constant.FunctionType;
8+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
89
import com.github.sidhant92.boolparser.exception.InvalidContainerTypeException;
910
import com.github.sidhant92.boolparser.exception.InvalidDataType;
1011
import com.github.sidhant92.boolparser.exception.InvalidExpressionException;
@@ -21,7 +22,7 @@ public FunctionEvaluatorService() {
2122
FunctionFactory.initialize();
2223
}
2324

24-
public Object evaluateArithmeticFunction(final FunctionType functionType, final List<Pair<Object, DataType>> items) {
25+
public Object evaluateArithmeticFunction(final FunctionType functionType, final List<EvaluatedNode> items) {
2526
final AbstractFunction abstractFunction = FunctionFactory.getArithmeticFunction(functionType);
2627
if (items.isEmpty()) {
2728
log.error("Empty items not allowed");
@@ -33,15 +34,15 @@ public Object evaluateArithmeticFunction(final FunctionType functionType, final
3334
throw new InvalidContainerTypeException();
3435
}
3536
final boolean validDataType = items
36-
.stream().allMatch(item -> abstractFunction.getAllowedDataTypes().contains(item.getValue()));
37+
.stream().allMatch(item -> abstractFunction.getAllowedDataTypes().contains(item.getDataType()));
3738
if (!validDataType) {
3839
log.error("Invalid data type {} for function {}", items, functionType.name());
3940
throw new InvalidDataType();
4041
}
4142

4243
items.forEach(item -> {
43-
if (!ContainerDataType.PRIMITIVE.isValid(item.getValue(), item.getKey())) {
44-
log.error("Validation failed for the function {} for the operand {}", functionType.name(), item.getKey());
44+
if (!ContainerDataType.PRIMITIVE.isValid(item.getDataType(), item.getValue())) {
45+
log.error("Validation failed for the function {} for the operand {}", functionType.name(), item.getValue());
4546
throw new InvalidDataType();
4647
}
4748
});

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/AbstractFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import com.github.sidhant92.boolparser.constant.ContainerDataType;
66
import com.github.sidhant92.boolparser.constant.DataType;
77
import com.github.sidhant92.boolparser.constant.FunctionType;
8+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
89
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
910

1011
/**
1112
* @author sidhant.aggarwal
1213
* @since 21/05/2024
1314
*/
1415
public abstract class AbstractFunction {
15-
public abstract Object evaluate(final List<Pair<Object, DataType>> items);
16+
public abstract Object evaluate(final List<EvaluatedNode> items);
1617

1718
public abstract FunctionType getFunctionType();
1819

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/AvgFunction.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.sidhant92.boolparser.constant.ContainerDataType;
88
import com.github.sidhant92.boolparser.constant.DataType;
99
import com.github.sidhant92.boolparser.constant.FunctionType;
10+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1011
import com.github.sidhant92.boolparser.util.ValueUtils;
1112

1213
/**
@@ -15,19 +16,19 @@
1516
*/
1617
public class AvgFunction extends AbstractFunction {
1718
@Override
18-
public Object evaluate(final List<Pair<Object, DataType>> items) {
19+
public Object evaluate(final List<EvaluatedNode> items) {
1920
if (items
20-
.stream().anyMatch(a -> a.getValue().equals(DataType.DECIMAL))) {
21+
.stream().anyMatch(a -> a.getDataType().equals(DataType.DECIMAL))) {
2122
return ValueUtils.caseDouble(items
22-
.stream().mapToDouble(a -> Double.parseDouble(a.getKey().toString())).average().getAsDouble());
23+
.stream().mapToDouble(a -> Double.parseDouble(a.getValue().toString())).average().getAsDouble());
2324
}
2425
if (items
25-
.stream().anyMatch(a -> a.getValue().equals(DataType.LONG))) {
26+
.stream().anyMatch(a -> a.getDataType().equals(DataType.LONG))) {
2627
return ValueUtils.caseDouble(items
27-
.stream().mapToLong(a -> Long.parseLong(a.getKey().toString())).average().getAsDouble());
28+
.stream().mapToLong(a -> Long.parseLong(a.getValue().toString())).average().getAsDouble());
2829
}
2930
return ValueUtils.caseDouble(items
30-
.stream().mapToInt(a -> Integer.parseInt(a.getKey().toString())).average().getAsDouble());
31+
.stream().mapToInt(a -> Integer.parseInt(a.getValue().toString())).average().getAsDouble());
3132
}
3233

3334
@Override

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/IntFunction.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
import com.github.sidhant92.boolparser.constant.ContainerDataType;
88
import com.github.sidhant92.boolparser.constant.DataType;
99
import com.github.sidhant92.boolparser.constant.FunctionType;
10+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1011

1112
/**
1213
* @author sidhant.aggarwal
1314
* @since 21/05/2024
1415
*/
1516
public class IntFunction extends AbstractFunction {
1617
@Override
17-
public Object evaluate(final List<Pair<Object, DataType>> items) {
18-
final Pair<Object, DataType> item = items.get(0);
19-
if (item.getValue() == DataType.DECIMAL) {
20-
return ((Double) item.getKey()).intValue();
18+
public Object evaluate(final List<EvaluatedNode> items) {
19+
final EvaluatedNode item = items.get(0);
20+
if (item.getDataType() == DataType.DECIMAL) {
21+
return ((Double) item.getValue()).intValue();
2122
}
22-
if (item.getValue() == DataType.LONG) {
23-
return ((Long) item.getKey()).intValue();
23+
if (item.getDataType() == DataType.LONG) {
24+
return ((Long) item.getValue()).intValue();
2425
}
25-
return item.getKey();
26+
return item.getValue();
2627
}
2728

2829
@Override

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/LenFunction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
import com.github.sidhant92.boolparser.constant.ContainerDataType;
77
import com.github.sidhant92.boolparser.constant.DataType;
88
import com.github.sidhant92.boolparser.constant.FunctionType;
9+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
910

1011
/**
1112
* @author sidhant.aggarwal
1213
* @since 21/05/2024
1314
*/
1415
public class LenFunction extends AbstractFunction {
1516
@Override
16-
public Object evaluate(final List<Pair<Object, DataType>> items) {
17-
if (items.size() == 1 && items.get(0).getKey() instanceof String) {
18-
return ((String) items.get(0).getKey()).length();
17+
public Object evaluate(final List<EvaluatedNode> items) {
18+
if (items.size() == 1 && items.get(0).getValue() instanceof String) {
19+
return ((String) items.get(0).getValue()).length();
1920
}
2021
return items.size();
2122
}

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/MaxFunction.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.sidhant92.boolparser.constant.ContainerDataType;
88
import com.github.sidhant92.boolparser.constant.DataType;
99
import com.github.sidhant92.boolparser.constant.FunctionType;
10+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1011
import com.github.sidhant92.boolparser.util.ValueUtils;
1112

1213
/**
@@ -15,19 +16,19 @@
1516
*/
1617
public class MaxFunction extends AbstractFunction {
1718
@Override
18-
public Object evaluate(final List<Pair<Object, DataType>> items) {
19+
public Object evaluate(final List<EvaluatedNode> items) {
1920
if (items
20-
.stream().anyMatch(a -> a.getValue().equals(DataType.DECIMAL))) {
21+
.stream().anyMatch(a -> a.getDataType().equals(DataType.DECIMAL))) {
2122
return ValueUtils.caseDouble(items
22-
.stream().mapToDouble(a -> Double.parseDouble(a.getKey().toString())).max().getAsDouble());
23+
.stream().mapToDouble(a -> Double.parseDouble(a.getValue().toString())).max().getAsDouble());
2324
}
2425
if (items
25-
.stream().anyMatch(a -> a.getValue().equals(DataType.LONG))) {
26+
.stream().anyMatch(a -> a.getDataType().equals(DataType.LONG))) {
2627
return ValueUtils.caseDouble(items
27-
.stream().mapToLong(a -> Long.parseLong(a.getKey().toString())).max().getAsLong());
28+
.stream().mapToLong(a -> Long.parseLong(a.getValue().toString())).max().getAsLong());
2829
}
2930
return ValueUtils.caseDouble(items
30-
.stream().mapToInt(a -> Integer.parseInt(a.getKey().toString())).max().getAsInt());
31+
.stream().mapToInt(a -> Integer.parseInt(a.getValue().toString())).max().getAsInt());
3132
}
3233

3334
@Override

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/MeanFunction.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@
99
import com.github.sidhant92.boolparser.constant.ContainerDataType;
1010
import com.github.sidhant92.boolparser.constant.DataType;
1111
import com.github.sidhant92.boolparser.constant.FunctionType;
12+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
13+
import com.github.sidhant92.boolparser.util.ValueUtils;
1214

1315
/**
1416
* @author sidhant.aggarwal
1517
* @since 21/05/2024
1618
*/
1719
public class MeanFunction extends AbstractFunction {
1820
@Override
19-
public Object evaluate(final List<Pair<Object, DataType>> items) {
21+
public Object evaluate(final List<EvaluatedNode> items) {
2022
final double mean = StatUtils.mean(items
21-
.stream().mapToDouble(a -> Double.parseDouble(a.getKey().toString())).toArray());
22-
if ((int) mean == mean) {
23-
return (int) mean;
24-
}
25-
return mean;
23+
.stream().mapToDouble(a -> Double.parseDouble(a.getValue().toString())).toArray());
24+
return ValueUtils.caseDouble(mean);
2625
}
2726

2827
@Override

src/main/java/com/github/sidhant92/boolparser/function/arithmetic/MedianFunction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.sidhant92.boolparser.constant.ContainerDataType;
99
import com.github.sidhant92.boolparser.constant.DataType;
1010
import com.github.sidhant92.boolparser.constant.FunctionType;
11+
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1112
import com.github.sidhant92.boolparser.util.ValueUtils;
1213

1314
/**
@@ -22,9 +23,9 @@ public MedianFunction() {
2223
}
2324

2425
@Override
25-
public Object evaluate(final List<Pair<Object, DataType>> items) {
26+
public Object evaluate(final List<EvaluatedNode> items) {
2627
final double res = median.evaluate(items
27-
.stream().mapToDouble(a -> Double.parseDouble(a.getKey().toString())).toArray());
28+
.stream().mapToDouble(a -> Double.parseDouble(a.getValue().toString())).toArray());
2829
return ValueUtils.caseDouble(res);
2930
}
3031

0 commit comments

Comments
 (0)