commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.factory; |
|
26 |
|
|
27 |
import cn.hutool.core.collection.CollectionUtil; |
|
28 |
import cn.hutool.core.util.ObjectUtil; |
|
29 |
import lombok.Data; |
|
30 |
import vip.xiaonuo.core.pojo.base.node.BaseTreeNode; |
|
31 |
|
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.List; |
|
34 |
|
|
35 |
/** |
|
36 |
* 默认递归工具类,用于遍历有父子关系的节点,例如菜单树,字典树等等 |
|
37 |
* |
|
38 |
* @author xuyuxiang |
|
39 |
* @date 2020/4/5 14:17 |
|
40 |
*/ |
|
41 |
@Data |
|
42 |
public class TreeBuildFactory<T extends BaseTreeNode> { |
|
43 |
|
|
44 |
/** |
|
45 |
* 顶级节点的父节点id(默认0) |
|
46 |
*/ |
|
47 |
private Long rootParentId = 0L; |
|
48 |
|
|
49 |
/** |
|
50 |
* 树节点构造 |
|
51 |
* |
|
52 |
* @author xuyuxiang |
|
53 |
* @date 2020/4/5 14:09 |
|
54 |
*/ |
|
55 |
public List<T> doTreeBuild(List<T> nodes) { |
|
56 |
|
|
57 |
//具体构建的过程 |
|
58 |
List<T> buildComplete = this.executeBuilding(nodes); |
|
59 |
|
|
60 |
//构建之后的处理工作 |
|
61 |
return this.afterBuild(buildComplete); |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* 查询子节点集合 |
|
66 |
* |
|
67 |
* @author xuyuxiang |
|
68 |
* @date 2020/4/5 14:10 |
|
69 |
*/ |
|
70 |
private void buildChildNodes(List<T> totalNodes, T node, List<T> childNodeLists) { |
|
71 |
if (ObjectUtil.hasEmpty(totalNodes, node)) { |
|
72 |
return; |
|
73 |
} |
|
74 |
List<T> nodeSubLists = this.getSubChildLevelOne(totalNodes, node); |
|
75 |
if (ObjectUtil.isNotEmpty(nodeSubLists)) { |
|
76 |
nodeSubLists.forEach(t -> this.buildChildNodes(totalNodes, t, CollectionUtil.newArrayList())); |
|
77 |
} |
|
78 |
// childNodeLists.addAll(nodeSubLists); |
|
79 |
node.setChildren(nodeSubLists); |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* 获取子一级节点的集合 |
|
84 |
* |
|
85 |
* @author xuyuxiang |
|
86 |
* @date 2020/4/5 14:12 |
|
87 |
*/ |
|
88 |
private List<T> getSubChildLevelOne(List<T> list, T node) { |
|
89 |
List<T> nodeList = CollectionUtil.newArrayList(); |
|
90 |
if (ObjectUtil.isNotEmpty(list)) { |
|
91 |
list.forEach(t -> { |
|
92 |
if (t.getPid().equals(node.getId())) { |
|
93 |
nodeList.add(t); |
|
94 |
} |
|
95 |
}); |
|
96 |
} |
|
97 |
return nodeList; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 执行构造 |
|
102 |
* |
|
103 |
* @author xuyuxiang |
|
104 |
* @date 2020/4/5 14:13 |
|
105 |
*/ |
|
106 |
private List<T> executeBuilding(List<T> nodes) { |
|
107 |
List<T> parentNodes = afterBuild(nodes); |
|
108 |
parentNodes.forEach(t -> this.buildChildNodes(nodes, t, CollectionUtil.newArrayList())); |
|
109 |
return parentNodes; |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 构造之后 |
|
114 |
* |
|
115 |
* @author xuyuxiang |
|
116 |
* @date 2020/4/5 14:13 |
|
117 |
*/ |
|
118 |
private List<T> afterBuild(List<T> nodes) { |
|
119 |
//去掉所有的二级节点 |
|
120 |
ArrayList<T> results = CollectionUtil.newArrayList(); |
|
121 |
nodes.forEach(t -> { |
|
122 |
if (rootParentId.equals(t.getPid())) { |
|
123 |
results.add(t); |
|
124 |
} |
|
125 |
}); |
|
126 |
return results; |
|
127 |
} |
|
128 |
} |