条件字符串解析器在Java API中? [英] Condition String Resolver in java API?
本文介绍了条件字符串解析器在Java API中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解决以下条件字符串。因为我想在我的项目中支持动态条件。
I would like to resolve the following condtion string. Because I would like to support dynamic condtion in my project.
a != 0 && b > 5
我的预期计划是
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
}
}
更新:
我不是要解决数学公式,如下所示
I am not trying to resolve the math equation, like below
((a + b) * y) /x
推荐答案
从java 1.6开始,您可以使用 ScriptEngine
并评估 javascript 对你来说已经足够了和/或如果你不想引入另一个库。
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);
输出
false
true
这篇关于条件字符串解析器在Java API中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文