/** * Evaluates the given expression. The given variables will be substituted * in the expression. * * @param expression the expression. * @param vars the variables, can be null. * @return the result of the evaluation. */ public static Object evaluate( String expression, Map<String, Object> vars ) { return evaluate( expression, vars, false ); }
public static Object evaluate( String expression ) { return evaluate( expression, null ); } }
/** * Evaluates the given expression to true or false. The given variables will * be substituted in the expression. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isTrue( String expression, Map<String, Object> vars ) { Object result = evaluate( expression, vars ); return ( result != null && result instanceof Boolean ) ? (Boolean) result : false; }
/** * Indicates whether the given expression is valid and evaluates to true or * false. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isBoolean( String expression, Map<String, Object> vars ) { try { Object result = evaluate( expression, vars ); return ( result instanceof Boolean ); } catch ( JexlException | NumberFormatException ex ) { return false; } }
/** * Indicates whether the given expression is valid, i.e. can be successfully * evaluated. * * @param expression the expression. * @param vars the variables, can be null. * @return true or false. */ public static boolean isValid( String expression, Map<String, Object> vars ) { try { Object result = evaluate( expression, vars, true ); return result != null; } catch ( JexlException | NumberFormatException ex ) { if ( ex.getMessage().contains( "divide error" ) ) { return true; //TODO division by zero masking } return false; } }
/** * Evaluates the given expression. The given variables will be substituted * in the expression. Converts the result of the evaluation to a Double. * Throws an IllegalStateException if the result could not be converted to * a Double * * @param expression the expression. * @param vars the variables, can be null. * @return the result of the evaluation. */ 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 ) ); }
/** * Return string value of boolean output. False will be returned in case * of wrongly created expression * * @param expression to be evaluated. * @return string value of boolean true/false. */ @Nonnull @Override public String evaluate( @Nonnull String expression ) { String result = ""; try { result = ExpressionUtils.evaluate( expression ).toString(); } catch ( JexlException je ) { result = "false"; log.debug( DebugUtils.getStackTrace( je.getCause() ) ); } return result; } }