工具类
import java.math.BigDecimal;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.mvel2.CompileException;
import org.mvel2.MVEL;
import org.mvel2.PropertyAccessException;
/**
* 计算工具
*
* @author valsong
* @date Jul 24, 2017
*
*/
public class CalculateUtils {
/**
* 根据传入的公式和参数进行计算
*
* @param formula
* @param variables
* @return
*/
public static BigDecimal calculate(String formula, Map variables) {
if (SimpleStringUtils.isBlank(formula)) {
throw new CalculateException("MVEL formula can't be null! formula : " + formula); // 公式不能为空
}
if (variables == null || variables.size() == 0) {
throw new CalculateException("MVEL variables can't be null! variables : " + String.valueOf(variables)); // 参数不能为空
}
try {
// 将公式中的变量全部转化为BigDecimal类型
variables.entrySet().stream().filter(e -> e != null && e.getKey() != null && e.getValue() != null)
.map(CalculateUtils::convert).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
} catch (NumberFormatException e) {
throw new CalculateException(
"MVEL can't