1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.table;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.student.common.ui.client.widgets.rules.Token;
22
23
24
25 public class Node<T> implements Cloneable{
26
27 List<Node> childrenList = new ArrayList<Node>();
28
29 Node parent;
30
31 T userObject;
32 String id;
33
34
35
36 public Node() {}
37
38
39
40
41
42 public Node(T obj) {
43 userObject = obj;
44 }
45
46
47
48
49
50 public T getUserObject() {
51 return userObject;
52 }
53
54
55
56
57
58 public void setUserObject(T obj) {
59 userObject = obj;
60 }
61
62
63
64
65
66 public void setParent(Node parent) {
67 this.parent = parent;
68 }
69
70
71
72
73
74 public Node getParent() {
75 return parent;
76 }
77
78
79
80
81
82
83 public void addNode(Node node) {
84 childrenList.add(node);
85 node.setParent(this);
86 }
87
88
89
90
91
92 public boolean isLeaf() {
93 return getChildCount() == 0;
94 }
95
96
97
98
99
100 public int getChildCount() {
101 return childrenList.size();
102 }
103
104
105
106
107
108
109 public Node getChildAt(int index) {
110 return childrenList.get(index);
111 }
112
113 public void removeFromParent() {
114 Node parent = this.getParent();
115 if (parent == null) {
116 return;
117 } else {
118 parent.children().remove(this);
119 this.setParent(null);
120 }
121 }
122
123
124
125
126 public void remove(int childIndex) {
127 Node child = getChildAt(childIndex);
128 childrenList.remove(childIndex);
129 child.setParent(null);
130 }
131
132
133
134
135 public void remove(Node child) {
136 childrenList.remove(child);
137 child.setParent(null);
138 }
139 public void removeAllChildren(){
140 childrenList.clear();
141 }
142
143
144
145
146
147
148 public boolean isNodeChild(Node aNode) {
149 boolean retval;
150 if (aNode == null) {
151 retval = false;
152 } else {
153 if (getChildCount() == 0) {
154 retval = false;
155 } else {
156 retval = (aNode.getParent() == this);
157 }
158 }
159 return retval;
160 }
161
162 public int getIndex(Node aChild) {
163 if (!isNodeChild(aChild)) {
164 return -1;
165 }
166 return childrenList.indexOf(aChild);
167 }
168
169 public boolean isNodeSibling(Node anotherNode) {
170 boolean retval;
171
172 if (anotherNode == null) {
173 retval = false;
174 } else if (anotherNode == this) {
175 retval = true;
176 } else {
177 Node myParent = getParent();
178 retval = (myParent != null && myParent == anotherNode.getParent());
179
180 if (retval && !((Node) getParent()).isNodeChild(anotherNode)) {
181 throw new Error("sibling has different parent");
182 }
183 }
184 return retval;
185 }
186
187
188
189
190
191
192 public int getAllLeafCount() {
193 int count = 0;
194 List<Node<T>> nodeList = getAllChildren();
195 for (Node<T> node : nodeList) {
196 if (node.isLeaf()) {
197 count++;
198 }
199 }
200
201 return count;
202 }
203
204 public List<Node<T>> getAllChildren() {
205 List<Node<T>> nodeList = new ArrayList<Node<T>>();
206 for (Node<T> child : childrenList) {
207 nodeList.add(child);
208 if (!child.isLeaf()) {
209 nodeList.addAll(child.getAllChildren());
210 }
211 }
212 return nodeList;
213 }
214
215 public Node<T> getFirstLeafDescendant() {
216 List<Node<T>> list = getAllChildren();
217 for (Node<T> node : list) {
218 if (node.isLeaf()) {
219 return node;
220 }
221 }
222 return null;
223
224 }
225
226
227
228
229 public List<Node> getNonLeafChildren() {
230 List<Node> list = new ArrayList<Node>();
231 for (Node n : children()) {
232 if (n.isLeaf() == false) {
233 list.add(n);
234 }
235 }
236 return list;
237 }
238
239 public List<Node> getLeafChildren() {
240 List<Node> list = new ArrayList<Node>();
241 for (Node n : children()) {
242 if (n.isLeaf()) {
243 list.add(n);
244 }
245 }
246 return list;
247 }
248
249 public List<Node> getSiblings() {
250 Node parent = this.getParent();
251 List<Node> list = new ArrayList<Node>();
252 if (parent != null) {
253
254 for (int i = 0; i < parent.getChildCount(); i++) {
255 if (parent.getChildAt(i) != this) {
256 list.add(parent.getChildAt(i));
257 }
258 }
259 return list;
260 }
261 return list;
262
263 }
264
265 public List<Node> getLeafSiblings() {
266 List<Node> list = new ArrayList<Node>();
267 List<Node> siblings = getSiblings();
268 for (Node n : siblings) {
269 if (n.isLeaf()) {
270 list.add(n);
271 }
272 }
273
274 return list;
275 }
276
277 public List<Node> children() {
278 if (childrenList == null) {
279 return new ArrayList<Node>();
280 } else {
281 return childrenList;
282 }
283 }
284
285
286
287 public List<List<Node>> toLevel() {
288 List<List<Node>> levelList = new ArrayList<List<Node>>();
289
290 List<Node> level = new ArrayList<Node>();
291 level.add(this);
292 levelList.add(level);
293
294 int maxDistance = getMaxLevelDistance();
295
296 List<Node<T>> nodeList = getAllChildren();
297 for (int levelIndex = 1; levelIndex <= maxDistance; levelIndex++) {
298 level = new ArrayList<Node>();
299 for (Node node : nodeList) {
300 int d = getDistance(node);
301 if (levelIndex == d) {
302 level.add(node);
303 }
304 }
305 levelList.add(level);
306 }
307
308 return levelList;
309 }
310
311 public List<Node> deepTrans(Node root) {
312 List<Node> list = new ArrayList<Node>();
313 for (int i = 0; i < root.getChildCount(); i++) {
314 Node n = root.getChildAt(i);
315 if (n.isLeaf()) {
316 list.add(n);
317 } else {
318 list.addAll(deepTrans(n));
319 }
320
321 }
322 list.add(root);
323 return list;
324 }
325
326 public int getMaxLevelDistance() {
327 List<Node<T>> nodeList = getAllChildren();
328 int maxDistance = 0;
329 for (Node<T> node : nodeList) {
330 int d = getDistance(node);
331 if (maxDistance < d) {
332 maxDistance = d;
333 }
334 }
335 return maxDistance;
336 }
337
338
339 public int getDistance(Node node) {
340 Node myParent = node.getParent();
341 int level = 1;
342 while (myParent != null && myParent != this) {
343 level++;
344 myParent = myParent.getParent();
345 }
346 return level;
347 }
348
349 public String toString() {
350 if (userObject == null) {
351 return "no user object";
352 }
353
354 StringBuilder sb = new StringBuilder(userObject.toString());
355 if (this.getChildCount() == 0) {
356 return sb.toString();
357 }
358 sb.append("{");
359 for (int i = 0; i < this.getChildCount(); i++) {
360 if (this.getChildAt(i).getParent() == this) {
361 sb.append(this.getChildAt(i).toString() + ",");
362 }
363
364 }
365 sb.append("}");
366 return sb.toString();
367 }
368
369 public Node<T> clone() {
370 Node<T> newNode = new Node<T>();
371 newNode.setUserObject(this.getUserObject());
372 if (this.isLeaf() == false) {
373 for (Node<T> n : this.childrenList) {
374 newNode.addNode(n.clone());
375 }
376 }
377 return newNode;
378 }
379
380 public static void main(String[] argv) {
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 ExpressionParser p = new ExpressionParser();
411 Node<Token> n = p.parse("(a or b) and c or d and f");
412 System.out.println(n);
413 System.out.println(ExpressionParser.getExpressionString(n));
414 }
415 }