44import java .util .Collection ;
55import java .util .List ;
66import java .util .Map ;
7- import java .util .Optional ;
87import java .util .stream .Collectors ;
98import com .github .sidhant92 .boolparser .constant .ContainerDataType ;
109import com .github .sidhant92 .boolparser .constant .DataType ;
1110import com .github .sidhant92 .boolparser .constant .Operator ;
1211import com .github .sidhant92 .boolparser .domain .EvaluatedNode ;
1312import com .github .sidhant92 .boolparser .domain .UnaryNode ;
1413import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticNode ;
15- import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticUnaryNode ;
1614import com .github .sidhant92 .boolparser .domain .Node ;
1715import com .github .sidhant92 .boolparser .domain .arithmetic .ArithmeticFunctionNode ;
1816import com .github .sidhant92 .boolparser .exception .UnsupportedToken ;
@@ -54,10 +52,6 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
5452 switch (node .getTokenType ()) {
5553 case ARITHMETIC :
5654 return evaluateArithmeticToken ((ArithmeticNode ) node , data );
57- case ARITHMETIC_LEAF :
58- return evaluateUnaryNode ((UnaryNode ) node , data );
59- case ARITHMETIC_UNARY :
60- return evaluateUnaryArithmeticToken ((ArithmeticUnaryNode ) node , data );
6155 case ARITHMETIC_FUNCTION :
6256 return evaluateArithmeticFunctionToken ((ArithmeticFunctionNode ) node , data );
6357 case UNARY :
@@ -73,25 +67,6 @@ private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, O
7367 .orElse (unaryNode .getValue ()) : unaryNode .getValue ();
7468 }
7569
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 );
79- return fetchedValue
80- .map (o -> EvaluatedNode .builder ().value (o ).dataType (ValueUtils .getDataType (o )).build ())
81- .orElseGet (() -> EvaluatedNode .builder ().value (unaryNode .getValue ()).dataType (unaryNode .getDataType ()).build ());
82- }
83-
84- private Object evaluateUnaryArithmeticToken (final ArithmeticUnaryNode arithmeticUnaryNode , final Map <String , Object > data ) {
85- final Object resolvedValue = evaluateToken (arithmeticUnaryNode .getOperand (), data );
86- if (resolvedValue instanceof EvaluatedNode ) {
87- final EvaluatedNode evaluatedNode = (EvaluatedNode ) resolvedValue ;
88- return operatorService .evaluateArithmeticOperator (evaluatedNode .getValue (), evaluatedNode .getDataType (), null , null , Operator .UNARY ,
89- ContainerDataType .PRIMITIVE );
90- }
91- final DataType dataType = ValueUtils .getDataType (resolvedValue );
92- return operatorService .evaluateArithmeticOperator (resolvedValue , dataType , null , null , Operator .UNARY , ContainerDataType .PRIMITIVE );
93- }
94-
9570 private Object evaluateArithmeticFunctionToken (final ArithmeticFunctionNode arithmeticFunctionNode , final Map <String , Object > data ) {
9671 final List <Object > resolvedValues = arithmeticFunctionNode .getItems ()
9772 .stream ()
@@ -120,6 +95,17 @@ private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arit
12095
12196 private Object evaluateArithmeticToken (final ArithmeticNode arithmeticNode , final Map <String , Object > data ) {
12297 final Object leftValue = evaluateToken (arithmeticNode .getLeft (), data );
98+ if (arithmeticNode .getOperator ().equals (Operator .UNARY )) {
99+ if (leftValue instanceof EvaluatedNode ) {
100+ final EvaluatedNode leftPair = (EvaluatedNode ) leftValue ;
101+ return operatorService .evaluateArithmeticOperator (leftPair .getValue (), leftPair .getDataType (), null , null ,
102+ arithmeticNode .getOperator (), ContainerDataType .PRIMITIVE );
103+ } else {
104+ final DataType leftDataType = ValueUtils .getDataType (leftValue );
105+ return operatorService .evaluateArithmeticOperator (leftValue , leftDataType , null , null , arithmeticNode .getOperator (),
106+ ContainerDataType .PRIMITIVE );
107+ }
108+ }
123109 final Object rightValue = evaluateToken (arithmeticNode .getRight (), data );
124110 if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode ) {
125111 final EvaluatedNode leftPair = (EvaluatedNode ) leftValue ;
0 commit comments