SubjectTreeOrder.java
6.58 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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;
}
}