11package com .github .sidhant92 .boolparser .application ;
22
3+ import java .util .ArrayList ;
4+ import java .util .Collection ;
5+ import java .util .List ;
36import java .util .Map ;
47import java .util .Optional ;
8+ import java .util .stream .Collectors ;
59import org .apache .commons .lang3 .tuple .Pair ;
610import com .github .sidhant92 .boolparser .constant .ContainerDataType ;
711import com .github .sidhant92 .boolparser .constant .DataType ;
1115import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticNode ;
1216import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticUnaryNode ;
1317import com .github .sidhant92 .boolparser .domain .Node ;
18+ import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticFunctionNode ;
1419import com .github .sidhant92 .boolparser .exception .UnsupportedToken ;
20+ import com .github .sidhant92 .boolparser .function .FunctionEvaluatorService ;
1521import com .github .sidhant92 .boolparser .operator .OperatorService ;
1622import com .github .sidhant92 .boolparser .parser .BoolExpressionParser ;
1723import com .github .sidhant92 .boolparser .util .ValueUtils ;
@@ -28,9 +34,12 @@ public class ArithmeticExpressionEvaluator {
2834
2935 private final OperatorService operatorService ;
3036
37+ private final FunctionEvaluatorService functionEvaluatorService ;
38+
3139 public ArithmeticExpressionEvaluator (final BoolExpressionParser boolExpressionParser ) {
3240 this .boolExpressionParser = boolExpressionParser ;
3341 operatorService = new OperatorService ();
42+ functionEvaluatorService = new FunctionEvaluatorService ();
3443 }
3544
3645 public Try <Object > evaluate (final String expression , final Map <String , Object > data ) {
@@ -50,6 +59,8 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
5059 return evaluateArithmeticLeafToken ((ArithmeticLeafNode ) node , data );
5160 case ARITHMETIC_UNARY :
5261 return evaluateUnaryArithmeticToken ((ArithmeticUnaryNode ) node , data );
62+ case ARITHMETIC_FUNCTION :
63+ return evaluateArithmeticFunctionToken ((ArithmeticFunctionNode ) node , data );
5364 case STRING :
5465 return evaluateStringToken ((StringNode ) node , data );
5566 default :
@@ -63,7 +74,8 @@ private Object evaluateStringToken(final StringNode stringNode, final Map<String
6374 }
6475
6576 private Pair <Object , DataType > evaluateArithmeticLeafToken (final ArithmeticLeafNode arithmeticLeafNode , final Map <String , Object > data ) {
66- final Optional <Object > fetchedValue = ValueUtils .getValueFromMap (arithmeticLeafNode .getOperand ().toString (), data );
77+ final Optional <Object > fetchedValue = arithmeticLeafNode .getDataType () != DataType .STRING ? Optional .of (
78+ arithmeticLeafNode .getOperand ()) : ValueUtils .getValueFromMap (arithmeticLeafNode .getOperand ().toString (), data );
6779 return fetchedValue
6880 .map (o -> Pair .of (o , ValueUtils .getDataType (o )))
6981 .orElseGet (() -> Pair .of (arithmeticLeafNode .getOperand (), arithmeticLeafNode .getDataType ()));
@@ -80,6 +92,22 @@ private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmetic
8092 return operatorService .evaluateArithmeticOperator (resolvedValue , dataType , null , null , Operator .UNARY , ContainerDataType .PRIMITIVE );
8193 }
8294
95+ private Object evaluateArithmeticFunctionToken (final ArithmeticFunctionNode arithmeticFunctionNode , final Map <String , Object > data ) {
96+ final List <Pair <Object , DataType >> resolvedValues = arithmeticFunctionNode .getItems ()
97+ .stream ()
98+ .map (item -> evaluateArithmeticLeafToken (item , data ))
99+ .collect (Collectors .toList ());
100+ final List <Pair <Object , DataType >> flattenedValues = new ArrayList <>();
101+ resolvedValues .forEach (value -> {
102+ if (value .getKey () instanceof Collection ) {
103+ ((Collection <?>) value .getKey ()).forEach (v -> flattenedValues .add (Pair .of (v , ValueUtils .getDataType (v ))));
104+ } else {
105+ flattenedValues .add (value );
106+ }
107+ });
108+ return functionEvaluatorService .evaluateArithmeticFunction (arithmeticFunctionNode .getFunctionType (), flattenedValues );
109+ }
110+
83111 private Object evaluateArithmeticToken (final ArithmeticNode arithmeticNode , final Map <String , Object > data ) {
84112 final Object leftValue = evaluateToken (arithmeticNode .getLeft (), data );
85113 final Object rightValue = evaluateToken (arithmeticNode .getRight (), data );
0 commit comments