四则运算

public class ArithmeticUtil {
    private static GroovyShell groovyShell = new GroovyShell();
    private static Map<String, Script> scriptCache = new ConcurrentHashMap<>();

    /**
     *  四则运算
     */
    public static BigDecimal getFormulaResult(Map<String, BigDecimal> map, String formula){
        if (StringUtils.isBlank(formula) || map == null) {
            return BigDecimal.ONE;
        }
        
        log.info("数据计算公式:{},参数值:{}", formula , JSON.toJSON(map));
        Object resultData = runGroovyShell(map, formula);
        BigDecimal result = new BigDecimal(resultData.toString());
        log.info("计算结果:" + result);
        return result;
    }

    public static Object runGroovyShell(Map<String, BigDecimal> values, String formula){
        if (values == null || StringUtils.isBlank(formula)) {
            return BigDecimal.ONE;
        }
        if (formula.length() == 1) {
            return values.getOrDefault(formula,BigDecimal.ONE);
        }
        Object scriptObject = null;
        try {
            Binding binding = new Binding();
            for (String key : values.keySet()) {
                binding.setVariable(key, values.get(key));
            }
            Script shell = scriptCache.get(formula);
            if (shell == null) {
                shell = groovyShell.parse(formula,formula);
                scriptCache.put(formula, shell);
            }
            scriptObject = InvokerHelper.createScript(shell.getClass(), binding).run();
        } catch (Throwable t) {
            log.error("groovy script eval error. script: " + formula, t);
        }

        return scriptObject;
    }


    public static void main(String[] args) throws Exception {
        int end=10,start=1;
        for (int i=0;i<10;i++) {
            Thread t = new Thread(() -> {
                String formula = "A";
                Map<String, BigDecimal> map = new HashMap();
                for (int j=0;j<3;j++){
                    map.put("A", new BigDecimal((int)(Math.random() * (end-start+1) + start)));
                    map.put("B", new BigDecimal((int)(Math.random() * (end-start+1) + start)));
                    map.put("C", new BigDecimal((int)(Math.random() * (end-start+1) + start)));
                    map.put("D", new BigDecimal((int)(Math.random() * (end-start+1) + start)));
                    map.put("E", new BigDecimal((int)(Math.random() * (end-start+1) + start)));
                    BigDecimal result = getFormulaResult(map, formula);
                    System.out.println("线程"+Thread.currentThread().getName()+ "--" + JSON.toJSONString(map) + "--"+ result);
                }
            });
            t.start();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值