ExpressionUtils { public static Double evaluateToDouble( String expression, Map<String, Object> vars ) { Object result = evaluate( expression, vars ); if ( result == null ) { throw new IllegalStateException( "Result must be not null" ); } if ( !isNumeric( String.valueOf( result ) ) ) { throw new IllegalStateException( "Result must be numeric: " + result + ", " + result.getClass() ); } return Double.valueOf( String.valueOf( result ) ); }  static Object evaluate( String expression, Map<String, Object> vars ); static Double evaluateToDouble( String expression, Map<String, Object> vars ); static boolean isTrue( String expression, Map<String, Object> vars ); static boolean isBoolean( String expression, Map<String, Object> vars ); static boolean isValid( String expression, Map<String, Object> vars ); static boolean isNumeric( String value ); static String asSql( String expression ); static Object evaluate( String expression );  }
@Test public void testEvaluateToDouble() { assertEquals( 3d, ExpressionUtils.evaluateToDouble( "3", null ), DELTA ); assertEquals( 3.45, ExpressionUtils.evaluateToDouble( "3.45", null ), DELTA ); assertEquals( 5d, ExpressionUtils.evaluateToDouble( "2 + 3", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "5 + -3", null ), DELTA ); assertEquals( 1d, ExpressionUtils.evaluateToDouble( "2 + 3 + -4", null ), DELTA ); assertEquals( 15.6, ExpressionUtils.evaluateToDouble( "12.4 + 3.2", null ), DELTA ); assertEquals( 2.0, ExpressionUtils.evaluateToDouble( "2 > 1 ? 2.0 : 1.0", null ), DELTA ); assertEquals( 1.0, ExpressionUtils.evaluateToDouble( "2 > 4 ? 2.0 : 1.0", null ), DELTA ); assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:zing(3)", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:zing(-3) + 2.0", null ), DELTA ); assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:zing(-1) + 4 + d2:zing(-2)", null ), DELTA ); assertEquals( 0d, ExpressionUtils.evaluateToDouble( "d2:oizp(-4)", null ), DELTA ); assertEquals( 1d, ExpressionUtils.evaluateToDouble( "d2:oizp(0)", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:oizp(-4) + d2:oizp(0) + d2:oizp(3.0)", null ), DELTA ); assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:daysBetween('2015-03-01','2015-03-04')", null ), DELTA ); assertEquals( 1d, ExpressionUtils.evaluateToDouble( "d2:oizp(d2:zing(3))", null ), DELTA ); assertEquals( 1d, ExpressionUtils.evaluateToDouble( "d2:zing(d2:oizp(3))", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:zpvc(1,3)", null ), DELTA ); assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:zpvc(1,-1,2,-3,0)", null ), DELTA ); assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:condition('3 > 2',4,3)", null ), DELTA ); assertEquals( 3d, ExpressionUtils.evaluateToDouble( "2 + null + 1", null ), DELTA ); assertEquals( 4d, ExpressionUtils.evaluateToDouble( "null + 4", null ), DELTA ); assertEquals( 5d, ExpressionUtils.evaluateToDouble( "(3 + 2) - null", null ), DELTA ); assertEquals( 9d, ExpressionUtils.evaluateToDouble( "(3 + 2) - null + 4 + null", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:zing(null) + 2", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:oizp(null) + 2", null ), DELTA ); assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:zpvc(1,null,2,-3,0)", null ), DELTA ); assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:zpvc(null,null,2,-3,0)", null ), DELTA ); }
@Test public void testEvaluateToDoubleWithVars() { Map<String, Object> vars = new HashMap<>(); vars.put( "v1", 4d ); vars.put( "v2", -5d ); assertEquals( 7d, ExpressionUtils.evaluateToDouble( "v1 + 3", vars ), DELTA ); assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:zing(v1)", vars ), DELTA ); assertEquals( 0d, ExpressionUtils.evaluateToDouble( "d2:zing(v2)", vars ), DELTA ); assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:zing(v1) + d2:zing(v2)", vars ), DELTA ); }
@Test public void testEvaluateToDoubleZeroPositiveValueCount() { String expression = "d2:zpvc(2,3,-1,0)"; assertEquals( 3d, ExpressionUtils.evaluateToDouble( expression, null ), DELTA ); expression = "(d2:zing(4) + d2:zing(0) + d2:zing(-1)) / d2:zpvc(2,0,-1)"; assertEquals( 2d, ExpressionUtils.evaluateToDouble( expression, null ), DELTA ); expression = "((d2:zing(4) + d2:zing(0) + d2:zing(-1)) / d2:zpvc(2,0,-1) * 0.25) + " + "((d2:zing(8) + d2:zing(0) + d2:zing(-1)) / d2:zpvc(2,0,-1) * 0.75)"; assertEquals( 3.5, ExpressionUtils.evaluateToDouble( expression, null ), DELTA ); }