Condition String Resolver in java API?
java
Solution
As of java 1.6 you can use the `ScriptEngine` and evaluate javascript if this is enough for you and/or if you don't want to introduce another library.
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("javascript");
SimpleBindings bindings = new SimpleBindings();
bindings.put("a", 0);
bindings.put("b", 6);
boolean firstEval = (Boolean) scriptEngine.eval("a != 0 && b > 5", bindings);
System.out.println(firstEval);
bindings.put("a", 2);
bindings.put("b", 6);
boolean secondEval = (Boolean) scriptEngine.eval("a != 0 && b > 5", bindings);
System.out.println(secondEval);
Output
false
true
Problem
I would like to resolve the following condtion string. Because I would like to support dynamic condtion in my project. ``` a != 0 && b > 5 ``` My expected Program is ``` public boolean resolve() { String condition = "a != 0 && b > 5"; Map<String, Object> paramMap = new HashMap<String, Object>; paramMap.put("a", 2); paramMap.put("b", 6); boolean result = ConditionResolver.resolve(condition, paramMap); if(result) { //do something } } ``` Update : I am not trying to resolve the math equation, like below ``` ((a + b) * y) /x ```