java中有ScriptEngineManager内可以执行部分特殊的js操作,一下是我遇到的场景:传一个值判断是否符合一个规则,这个规则是一个多个逻辑运算的集合,java中不能对字符串格式的逻辑组合进行计算,js中有这种计算,不说了直接上代码:
public static Boolean countThreshold(String elValue,String el){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("x",elValue);
boolean eval = false;
try {
eval = (boolean) engine.eval(el);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return eval;
}
public static void main(String[] args) {
System.out.println(StringUtils.countThreshold("0.9","(x<0.8478241755535717)&&(x>0.17470429385819297)"));
}