11package com .github .sidhant92 .boolparser .function .arithmetic ;
22
3+ import java .math .BigDecimal ;
34import java .util .Arrays ;
45import java .util .Collections ;
6+ import java .util .Comparator ;
57import java .util .List ;
6- import org .apache .commons .lang3 .tuple .Pair ;
8+ import java .util .Optional ;
9+ import java .util .stream .Collectors ;
710import com .github .sidhant92 .boolparser .constant .ContainerDataType ;
811import com .github .sidhant92 .boolparser .constant .DataType ;
912import com .github .sidhant92 .boolparser .constant .FunctionType ;
13+ import com .github .sidhant92 .boolparser .datatype .DataTypeFactory ;
14+ import com .github .sidhant92 .boolparser .datatype .DecimalDataType ;
15+ import com .github .sidhant92 .boolparser .datatype .IntegerDataType ;
16+ import com .github .sidhant92 .boolparser .datatype .LongDataType ;
1017import com .github .sidhant92 .boolparser .domain .EvaluatedNode ;
1118import com .github .sidhant92 .boolparser .util .ValueUtils ;
1219
@@ -19,16 +26,25 @@ public class MaxFunction extends AbstractFunction {
1926 public Object evaluate (final List <EvaluatedNode > items ) {
2027 if (items
2128 .stream ().anyMatch (a -> a .getDataType ().equals (DataType .DECIMAL ))) {
22- return ValueUtils .caseDouble (items
23- .stream ().mapToDouble (a -> Double .parseDouble (a .getValue ().toString ())).max ().getAsDouble ());
29+ final DecimalDataType decimalDataType = (DecimalDataType ) DataTypeFactory .getDataType (DataType .DECIMAL );
30+ final List <BigDecimal > itemsConverted = items
31+ .stream ()
32+ .map (item -> decimalDataType .getValue (item .getValue ()))
33+ .map (Optional ::get )
34+ .collect (Collectors .toList ());
35+ final BigDecimal max = itemsConverted
36+ .stream ().max (Comparator .naturalOrder ()).get ();
37+ return ValueUtils .castDecimal (max );
2438 }
2539 if (items
2640 .stream ().anyMatch (a -> a .getDataType ().equals (DataType .LONG ))) {
27- return ValueUtils .caseDouble (items
28- .stream ().mapToLong (a -> Long .parseLong (a .getValue ().toString ())).max ().getAsLong ());
41+ final LongDataType longDataType = (LongDataType ) DataTypeFactory .getDataType (DataType .LONG );
42+ return ValueUtils .castDecimal (items
43+ .stream ().mapToLong (a -> longDataType .getValue (a .getValue ()).get ()).max ().getAsLong ());
2944 }
30- return ValueUtils .caseDouble (items
31- .stream ().mapToInt (a -> Integer .parseInt (a .getValue ().toString ())).max ().getAsInt ());
45+ final IntegerDataType integerDataType = (IntegerDataType ) DataTypeFactory .getDataType (DataType .INTEGER );
46+ return ValueUtils .castDecimal (items
47+ .stream ().mapToInt (a -> integerDataType .getValue (a .getValue ()).get ()).max ().getAsInt ());
3248 }
3349
3450 @ Override
0 commit comments