日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java mvel_MVEL实现java直接根据公式计算结果

發布時間:2024/9/27 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mvel_MVEL实现java直接根据公式计算结果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工具類

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 convert to BigDecimal, please check the variables : " + String.valueOf(variables) + "!",

e);

} catch (Exception e) {

throw e;

}

BigDecimal result = null;

try {

result = (BigDecimal) MVEL.eval(formula, variables);

} catch (PropertyAccessException pae) {

throw new CalculateException(

"MVEL please check the formula :" + formula + " & variables : " + String.valueOf(variables) + "!",

pae);

} catch (CompileException ce) {

throw new CalculateException("MVEL calculate error! ", ce);

} catch (Exception e) {

throw e;

}

return result;

}

/**

* 將參數轉化為Bigdecimal類型

*

* @param entry

* @return

*/

@SuppressWarnings("unchecked")

public static Entry convert(Entry entry) {

if (entry != null) {

BigDecimal value = null;

if (entry.getValue() instanceof BigDecimal) {

value = (BigDecimal) entry.getValue();

} else {

value = NumberUtils.getNum(NumberUtils.getNum(entry.getValue()));

}

entry.setValue((T) value);

}

return entry;

}

}

異常類

/**

* 計算異常

*

* @author Val Song

* @date Jul 28, 2017

*

*/

public class CalculateException extends RuntimeException {

private static final long serialVersionUID = 5162710183389028792L;

/**

* Constructs a {@code CalculateException} with no detail message.

*/

public CalculateException() {

super();

}

/**

* Constructs a {@code CalculateException} with the specified detail

* message.

*

* @param message

* the detail message.

*/

public CalculateException(String message) {

super(message);

}

/**

* Constructs a {@code CalculateException} with the specified detail

* message & cause

*

* @param message

* @param cause

*/

public CalculateException(String message, Throwable cause) {

super(message, cause);

}

}

字符串工具類

/**

* 字符串工具

*

* @author valsong

*

*/

public class SimpleStringUtils {

/**

* 判斷字符串不為空

*

* @param str

* @return

*/

public static boolean isNotBlank(String str) {

return !isBlank(str);

}

/**

* 判斷字符串為空

*

* @param str

* @return

*/

public static boolean isBlank(String str) {

if (str == null || "".equals(str.trim())) {

return true;

}

return false;

}

/**

* 去首尾空格

*

* @param s

* @return

*/

public static String trim(T s) {

if (s instanceof String) {

return s == null ? null : ((String) s).trim();

} else {

return s == null ? null : String.valueOf(s).trim();

}

}

/**

* 下劃線命名轉駝峰式

*

* @param str

* @return

*/

public static String toCamel(String str) {

if (SimpleStringUtils.isBlank(str)) {

return str;

}

StringBuffer buffer = new StringBuffer();

str = str.toLowerCase().trim();

char[] charArray = str.toCharArray();

if (charArray != null) {

for (int i = 0; i < charArray.length; i++) {

if ('_' == charArray[i]) {

i = i + 1;

buffer.append(Character.toUpperCase(charArray[i]));

} else {

buffer.append(charArray[i]);

}

}

}

return buffer.toString();

}

/**

* 駝峰轉下劃線

*

* @param str

* @return

*/

public static String toUnderline(String str) {

if (SimpleStringUtils.isBlank(str)) {

return str;

}

StringBuffer buffer = new StringBuffer();

str = str.trim();

char[] charArray = str.toCharArray();

if (charArray != null) {

for (int i = 0; i < charArray.length; i++) {

if (Character.isUpperCase(charArray[i])) {

buffer.append("_");

buffer.append(Character.toLowerCase(charArray[i]));

} else {

buffer.append(charArray[i]);

}

}

}

return buffer.toString();

}

}

測試類簡單粗暴String Double Integer Bigdecimal 都能支持

public class CalculateUtilsTest {

/**

* 20 * 100 - ( 30 -20 ) + 20 ^ 3 + 20/2

*/

@Test

public void calculateTest4() {

String formula = "A * B - (C -D) + E.pow(F) + G / H";

Map variables = new HashMap<>();

variables.put("A", "20");

variables.put("B", 100L);

variables.put("C", 30.0D);

variables.put("D", 20);

variables.put("E", new BigDecimal("20"));

variables.put("F", "3");

variables.put("G", "20");

variables.put("H", "2");

BigDecimal result = CalculateUtils.calculate(formula, variables);

Assert.assertTrue(new BigDecimal("10000.0").compareTo(result) == 0);

}

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java mvel_MVEL实现java直接根据公式计算结果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。