commit | author | age
|
a18bfa
|
1 |
package cn.autoform.web.formula.tool; |
Z |
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
import com.googlecode.aviator.AviatorEvaluator; |
|
6 |
|
|
7 |
import cn.autoform.web.formula.tool.function.Avg; |
|
8 |
import cn.autoform.web.formula.tool.function.Sum; |
|
9 |
|
|
10 |
public class AviatorTool { |
|
11 |
|
|
12 |
static{ |
|
13 |
// 子表单 聚合函数 总和 |
|
14 |
AviatorEvaluator.addFunction(new Sum()); |
|
15 |
// 子表单 聚合函数 平均值 |
|
16 |
AviatorEvaluator.addFunction(new Avg()); |
|
17 |
} |
|
18 |
|
|
19 |
public static <T> T execute(String expression, Map<String, Object> env, Class<T> clazz){ |
|
20 |
return execute(expression, env, clazz, null); |
|
21 |
} |
|
22 |
|
|
23 |
@SuppressWarnings("unchecked") |
|
24 |
public static <T> T execute(String expression, Map<String, Object> env, Class<T> clazz, CallBack<T> call){ |
|
25 |
try{ |
|
26 |
return (T) AviatorEvaluator.compile(expression).execute(env); |
|
27 |
}catch (Exception e) { |
|
28 |
if(call != null) |
|
29 |
return call.error(e); |
|
30 |
return null; |
|
31 |
} |
|
32 |
} |
|
33 |
|
|
34 |
@FunctionalInterface |
|
35 |
public static interface CallBack<T>{ |
|
36 |
|
|
37 |
T error(Exception e); |
|
38 |
|
|
39 |
} |
|
40 |
|
|
41 |
} |