java构建树用的Node

package org.ccnt.med.body;

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

public class Node {

    // 叶子节点
    public final static String TYPE_LEAF = "leaf";

    // 有子节点的节点
    public final static String TYPE_NODE = "node";

    // 节点键值
    private String org_code;

    // 节点名称
    private String org_name;

    // 节点状态
    private String status;

    // 节点类型
    private String org_type;

    // 父节点值
    private String parent_code;

    // 子节点数组
    private Node[] childNodes;

    /**
     * 转换列表为数组
     * 
     * @param NodeList
     * @return
     */
    public static Node[] listToArray(List<Node> NodeList) {
        Node[] nodes = null;
        if (NodeList != null) {
            nodes = new Node[NodeList.size()];
            for (int i = 0; i < NodeList.size(); i++) {
                nodes[i] = (Node) NodeList.get(i);
            }
        }
        return nodes;
    }

    /**
     * 判断是否是有子节点的节点
     * 
     * @return
     */
    public boolean isNode() {
        if (this.getChildNodes() != null && this.getChildNodes().length > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是叶子节点
     * 
     * @return
     */
    public boolean isLeaf() {
        return !isNode();
    }

    /**
     * 根据列表设置当前节点的子节点
     */
    public void filterChildNodes(Node[] nodes) {
        List<Node> list = new ArrayList<Node>();
        Node tempNode = new Node();
        for (int i = 0; i < nodes.length; i++) {
            tempNode = nodes[i];
            if ("root".equals(tempNode.getParent_code())
                    && this.getOrg_code() == null) {
                list.add(tempNode);
            } else {
                if (!"root".equals(tempNode.getParent_code())
                        && this.getOrg_code() != null) {
                    if (tempNode.getParent_code().equals(this.getOrg_code())) {
                        list.add(tempNode);
                    }
                }
            }
        }

        this.setChildNodes(listToArray(list));
    }

    public String getOrg_code() {
        return org_code;
    }

    public void setOrg_code(String org_code) {
        this.org_code = org_code;
    }

    public String getOrg_name() {
        return org_name;
    }

    public void setOrg_name(String org_name) {
        this.org_name = org_name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getOrg_type() {
        return org_type;
    }

    public void setOrg_type(String org_type) {
        this.org_type = org_type;
    }

    public String getParent_code() {
        return parent_code;
    }

    public void setParent_code(String parent_code) {
        this.parent_code = parent_code;
    }

    public Node[] getChildNodes() {
        return childNodes;
    }

    public void setChildNodes(Node[] childNodes) {
        this.childNodes = childNodes;
    }

    public static String getTYPE_LEAF() {
        return TYPE_LEAF;
    }

    public static String getTYPE_NODE() {
        return TYPE_NODE;
    }
}
原文地址:https://www.cnblogs.com/549294286/p/3150054.html