1. ABS函數
ABS 函數可用于返回數字的絕對值
2. 函數用法
ABS(number)
3. 函數示例
如,ABS(-12)
和ABS(12)
的返回結果均為 12。
4. 代碼實戰
首先我們在function包下創建math包,在math包下創建AbsFunction類,代碼如下:
package com.ql.util.express.self.combat.function.math;import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;/*** 類描述: ABS函數** @author admin* @version 1.0.0* @date 2023/11/23 8:57*/
public class AbsFunction extends Operator {public AbsFunction(String name) {this.name = name;}@Overridepublic Object executeInner(Object[] list) throws Exception {if (list.length == 0) {throw new FormulaException("操作數異常");}// 取出來數據Object result = list[0];if (result instanceof Integer) {int val = ((Integer) result).intValue();// 調用Math函數提供的取絕對值的方法result = Math.abs(val);} else if (result instanceof Double) {double val = ((Double) result).doubleValue();result = Math.abs(val);} else if (result instanceof Float) {double val = ((Float) result).floatValue();result = Math.abs(val);} else if (result instanceof Long) {long val = ((Long) result).longValue();result = Math.abs(val);} else if (result instanceof Short) {short val = ((Short) result).shortValue();result = Math.abs(val);} else {throw new FormulaException("參數數據類型異常");}return result;}
}
把AbsFunction類注冊到公式函數入口類中,代碼如下:
package com.ql.util.express.self.combat.ext;import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressResourceLoader;
import com.ql.util.express.parse.NodeTypeManager;
import com.ql.util.express.self.combat.function.logic.*;
import com.ql.util.express.self.combat.function.math.AbsFunction;/*** 類描述: 仿簡道云公式函數實戰入口類** @author admin* @version 1.0.0* @date 2023/11/21 15:29*/
public class FormulaRunner extends ExpressRunner {public FormulaRunner() {super();}public FormulaRunner(boolean isPrecise, boolean isTrace) {super(isPrecise,isTrace);}public FormulaRunner(boolean isPrecise, boolean isStrace, NodeTypeManager nodeTypeManager) {super(isPrecise,isStrace,nodeTypeManager);}public FormulaRunner(boolean isPrecise, boolean isTrace, IExpressResourceLoader iExpressResourceLoader, NodeTypeManager nodeTypeManager) {super(isPrecise,isTrace,iExpressResourceLoader,nodeTypeManager);}@Overridepublic void addSystemFunctions() {// ExpressRunner 的內部系統函數super.addSystemFunctions();// 擴展公式函數this.customFunction();}/**** 自定義公式函數*/public void customFunction() {// 邏輯公式函數this.addLogicFunction();// 數學公式函數this.addMathFunction();}public void addLogicFunction() {// AND函數this.addFunction("AND",new AndFunction("AND"));// IF函數this.addFunction("IF",new IfFunction("IF"));// IFS函數this.addFunction("IFS",new IfsFunction("IFS"));// XOR函數this.addFunction("XOR",new XorFunction("XOR"));// TRUE函數this.addFunction("TRUE",new TrueFunction("TRUE"));// FALSE函數this.addFunction("FALSE",new FalseFunction("FALSE"));// NOT函數this.addFunction("NOT",new NotFunction("NOT"));// OR函數this.addFunction("OR",new OrFunction("OR"));}public void addMathFunction() {// ABS函數this.addFunction("ABS",new AbsFunction("ABS"));}
}
創建測試用例
package com.ql.util.express.self.combat;import com.ql.util.express.DefaultContext;
import com.ql.util.express.self.combat.ext.FormulaRunner;
import org.junit.Test;/*** 類描述: 實戰測試類** @author admin* @version 1.0.0* @date 2023/11/21 15:45*/
public class CombatTest {@Testpublic void ABS() throws Exception{FormulaRunner formulaRunner = new FormulaRunner(true,true);// 創建上下文DefaultContext<String, Object> context = new DefaultContext<>();String express = "ABS(-4294967294)";context.put("a",-11.1111);context.put("b",3.0);Object object = formulaRunner.execute(express, context, null, true, true);System.out.println(object);}}
運行結果