You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package com.fanruan.api.cal;
|
|
|
|
|
|
|
|
import com.fanruan.api.err.KitError;
|
|
|
|
import com.fr.base.BaseFormula;
|
|
|
|
import com.fr.script.Calculator;
|
|
|
|
import com.fr.stable.FormulaProvider;
|
|
|
|
import com.fr.stable.UtilEvalError;
|
|
|
|
import com.fr.stable.script.CalculatorProvider;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author richie
|
|
|
|
* @version 10.0
|
|
|
|
* Created by richie on 2019-08-15
|
|
|
|
* 公式计算相关的工具类
|
|
|
|
*/
|
|
|
|
public class FormulaKit {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 计算公式的值,会新建一个算子对象来计算该公式
|
|
|
|
* @param formula 公式内容
|
|
|
|
* @return 公式计算后的结果值
|
|
|
|
* @throws KitError 如果计算过程中出现错误,则抛出此异常
|
|
|
|
*/
|
|
|
|
public static @Nullable Object eval(String formula) throws KitError {
|
|
|
|
return eval(Calculator.createCalculator(), formula);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 计算公式的值
|
|
|
|
* @param calculator 自定义算子
|
|
|
|
* @param formula 公式内容
|
|
|
|
* @return 公式计算后的结果值
|
|
|
|
* @throws KitError 如果计算过程中出现错误,则抛出此异常
|
|
|
|
*/
|
|
|
|
public static @Nullable Object eval(CalculatorProvider calculator, String formula) throws KitError {
|
|
|
|
try {
|
|
|
|
return BaseFormula.createFormulaBuilder().build(formula).eval(calculator);
|
|
|
|
} catch (UtilEvalError u) {
|
|
|
|
throw new KitError(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生成公式对象
|
|
|
|
* @param content 公式的内容
|
|
|
|
* @return 公式对象
|
|
|
|
*/
|
|
|
|
public static @NotNull FormulaProvider newFormula(Object content) {
|
|
|
|
return BaseFormula.createFormulaBuilder().build(content);
|
|
|
|
}
|
|
|
|
}
|