All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ql.util.express.match.QLMatchResultTree Maven / Gradle / Ivy

Go to download

QLExpress is a powerful, lightweight, dynamic language for the Java platform aimed at improving developers’ productivity in different business scenes.

The newest version!
package com.ql.util.express.match;

import java.util.ArrayList;
import java.util.List;

public class QLMatchResultTree {
    private final INodeType matchNodeType;
    private final IDataNode ref;

    private INodeType targetNodeType;
    private List left;
    private List right;

    public QLMatchResultTree(INodeType iNodeType, IDataNode ref, INodeType targetNodeType) {
        this(iNodeType, ref);
        this.targetNodeType = targetNodeType;
    }

    public QLMatchResultTree(INodeType nodeType, IDataNode ref) {
        this.matchNodeType = nodeType;
        this.ref = ref;
    }

    public IDataNode getRef() {
        return ref;
    }

    public List getLeft() {
        return this.left;
    }

    public void addLeft(QLMatchResultTree node) {
        if (this.left == null) {
            this.left = new ArrayList<>();
        }
        this.left.add(node);
    }

    public void addLeftAll(List list) {
        if (this.left == null) {
            this.left = new ArrayList<>();
        }
        this.left.addAll(list);
    }

    public void addRightAll(List list) {
        if (this.right == null) {
            this.right = new ArrayList<>();
        }
        this.right.addAll(list);
    }

    public IDataNode transferExpressNodeType(IDataNode sourceNode, INodeType targetType) {
        sourceNode.setNodeType(targetType);
        if (targetType == targetType.getManager().findNodeType("CONST_STRING")) {
            sourceNode.setObjectValue(sourceNode.getValue());
            sourceNode.setTreeType(targetType.getManager().findNodeType("CONST"));
        }
        return sourceNode;
    }

    public void buildExpressNodeTree() {
        if (this.targetNodeType != null) {
            transferExpressNodeType(this.ref, this.targetNodeType);
        }
        if (this.left != null) {
            for (QLMatchResultTree item : left) {
                this.ref.addChild(item.ref);
                item.buildExpressNodeTree();
            }
        }
        if (this.right != null) {
            for (QLMatchResultTree item : right) {
                this.ref.addChild(item.ref);
                item.buildExpressNodeTree();
            }
        }
    }

    public void printNode(StringBuilder builder, int level) {
        builder.append(level).append(":");
        for (int i = 0; i < level; i++) {
            builder.append("   ");
        }
        builder.append(ref.getValue()).append(":").append(this.matchNodeType.getName())
            .append("\n");
        if (this.left != null) {
            for (QLMatchResultTree item : this.left) {
                item.printNode(builder, level + 1);
            }
        }
        if (this.right != null) {
            for (QLMatchResultTree item : this.right) {
                item.printNode(builder, level + 1);
            }
        }
    }

    public void setTargetNodeType(INodeType targetNodeType) {
        this.targetNodeType = targetNodeType;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        printNode(builder, 1);
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy