本文主要是介绍24.仿简道云公式函数实战-数学函数-COUNTIF,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. COUNTIF函数
2. 函数用法
COUNTIF(value1, value2, …, criteria)
3. 函数示例
统计满足条件的参数个数,value 代表不同的值,criteria 为判断条件。
COUNTIF([1,2,3,4], ">2"),返回结果为2
COUNTIF([1,2,3,4], "!=2"),返回结果为3
COUNTIF([1,2,3,4], ">=1"),返回结果为4
COUNTIF(["a", "b", "c"], "d"),返回结果为0
4. 代码实战
首先我们在function包下创建math包,在math包下创建CountIfFunction类,代码如下:
package com.ql.util.express.self.combat.function.math;import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;import java.util.Arrays;
import java.util.function.Predicate;/*** 类描述: COUNTIF函数** @author admin* @version 1.0.0* @date 2023/11/23 10:40*/
public class CountIfFunction extends Operator {public CountIfFunction(String name) {this.name = name;}@Overridepublic Object executeInner(Object[] objects) throws Exception {// 参数校验if (objects.length == 0) {throw new FormulaException("操作数异常");}Predicate<Object> predicate = null;// 截取条件String criteria = objects[objects.length-1].toString();String op = criteria.replaceAll("[0-9]", "");// 值Object[] values = copyV(objects);if (">".equals(op)) {// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param > p;};} else if ("<".equals(op)) {// >=// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param < p;};} else if ("<=".equals(op)) {// >=// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param <= p;};} else if (">=".equals(op)) {// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param >= p;};} else if ("!=".equals(op)) {// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param != p;};} else if ("==".equals(op)) {// 提取数字值String conditionV = criteria.replaceAll("[^0-9]", "");predicate = object -> {double param = Double.parseDouble(object.toString());double p = Double.parseDouble(conditionV);return param == p;};} else {predicate = object -> object.toString().equals(criteria);}final long count = Arrays.stream(values).filter(predicate).count();return count;}public Object[] copyV(Object[] src) {Object[] rst = new Object[src.length-1];for (int i=0;i<rst.length;i++) {rst[i] = src[i];}return rst;}}
把CountIfFunction类注册到公式函数入口类中,代码如下:
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.*;/*** 类描述: 仿简道云公式函数实战入口类** @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"));// AVERAGE函数this.addFunction("AVERAGE",new AvgFunction("AVERAGE"));// CEILING函数this.addFunction("CEILING",new CeilingFunction("CEILING"));// RADIANS函数this.addFunction("RADIANS",new RadiansFunction("RADIANS"));// COS函数this.addFunction("COS",new CosFunction("COS"));// COT函数this.addFunction("COT",new CotFunction("COT"));// COUNT函数this.addFunction("COUNT",new CountFunction("COUNT"));// COUNTIF函数this.addFunction("COUNTIF",new CountIfFunction("COUNTIF"));}
}
创建测试用例
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 COUNTIF() throws Exception{FormulaRunner formulaRunner = new FormulaRunner(true,true);// 创建上下文DefaultContext<String, Object> context = new DefaultContext<>();String express = "COUNTIF(1,2,3,4,5,'!=2')";Object object = formulaRunner.execute(express, context, null, true, true);System.out.println(object);}}
运行结果
这篇关于24.仿简道云公式函数实战-数学函数-COUNTIF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!