51.仿简道云公式函数实战-文本函数-JOIN

1. JOIN函数

JOIN 函数可通过连接符将数组的值连成文本。

2. 函数用法

JOIN(数组,"连接符")

3. 函数示例

如需将复选框中勾选的选项通过"-"组合在一起,则可设置公式为JOIN(复选框组,"-")

4. 代码实战

首先我们在function包下创建text包,在text包下创建JoinFunction类,代码如下:

java 复制代码
package com.ql.util.express.self.combat.function.text;

import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;

import java.lang.reflect.Array;
import java.util.List;

/**
 * 类描述: JOIN函数
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/24 14:01
 */
public class JoinFunction extends Operator {

    public JoinFunction(String name) {
        this.name = name;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {

        if (list.length == 0) {
            throw new FormulaException("操作数异常");
        }

        Object rst = null;
        Object arr = list[0];

        if ((arr.getClass().isArray() || arr instanceof List)) {
            StringBuilder sb = new StringBuilder();
            if (arr.getClass().isArray()) {
                int len = Array.getLength(arr);
                Object opt = list[list.length-1];

                for (int i=0;i<len;i++) {
                    sb.append(Array.get(arr, i)).append(opt);
                }
                rst = sb.substring(0,sb.length()-1);
            } else if (arr instanceof List){
                List<Object> objs = (List<Object>) arr;
                Object opt = list[list.length-1];
                for (int i=0;i<objs.size();i++) {
                    sb.append(objs.get(i)).append(opt);
                }
                rst = sb.substring(0,sb.length()-1);
            } else {
                throw new FormulaException("操作数类型异常");
            }
        } else {
            StringBuilder sb = new StringBuilder();
            // 操作符号
            Object opt = list[list.length-1];
            // 循环
            for (int i=0;i<list.length-1;i++) {
                sb.append(list[i]).append(opt);
            }

            rst = sb.substring(0,sb.length()-1);
        }
        return rst;
    }
}

把JoinFunction类注册到公式函数入口类中,代码如下:

java 复制代码
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.*;
import com.ql.util.express.self.combat.function.text.*;

/**
 * 类描述: 仿简道云公式函数实战入口类
 *
 * @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);
    }

    @Override
    public void addSystemFunctions() {
        // ExpressRunner 的内部系统函数
        super.addSystemFunctions();
        // 扩展公式函数
        this.customFunction();
    }
    /***
     * 自定义公式函数
     */
    public void customFunction() {

        // 逻辑公式函数
        this.addLogicFunction();

        // 数学公式函数
        this.addMathFunction();

        // 文本函数
        this.addTextFunction();
    }

    public void addTextFunction() {
        // CHAR函数
        this.addFunction("CHAR",new CharFunction("CHAR"));

        // CONCATENATE函数
        this.addFunction("CONCATENATE",new ConcatenateFunction("CONCATENATE"));

        // EXACT函数
        this.addFunction("EXACT",new ExactFunction("EXACT"));

        // IP函数
        this.addFunction("IP",new IpFunction("IP"));

        // ISEMPTY函数
        this.addFunction("ISEMPTY",new IsEmptyFunction("ISEMPTY"));

        // JOIN函数
        this.addFunction("JOIN",new JoinFunction("JOIN"));
    }

    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"));

        // FIXED函数
        this.addFunction("FIXED",new FixedFunction("FIXED"));

        // FLOOR函数
        this.addFunction("FLOOR",new FloorFunction("FLOOR"));

        // INT函数
        this.addFunction("INT",new IntFunction("INT"));

        // LARGE函数
        this.addFunction("LARGE",new LargeFunction("LARGE"));

        // LOG函数
        this.addFunction("LOG",new LogFunction("LOG"));

        // MAX函数
        this.addFunction("MAX",new MaxFunction("MAX"));

        // MIN函数
        this.addFunction("MIN",new MinFunction("MIN"));

        // MOD函数
        this.addFunction("MOD",new ModFunction("MOD"));

        // POWER函数
        this.addFunction("POWER",new PowerFunction("POWER"));

        // PRODUCT函数
        this.addFunction("PRODUCT",new ProductFunction("PRODUCT"));

        // RAND函数
        this.addFunction("RAND",new RandFunction("RAND"));

        // ROUND函数
        this.addFunction("ROUND",new RoundFunction("ROUND"));

        // SIN函数
        this.addFunction("SIN",new SinFunction("SIN"));

        // SMALL函数
        this.addFunction("SMALL",new SmallFunction("SMALL"));

        // SQRT函数
        this.addFunction("SQRT",new SqrtFunction("SQRT"));

        // SUM函数
        this.addFunction("SUM",new SumFunction("SUM"));

        // SUMIF函数
        this.addFunction("SUMIF",new SumIfFunction("SUMIF"));

        // SUMIFS函数
        this.addFunction("SUMIFS",new SumIfsFunction("SUMIFS"));

        // SUMPRODUCT函数
        this.addFunction("SUMPRODUCT",new SumProductFunction("SUMPRODUCT"));

        // TAN函数
        this.addFunction("TAN",new TanFunction("TAN"));

    }
}

创建测试用例

java 复制代码
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 {

    @Test
    public void JOIN() throws Exception{

        FormulaRunner formulaRunner = new FormulaRunner(true,true);
        // 创建上下文
        DefaultContext<String, Object> context = new DefaultContext<>();
        String express = "JOIN([1,2,3],'%')";
        Object object = formulaRunner.execute(express, context, null, true, true);
        System.out.println(object);
    }

}

运行结果

相关推荐
loveLifeLoveCoding1 分钟前
Java List sort() 排序
java·开发语言
草履虫·7 分钟前
【Java集合】LinkedList
java
AngeliaXue9 分钟前
Java集合(List篇)
java·开发语言·list·集合
世俗ˊ10 分钟前
Java中ArrayList和LinkedList的比较
java·开发语言
zhouyiddd15 分钟前
Maven Helper 插件
java·maven·intellij idea
wx2004110218 分钟前
Codeforces Round 973 (Div. 2) - D题
数据结构·c++·算法
攸攸太上23 分钟前
Docker学习
java·网络·学习·docker·容器
Milo_K31 分钟前
项目文件配置
java·开发语言
程序员大金35 分钟前
基于SpringBoot+Vue+MySQL的养老院管理系统
java·vue.js·spring boot·vscode·后端·mysql·vim
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS网上购物商城(JAVA毕业设计)
java·vue.js·spring boot·后端·开源