1111import com .github .sidhant92 .boolparser .constant .Operator ;
1212import com .github .sidhant92 .boolparser .domain .EvaluatedNode ;
1313import com .github .sidhant92 .boolparser .domain .UnaryNode ;
14- import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticLeafNode ;
1514import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticNode ;
1615import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticUnaryNode ;
1716import com .github .sidhant92 .boolparser .domain .Node ;
@@ -56,7 +55,7 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
5655 case ARITHMETIC :
5756 return evaluateArithmeticToken ((ArithmeticNode ) node , data );
5857 case ARITHMETIC_LEAF :
59- return evaluateArithmeticLeafToken (( ArithmeticLeafNode ) node , data );
58+ return evaluateUnaryNode (( UnaryNode ) node , data );
6059 case ARITHMETIC_UNARY :
6160 return evaluateUnaryArithmeticToken ((ArithmeticUnaryNode ) node , data );
6261 case ARITHMETIC_FUNCTION :
@@ -74,12 +73,12 @@ private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, O
7473 .orElse (unaryNode .getValue ()) : unaryNode .getValue ();
7574 }
7675
77- private EvaluatedNode evaluateArithmeticLeafToken (final ArithmeticLeafNode arithmeticLeafNode , final Map <String , Object > data ) {
78- final Optional <Object > fetchedValue = arithmeticLeafNode .getDataType () != DataType .STRING ? Optional .of (
79- arithmeticLeafNode . getOperand ()) : ValueUtils .getValueFromMap (arithmeticLeafNode . getOperand ().toString (), data );
76+ private EvaluatedNode evaluateUnaryNode (final UnaryNode unaryNode , final Map <String , Object > data ) {
77+ final Optional <Object > fetchedValue = unaryNode .getDataType () != DataType .STRING ? Optional .of (
78+ unaryNode . getValue ()) : ValueUtils .getValueFromMap (unaryNode . getValue ().toString (), data );
8079 return fetchedValue
8180 .map (o -> EvaluatedNode .builder ().value (o ).dataType (ValueUtils .getDataType (o )).build ())
82- .orElseGet (() -> EvaluatedNode .builder ().value (arithmeticLeafNode . getOperand ()).dataType (arithmeticLeafNode .getDataType ()).build ());
81+ .orElseGet (() -> EvaluatedNode .builder ().value (unaryNode . getValue ()).dataType (unaryNode .getDataType ()).build ());
8382 }
8483
8584 private Object evaluateUnaryArithmeticToken (final ArithmeticUnaryNode arithmeticUnaryNode , final Map <String , Object > data ) {
@@ -94,16 +93,26 @@ private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmetic
9493 }
9594
9695 private Object evaluateArithmeticFunctionToken (final ArithmeticFunctionNode arithmeticFunctionNode , final Map <String , Object > data ) {
97- final List <EvaluatedNode > resolvedValues = arithmeticFunctionNode .getItems ()
96+ final List <Object > resolvedValues = arithmeticFunctionNode .getItems ()
9897 .stream ()
99- .map (item -> evaluateArithmeticLeafToken (item , data ))
98+ .map (item -> evaluate (item , data ))
10099 .collect (Collectors .toList ());
101100 final List <EvaluatedNode > flattenedValues = new ArrayList <>();
102101 resolvedValues .forEach (value -> {
103- if (value .getValue () instanceof Collection ) {
104- ((Collection <?>) value .getValue ()).forEach (v -> flattenedValues .add (EvaluatedNode .builder ().value (v ).dataType (ValueUtils .getDataType (v )).build ()));
102+ if (value instanceof EvaluatedNode ) {
103+ final EvaluatedNode node = (EvaluatedNode ) value ;
104+ if (node .getValue () instanceof Collection ) {
105+ ((Collection <?>) node .getValue ()).forEach (
106+ v -> flattenedValues .add (EvaluatedNode .builder ().value (v ).dataType (ValueUtils .getDataType (v )).build ()));
107+ } else {
108+ flattenedValues .add (node );
109+ }
110+ }
111+ if (value instanceof Collection ) {
112+ ((Collection <?>) value ).forEach (
113+ v -> flattenedValues .add (EvaluatedNode .builder ().value (v ).dataType (ValueUtils .getDataType (v )).build ()));
105114 } else {
106- flattenedValues .add (value );
115+ flattenedValues .add (EvaluatedNode . builder (). value ( value ). dataType ( ValueUtils . getDataType ( value )). build () );
107116 }
108117 });
109118 return functionEvaluatorService .evaluateArithmeticFunction (arithmeticFunctionNode .getFunctionType (), flattenedValues );
@@ -115,8 +124,8 @@ private Object evaluateArithmeticToken(final ArithmeticNode arithmeticNode, fina
115124 if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode ) {
116125 final EvaluatedNode leftPair = (EvaluatedNode ) leftValue ;
117126 final EvaluatedNode rightPair = (EvaluatedNode ) rightValue ;
118- return operatorService .evaluateArithmeticOperator (leftPair .getValue (), leftPair .getDataType (), rightPair .getValue (), rightPair . getDataType (),
119- arithmeticNode .getOperator (), ContainerDataType .PRIMITIVE );
127+ return operatorService .evaluateArithmeticOperator (leftPair .getValue (), leftPair .getDataType (), rightPair .getValue (),
128+ rightPair . getDataType (), arithmeticNode .getOperator (), ContainerDataType .PRIMITIVE );
120129 } else if (leftValue instanceof EvaluatedNode ) {
121130 final EvaluatedNode leftPair = (EvaluatedNode ) leftValue ;
122131 final DataType rightDataType = ValueUtils .getDataType (rightValue );
0 commit comments