当前位置: 首页 > 工具软件 > QLExpress > 使用案例 >

QLExpress操作符底层实现原理(三)

凌恩
2023-12-01

QLExpress操作符底层实现原理(三)

1.OperatorNew

代码如下:

public class OperatorNew extends OperatorBase {
    public OperatorNew(String name) {
        this.name = name;
    }

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        Class<?> obj = (Class<?>)list.get(0).getObject(parent);
        if (obj.isArray()) {
            Class<?> tmpClass = obj;
            int dim = 0;
            while (tmpClass.isArray()) {
                tmpClass = tmpClass.getComponentType();
                dim = dim + 1;
            }
            int[] dimLength = new int[dim];
            for (int index = 0; index < dim; index++) {
                dimLength[index] = ((Number)(list.get(index + 1).getObject(parent)))
                    .intValue();
            }
            return OperateDataCacheManager.fetchOperateData(Array.newInstance(tmpClass, dimLength), obj);
        }
        Class<?>[] types = new Class[list.length - 1];
        Object[] objs = new Object[list.length - 1];
        Object tmpObj;
        for (int i = 0; i < types.length; i++) {
            tmpObj = list.get(i + 1).getObject(parent);
            types[i] = list.get(i + 1).getType(parent);
            objs[i] = tmpObj;
        }
        Constructor<?> c = ExpressUtil.findConstructorWithCache(obj, types);

        if (c == null) {
            // "没有找到" + obj.getName() + "的构造方法:"
            StringBuilder s = new StringBuilder();
            s.append("没有找到").append(obj.getName()).append("的构造方法:").append(obj.getName()).append("(");
            for (int i = 0; i < types.length; i++) {
                if (i > 0) {
                    s.append(",");
                }
                s.append(types[i].getName());
            }
            s.append(")");
            throw new QLException(s.toString());
        }

        tmpObj = c.newInstance(objs);
        return OperateDataCacheManager.fetchOperateData(tmpObj, obj);
    }
}

该操作符的作用是添加新的构造方法,并且构造方法必须包含在已定义的class类中。

2.OperatorNor

代码如下:

public class OperatorNor extends Operator {
    public OperatorNor(String name) {
        this.name = name;
    }

    public OperatorNor(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        return executeInner(list[0], list[1]);
    }

    public Object executeInner(Object op1, Object op2) {
        if (op1 != null) {
            return op1;
        } else {
            return op2;
        }
    }
}

该操作符的作用是判断两个对象是否为空,并返回不为空的对象。

3.OperatorNot

代码如下:

public class OperatorNot extends Operator {
    public OperatorNot(String name) {
        this.name = name;
    }

    public OperatorNot(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        return executeInner(list[0]);
    }

    public Object executeInner(Object op) throws Exception {
        Object result;
        if (op == null) {
            throw new QLException("null 不能执行操作:" + this.getAliasName());
        }
        if (Boolean.class.equals(op.getClass())) {
            result = !(Boolean)op;
        } else {
            String msg = "没有定义类型" + op.getClass().getName() + " 的 " + this.name + "操作";
            throw new QLException(msg);
        }
        return result;
    }
}

该操作符的作用是判断类中是否有该对象名的操作。

4.OperatorOr

代码如下:

public class OperatorOr extends Operator {
    public OperatorOr(String name) {
        this.name = name;
    }

    public OperatorOr(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        return executeInner(list[0], list[1]);
    }

    public Object executeInner(Object op1, Object op2) throws Exception {
        boolean r1;
        boolean r2;
        if (op1 == null) {
            r1 = false;
        } else if (op1 instanceof Boolean) {
            r1 = (Boolean)op1;
        } else {
            String msg = "没有定义类型" + op1 + "和" + op2 + " 的 " + this.name + "操作";
            throw new QLException(msg);
        }
        if (op2 == null) {
            r2 = false;
        } else if (op2 instanceof Boolean) {
            r2 = (Boolean)op2;
        } else {
            String msg = "没有定义类型" + op1 + "和" + op2 + " 的 " + this.name + "操作";
            throw new QLException(msg);
        }
        return r1 || r2;
    }
}

该操作符的作用是判断两个对象(false或true)通过“||”运算符判断返回值是否为true。

5.OperatorPrint

代码如下:

public class OperatorPrint extends Operator {
    public OperatorPrint(String name) {
        this.name = name;
    }

    public OperatorPrint(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        if (list.length != 1) {
            throw new QLException("操作数异常,有且只能有一个操作数");
        }
        System.out.print(list[0]);
        return null;
    }
}

该操作符的作用是打印操作数,并且只能打印一个操作数。

6.OperatorReduce

代码如下:

public class OperatorReduce extends Operator {
    public OperatorReduce(String name) {
        this.name = name;
    }

    public OperatorReduce(String aliasName, String name, String errorInfo) {
        this.name = name;
        this.aliasName = aliasName;
        this.errorInfo = errorInfo;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        return OperatorOfNumber.subtract(list[0], list[1], this.isPrecise);
    }
}

该操作符的作用是两个对象数组值进行减法运算。

7.OperatorRound

代码如下:

public class OperatorRound extends Operator {
    public OperatorRound(String name) {
        this.name = name;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {
        return executeInner(list[0], list[1]);
    }

    public Object executeInner(Object op1, Object op2) {
        return OperatorOfNumber.round(((Number)op1).doubleValue(), ((Number)op2).intValue());
    }
}

该操作数的作用是将数组对象值进行四舍五入保留相应的位数。
到此我已经把操作符定义的基本构造函数,总结的会有些纰漏,如有问题欢迎大家一起来探讨,留言必回呦。

 类似资料: