public static Object coerceNumber(Object value) { if ( value instanceof Number && !(value instanceof BigDecimal) ) { return getBigDecimalOrNull( value ); } else { return value; } }
private Object normalizeResult(Object result) { // this is to normalize types returned by external functions return result != null && result instanceof Number && !(result instanceof BigDecimal) ? EvalHelper.getBigDecimalOrNull( result.toString() ) : result; }
public static Object math(Object left, Object right, EvaluationContext ctx, BinaryOperator<BigDecimal> op) { BigDecimal l = EvalHelper.getBigDecimalOrNull( left ); BigDecimal r = EvalHelper.getBigDecimalOrNull( right ); if ( l == null || r == null ) { return null; } try { return op.apply( l, r ); } catch ( ArithmeticException e ) { // happens in cases like division by 0 return null; } }
public NumberNode(ParserRuleContext ctx) { super( ctx ); value = EvalHelper.getBigDecimalOrNull( ctx.getText() ); }
public static Object div(Object left, Object right, EvaluationContext ctx) { if ( left == null || right == null ) { return null; } else if ( left instanceof Duration && right instanceof Number ) { return ((Duration)left).dividedBy( ((Number) right).longValue() ); } else if ( left instanceof Number && right instanceof Duration ) { return Duration.ofSeconds( EvalHelper.getBigDecimalOrNull( left ).divide( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ).longValue() ); } else if ( left instanceof Duration && right instanceof Duration ) { return EvalHelper.getBigDecimalOrNull( ((Duration) left).getSeconds() ).divide( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ); } else if ( left instanceof Period && right instanceof Number ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( ((Period)left).toTotalMonths() ).divide( EvalHelper.getBigDecimalOrNull( ((Number) right).longValue() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Number && right instanceof Period ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( left ).divide( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Period && right instanceof Period ) { return EvalHelper.getBigDecimalOrNull( ((Period) left).toTotalMonths() ).divide( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ); } else { return math( left, right, ctx, (l, r) -> l.divide( r, MathContext.DECIMAL128 ) ); } }
public static Object mult(Object left, Object right, EvaluationContext ctx) { if ( left == null || right == null ) { return null; } else if ( left instanceof Duration && right instanceof Number ) { return ((Duration)left).multipliedBy( ((Number) right).longValue() ); } else if ( left instanceof Number && right instanceof Duration ) { return Duration.ofSeconds( EvalHelper.getBigDecimalOrNull( left ).multiply( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ).longValue() ); } else if ( left instanceof Duration && right instanceof Duration ) { return EvalHelper.getBigDecimalOrNull( ((Duration) left).getSeconds() ).multiply( EvalHelper.getBigDecimalOrNull( ((Duration)right).getSeconds() ), MathContext.DECIMAL128 ); } else if ( left instanceof Period && right instanceof Number ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( ((Period)left).toTotalMonths() ).multiply( EvalHelper.getBigDecimalOrNull( ((Number) right).longValue() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Number && right instanceof Period ) { return Period.ofMonths( EvalHelper.getBigDecimalOrNull( left ).multiply( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ).intValue() ); } else if ( left instanceof Period && right instanceof Period ) { return EvalHelper.getBigDecimalOrNull( ((Period) left).toTotalMonths() ).multiply( EvalHelper.getBigDecimalOrNull( ((Period)right).toTotalMonths() ), MathContext.DECIMAL128 ); } else { return math( left, right, ctx, (l, r) -> l.multiply( r, MathContext.DECIMAL128 ) ); } }
@Override public Object evaluate(EvaluationContext ctx) { if (expression == null) return null; BigDecimal result = EvalHelper.getBigDecimalOrNull( expression.evaluate( ctx ) ); if ( result == null ) { ctx.notifyEvt( astEvent(Severity.WARN, Msg.createMessage(Msg.NEGATING_A_NULL))); return null; } else if ( Sign.NEGATIVE == sign ) { return BigDecimal.valueOf( -1 ).multiply( result ); } else { return result; } }
public FEELFnResult<BigDecimal> invoke(@ParameterName("string") String string) { if ( string == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "string", "cannot be null")); } else { return FEELFnResult.ofResult( EvalHelper.getBigDecimalOrNull( string.length() ) ); } } }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list) { if ( list == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the list cannot be null")); } BigDecimal product = list.isEmpty() ? BigDecimal.ZERO : BigDecimal.ONE; for ( Object element : list ) { if ( element instanceof BigDecimal ) { product = product.multiply( (BigDecimal) element ); } else if ( element instanceof Number ) { product = product.multiply( EvalHelper.getBigDecimalOrNull( element ) ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "an element in the list is not suitable for the product")); } } return FEELFnResult.ofResult( product ); }
@Test public void testMethodGetBigDecimalOrNull() { assertThat( getBigDecimalOrNull((short) 1), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull((byte) 1), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull(1), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull(1L), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull(1f), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull(1.1f), is(BigDecimal.valueOf(1.1)) ); assertThat( getBigDecimalOrNull(1d), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull(1.1d), is(BigDecimal.valueOf(1.1)) ); assertThat( getBigDecimalOrNull("1"), is(BigDecimal.ONE) ); assertThat( getBigDecimalOrNull("1.1"), is(BigDecimal.valueOf(1.1)) ); assertThat( getBigDecimalOrNull("1.1000000"), is(BigDecimal.valueOf(1.1).setScale(7, BigDecimal.ROUND_HALF_EVEN)) ); assertThat( getBigDecimalOrNull(Double.POSITIVE_INFINITY), is(nullValue()) ); assertThat( getBigDecimalOrNull(Double.NEGATIVE_INFINITY), is(nullValue()) ); assertThat( getBigDecimalOrNull(Double.NaN), is(nullValue()) ); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName( "number" ) BigDecimal number) { if ( number == null ) { return FEELFnResult.ofError( new InvalidParametersEvent( FEELEvent.Severity.ERROR, "number", "cannot be null" ) ); } if ( number.signum() < 0 ) { return FEELFnResult.ofError( new InvalidParametersEvent( FEELEvent.Severity.ERROR, "number", "is negative" ) ); } return FEELFnResult.ofResult( sqrt( EvalHelper.getBigDecimalOrNull( number ) ) ); }
@Test public void testOthers() { assertThat( evaluate("ceiling( 1.01 )") , is( getBigDecimalOrNull( 2d ) ) ); assertThat( evaluate("ceiling( x )", var("x", 1.01d )) , is( getBigDecimalOrNull( 2d ) ) ); assertThat( ((Map) evaluate("{ myf : function( v1, v2 ) ceiling(v1), invoked: myf(v2: false, v1: x) }", var("x", 1.01d) )).get("invoked"), is( getBigDecimalOrNull( 2d ) ) ); assertThat( ((Map) evaluate("{ myf : function( v1, v2 ) v1, invoked: myf(v2: false, v1: x) }", var("x", 1.01d) )).get("invoked"), is( getBigDecimalOrNull( 1.01d ) ) ); assertThat( evaluate(" x.y ", var("x", new HashMap<String, Object>(){{ put("y", 1.01d); }} )), is( getBigDecimalOrNull( 1.01d ) ) ); assertThat( evaluate("ceiling( x.y )", var("x", new HashMap<String, Object>(){{ put("y", 1.01d); }} )), is( getBigDecimalOrNull( 2d ) ) ); }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Object sole) { if ( sole == null ) { return FEELFnResult.ofResult( null ); } else if( EvalHelper.getBigDecimalOrNull( sole ) == null ) { return FEELFnResult.ofError( new InvalidParametersEvent( FEELEvent.Severity.ERROR, "list", "the value can not be converted to a number" ) ); } return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "sample standard deviation of a single sample is undefined")); }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list) { if ( list == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the list cannot be null")); } BigDecimal sum = BigDecimal.ZERO; for ( Object element : list ) { if ( element instanceof BigDecimal ) { sum = sum.add( (BigDecimal) element ); } else if ( element instanceof Number ) { BigDecimal value = EvalHelper.getBigDecimalOrNull( element ); if (value == null) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "an element in the list is not suitable for the sum")); } else { sum = sum.add( value ); } } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "an element in the list is not a number")); } } return FEELFnResult.ofResult( sum ); }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) { if ( single == null ) { return FEELFnResult.ofResult( null ); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number")); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) Number single) { if ( single == null ) { return FEELFnResult.ofResult(null); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list is not a number")); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Object sole) { if ( sole == null ) { // Arrays.asList does not accept null as parameter return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "the single value list cannot be null")); } else if (EvalHelper.getBigDecimalOrNull(sole) == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "the value can not be converted to a number")); } return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "list", "sample standard deviation of a single sample is undefined")); }
public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) Number single) { if ( single == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "single", "the single value list cannot be null")); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list is not a number")); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) { if ( single == null ) { // Arrays.asList does not accept null as parameter return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the single value list cannot be null")); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number")); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single) { if ( single == null ) { // Arrays.asList does not accept null as parameter return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the single value list cannot be null")); } if( single instanceof BigDecimal ) { return FEELFnResult.ofResult((BigDecimal) single ); } BigDecimal result = EvalHelper.getBigDecimalOrNull( single ); if ( result != null ) { return FEELFnResult.ofResult( result ); } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "single element in list not a number")); } }