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

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

谢叶五
2023-12-01

QLExpress操作符底层实现原理

1.OperatorAdd

代码如下:

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

    public OperatorAdd(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.add(list[0], list[1], this.isPrecise);
    }
}

该操作符就是将对象里面的数组值进行相加,可以使用高精度数据。

2.OperatorAlias

代码如下:

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

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

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        String varName = (String)list.get(0).getObjectInner(parent);
        OperateDataAttr realAttr = (OperateDataAttr)list.get(1);
        OperateDataAttr result = new OperateDataAlias(varName, realAttr);
        parent.addSymbol(varName, result);
        return result;
    }
}

该操作符就是将变量的名字替换成别名。

3.OperatorAnd

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

    public OperatorAnd(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 operand1, Object operand2) throws Exception {
        boolean r1;
        if (operand1 == null) {
            r1 = false;
        } else if (operand1 instanceof Boolean) {
            r1 = (Boolean)operand1;
        } else {
            String msg = "没有定义类型" + operand1 + "和" + operand2 + " 的 " + this.name + "操作";
            throw new QLException(msg);
        }

        boolean r2;
        if (operand2 == null) {
            r2 = false;
        } else if (operand2 instanceof Boolean) {
            r2 = (Boolean)operand2;
        } else {
            String msg = "没有定义类型" + operand1 + "和" + operand2 + " 的 " + this.name + "操作";
            throw new QLException(msg);
        }
        return r1 && r2;
    }
}

该操作符就是一个连接符,就是将两个变量连接到一起,如String msg = “没有定义类型” + operand1 + “和” + operand2 + " 的 " + this.name + “操作”;

4.OperatorAnonymousNewMap

代码如下:

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

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

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        Map<Object, Object> result = new HashMap<>();
        for (int i = 0; i < list.length; i++) {
            Object key = ((OperateDataKeyValue)list.get(i)).getKey().getObject(parent);
            Object value = ((OperateDataKeyValue)list.get(i)).getValue().getObject(parent);
            result.put(key, value);
        }
        return OperateDataCacheManager.fetchOperateData(result, HashMap.class);
    }
}

该操作符就是将键值对(key,value)放入集合中去。

5.OperatorAdd

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

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

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        OperateData firstOperateData = list.get(0);
        if (firstOperateData == null || firstOperateData.getObject(parent) == null) {
            throw new QLException("对象为null,不能执行数组相关操作");
        }

        Object tmpObject = firstOperateData.getObject(parent);

        if (!tmpObject.getClass().isArray()) {
            Object property = list.get(1).getObject(parent);
            //支持data.get(index) ->data[index]
            if (tmpObject instanceof List && property instanceof Number) {
                int index = ((Number)property).intValue();
                return OperateDataCacheManager.fetchOperateDataArrayItem(firstOperateData, index);
            }
            //支持data.code -> data['code']
            if (property instanceof String || property instanceof Character) {
                return OperateDataCacheManager.fetchOperateDataField(tmpObject, String.valueOf(property));
            }
        }
        //支持原生Array:data[index]
        int index = ((Number)list.get(1).getObject(parent)).intValue();//获取数组的下标值
        return OperateDataCacheManager.fetchOperateDataArrayItem(firstOperateData, index);
    }
}

该操作符的作用就是获取到数组的下标值。

OperatorBit

代码如下:

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

    @Override
    public Object executeInner(Object[] list) throws Exception {
        if ("~".equals(this.name)) {
            if (list.length == 1 && list[0] instanceof Number) {
                if (list[0] instanceof Integer) {
                    return ~(((Number)list[0]).intValue());
                } else {
                    return ~(((Number)list[0]).longValue());
                }
            } else {
                throw new QLException("取反操作符 ~ 参数不合法:" + Arrays.toString(list));
            }
        }
        if ("&".equals(this.name)) {
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if (list[0] instanceof Integer && list[1] instanceof Integer) {
                    return (Integer)list[0] & (Integer)list[1];
                }
                return (((Number)list[0]).longValue()) & (((Number)list[1]).longValue());
            } else {
                throw new QLException("按位与操作符 & 两边的参数不合法:" + Arrays.toString(list));
            }
        }
        if ("|".equals(this.name)) {
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if (list[0] instanceof Integer && list[1] instanceof Integer) {
                    return (Integer)list[0] | (Integer)list[1];
                }
                return (((Number)list[0]).longValue()) | (((Number)list[1]).longValue());
            } else {
                throw new QLException("按位或操作符 | 两边的参数不合法:" + Arrays.toString(list));
            }
        }
        if ("^".equals(this.name)) {
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if (list[0] instanceof Integer && list[1] instanceof Integer) {
                    return (Integer)list[0] ^ (Integer)list[1];
                }
                return (((Number)list[0]).longValue()) ^ (((Number)list[1]).longValue());
            } else {
                throw new QLException("按位异或操作符 ^ 两边的参数不合法:" + Arrays.toString(list));
            }
        }
        if ("<<".equals(this.name)) {
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if (list[0] instanceof Integer && list[1] instanceof Integer) {
                    return (Integer)list[0] << (Integer)list[1];
                }
                return (((Number)list[0]).longValue()) << (((Number)list[1]).longValue());
            } else {
                throw new QLException("左移操作符 << 两边的参数不合法:" + Arrays.toString(list));
            }
        }
        if (">>".equals(this.name)) {
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if (list[0] instanceof Integer && list[1] instanceof Integer) {
                    return (Integer)list[0] >> (Integer)list[1];
                }
                return (((Number)list[0]).longValue()) >> (((Number)list[1]).longValue());
            } else {
                throw new QLException("右移操作符 >> 两边的参数不合法:" + Arrays.toString(list));
            }
        }
        throw new QLException("不支持的位运算操作符:" + Arrays.toString(list));
    }
}

OperatorDef

代码如下:

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

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

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        Object type = list.get(0).getObject(parent);
        String varName = (String)list.get(1).getObject(parent);
        Class<?> tmpClass;
        if (type instanceof Class) {
            tmpClass = (Class<?>)type;
        } else {
            tmpClass = OperateDataVirClass.class;
        }
        OperateDataLocalVar result = OperateDataCacheManager.fetchOperateDataLocalVar(varName, tmpClass);
        parent.addSymbol(varName, result);
        return result;
    }
}

该操作符的作用是定义calss类的类型。
由于内容太多,我将分三部分进行整理,有问题的请联系我的邮箱1443497132@qq.com。

 类似资料: