package cn.autoform.web.formula.tool;
|
|
import java.util.Map;
|
|
import com.googlecode.aviator.AviatorEvaluator;
|
|
import cn.autoform.web.formula.tool.function.Avg;
|
import cn.autoform.web.formula.tool.function.Sum;
|
|
public class AviatorTool {
|
|
static{
|
// 子表单 聚合函数 总和
|
AviatorEvaluator.addFunction(new Sum());
|
// 子表单 聚合函数 平均值
|
AviatorEvaluator.addFunction(new Avg());
|
}
|
|
public static <T> T execute(String expression, Map<String, Object> env, Class<T> clazz){
|
return execute(expression, env, clazz, null);
|
}
|
|
@SuppressWarnings("unchecked")
|
public static <T> T execute(String expression, Map<String, Object> env, Class<T> clazz, CallBack<T> call){
|
try{
|
return (T) AviatorEvaluator.compile(expression).execute(env);
|
}catch (Exception e) {
|
if(call != null)
|
return call.error(e);
|
return null;
|
}
|
}
|
|
@FunctionalInterface
|
public static interface CallBack<T>{
|
|
T error(Exception e);
|
|
}
|
|
}
|