陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.mzl.flower.utils;
 
import com.mzl.flower.base.Node;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.List;
 
@UtilityClass
public class TreeBuilderUtil {
 
    /**
     * list to tree
     * 默认以parentId 为空的为根节点
     *
     * @param dirs
     * @return
     */
    public List<? extends Node> buildListToTree(List<? extends Node> dirs) {
        List<Node> roots = findRoots(dirs, null);
        List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots);
        for (Node root : roots) {
            root.setChildren(findChildren(root, notRoots));
        }
        return roots;
    }
 
    /**
     * list to tree
     * 以parentId 为 rootParentIdValue值 的为根节点
     *
     * @param dirs
     * @param rootParentIdValue
     * @return
     */
    public List<? extends Node> buildListToTree(List<? extends Node> dirs, Object rootParentIdValue) {
        List<Node> roots = findRoots(dirs, rootParentIdValue);
        List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots);
        for (Node root : roots) {
            root.setChildren(findChildren(root, notRoots));
        }
        return roots;
    }
 
    /**
     * list to tree
     * 以没有父节点的节点作为根节点
     *
     * @param dirs
     * @return
     */
    public List<? extends Node> buildCommonListToTree(List<? extends Node> dirs) {
        List<Node> roots = findRoots(dirs, null);
        List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots);
        for (Node root : roots) {
            root.setChildren(findChildren(root, notRoots));
        }
        return roots;
    }
 
    private List<Node> findRoots(List<? extends Node> allNodes, Object rootParentIdValue) {
        List<Node> results = new ArrayList<Node>();
        for (Node node : allNodes) {
            if ((isBlank(rootParentIdValue) && (isBlank(node.getParentId()) || "-1".equals(node.getParentId())))
                    || (isNotBlank(rootParentIdValue) && "-1".equals(node.getParentId()) && rootParentIdValue.equals(node.getParentId()))) {
                results.add(node);
            }
        }
        return results;
    }
 
    private List<Node> findRoots(List<? extends Node> allNodes) {
        List<Node> results = new ArrayList<Node>();
        for (Node node : allNodes) {
            boolean isRoot = true;
            for (Node comparedOne : allNodes) {
                if (isNotBlank(node.getParentId()) && node.getParentId().equals(comparedOne.getId())) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                results.add(node);
            }
        }
        return results;
    }
 
    private List<Node> findChildren(Node root, List<Node> allNodes) {
        List<Node> children = new ArrayList<Node>();
 
        for (Node comparedOne : allNodes) {
            if (isNotBlank(comparedOne.getParentId()) && comparedOne.getParentId().equals(root.getId())) {
                children.add(comparedOne);
            }
        }
        List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children);
        for (Node child : children) {
            List<Node> tmpChildren = findChildren(child, notChildren);
            child.setChildren(tmpChildren);
        }
        return children;
    }
 
    private boolean isBlank(Object o) {
        return o == null || StringUtils.isBlank(o.toString());
    }
 
    private boolean isNotBlank(Object o) {
        return !isBlank(o);
    }
}