SubjectTreeOrder.java 6.58 KB
package com.meishu.vo.subject;

import com.meishu.util.tree.SerialNumber;
import lombok.Data;

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

@Data
public class SubjectTreeOrder {

    private String id;

    public List<GetSubjectTreeVO> nodeList;

    private String separator;

    private Long idSeq;

    private SerialNumber number;

    public SubjectTreeOrder(String separator, SerialNumber number) {
        init();
        this.separator = separator;
        this.number = number;

    }

    public void init() {
        idSeq = 0L;
        this.separator = ".";
        if (this.nodeList == null) {
            nodeList = new ArrayList<>();
        }
    }


    /**
     * <p>功能描述:根据父节点获取所有子节点。</p>
     */
    public List<GetSubjectTreeVO> getChildNodes(GetSubjectTreeVO pNode,List<GetSubjectTreeVO> nodeList) {
        Long pId = pNode.getId();
        return getChildNodes(pId,nodeList);
    }

    /**
     * <p>功能描述:根据父节点获取所有子节点。</p>
     */
    public List<GetSubjectTreeVO> getChildNodes(Long pId,List<GetSubjectTreeVO> nodeList) {
        List<GetSubjectTreeVO> childNodes = new ArrayList<>();
        for (GetSubjectTreeVO n : nodeList) {
            if (pId.equals(n.getParentId())) {
                childNodes.add(n);
            }
        }

        return childNodes;
    }

    /**
     * <p>功能描述:获取本级值最大的节点。</p>
     */
    public GetSubjectTreeVO getMaxNodeForThisLevel(GetSubjectTreeVO pNode,List<GetSubjectTreeVO> nodeList) {
        List<GetSubjectTreeVO> childList = getChildNodes(pNode,nodeList);
        GetSubjectTreeVO root = getRoot(nodeList);
        if (childList.size() <= 0) {
            return null;
        }
        GetSubjectTreeVO maxNode = root;
        for (GetSubjectTreeVO node : childList) {
            if (maxNode.getNumber().compareTo(node.getNumber()) <= 0) {
                maxNode = node;
            }
        }
        return maxNode;
    }

    /**
     * <p>功能描述:生成下一个子节点。</p>
     */
    public GetSubjectTreeVO generateNextChildNode(GetSubjectTreeVO node,List<GetSubjectTreeVO> nodeList) {
        GetSubjectTreeVO newNode = null;
        GetSubjectTreeVO maxNode = getMaxNodeForThisLevel(node,nodeList);
        String nextNumber = number.firstNumber();
        int level = node.getLevel();
        if (maxNode != null && !"0".equals(maxNode.getId())) {//本级存在子节点,且非根节点
            nextNumber = number.produceNext(maxNode.getNumber());
            level = maxNode.getLevel();
        }
        newNode = new GetSubjectTreeVO(++idSeq, nextNumber, node.getId(), level);

        generateNodeText(newNode, nextNumber,nodeList);

        return newNode;
    }


    /**
     *
     * <p>功能描述:获取父节点。</p>
     */
    public GetSubjectTreeVO getParentNode(GetSubjectTreeVO node,List<GetSubjectTreeVO> nodeList) {
        for (GetSubjectTreeVO n : nodeList) {
            if (node.getParentId().equals(n.getId())) {
                return n;
            }
        }
        return node;
    }

    /**
     * <p>功能描述:生成节点路径。</p>
     */
    public void generateNodeText(GetSubjectTreeVO node, String text,List<GetSubjectTreeVO> nodeList) {

        if (node == null || "0".equals(node.getId())) {
            return;
        }

        GetSubjectTreeVO pNode = getParentNode(node,nodeList);

            if (!"0".equals(pNode.getId())) {
                text = pNode.getText() + separator + text;
            }
        node.setText(text);
    }

//    /**
//     * <p>功能描述:遍历所有树节点。</p>
//     */
//    public void traverseNodeList(GetSubjectTreeVO node) {
//        if(node==null){
//            node = getRoot(nodeList);
//        }
//        List<GetSubjectTreeVO> childNodes = getChildNodes(node);
//        System.out.println(node.getText());
//        if (childNodes.size() > 0) {
//            for (GetSubjectTreeVO n : childNodes) {
//                traverseNodeList(n);
//            }
//        }
//    }
    public static void main(String[] args) {
        SerialNumber number = new SerialNumber();
        SubjectTreeOrder treeNode = new SubjectTreeOrder(".", number);

//        addSomeNodes(treeNode);
//        treeNode.traverseNodeList(null);
    }

    /**
     * <p>功能描述:获取根节点。</p>
     */
    public GetSubjectTreeVO getRoot(List<GetSubjectTreeVO> nodeList) {
        GetSubjectTreeVO root = null;
        if (nodeList.size() <= 0 || (root = getNodeById(nodeList, "0")) == null) {
            root = createRoot();
            nodeList.add(root);
        }
        return root;
    }

    private GetSubjectTreeVO getNodeById(List<GetSubjectTreeVO> nodeList, String id) {
        GetSubjectTreeVO node = null;
        if(id!=null){
            for (GetSubjectTreeVO n : nodeList) {
                if (id.equals(n.getId())) {
                    node = n;
                    break;
                }
            }
        }
        return node;
    }

    private GetSubjectTreeVO createRoot() {
        GetSubjectTreeVO root = new GetSubjectTreeVO(0L, number.rootNumber(), -1L, 0);
        root.setText("0");
        return root;
    }

    /**
     * <p>功能描述:测试添加节点。</p>
     */
//    private static GetSubjectTreeVO addSomeNodes(SubjectTreeOrder tree) {
//        GetSubjectTreeVO root = tree.getRoot(tree.nodeList);
//        GetSubjectTreeVO node1 = getNextNode(tree, root);//1
//        GetSubjectTreeVO node2 = getNextNode(tree, root);//2
//        GetSubjectTreeVO node3 = getNextNode(tree, root);//3
//        GetSubjectTreeVO node11 = getNextNode(tree, node1);//1.1
//        GetSubjectTreeVO node12 = getNextNode(tree, node1);//1.2
//        GetSubjectTreeVO node21 = getNextNode(tree, node2);//2.1
//        GetSubjectTreeVO node211 = getNextNode(tree, node21);//2.1.1
//        GetSubjectTreeVO node212 = getNextNode(tree, node21);//2.1.2
//        GetSubjectTreeVO node22 = getNextNode(tree, node2);//2.2
//        GetSubjectTreeVO node221 = getNextNode(tree, node22);//2.2.1
//        GetSubjectTreeVO node31 = getNextNode(tree, node3);
//        GetSubjectTreeVO node32 = getNextNode(tree, node3);
//        GetSubjectTreeVO node311 = getNextNode(tree, node31);
//        GetSubjectTreeVO node3111 = getNextNode(tree, node311);
//        return root;
//    }

    public static GetSubjectTreeVO getNextNode(SubjectTreeOrder tree, GetSubjectTreeVO pNode,List<GetSubjectTreeVO> nodeList) {
        GetSubjectTreeVO node = tree.generateNextChildNode(pNode,nodeList);
        if (node != null) {
            nodeList.add(node);
        }
        return node;
    }

}