1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.tree;
17
18 import static com.google.common.collect.Lists.newArrayList;
19 import static com.google.common.collect.Lists.reverse;
20 import static com.google.common.collect.Lists.transform;
21 import static org.kuali.common.util.base.Precondition.checkNotNull;
22
23 import java.util.List;
24
25 public abstract class AbstractNode<T> implements Node<T> {
26
27
28
29
30 @Override
31 public boolean isRoot() {
32 return !getParent().isPresent();
33 }
34
35
36
37
38 @Override
39 public boolean isLeaf() {
40 return getChildren().isEmpty();
41 }
42
43
44
45
46 @Override
47 public int getLevel() {
48 int level = 0;
49 Node<T> ancestor = this;
50 while (ancestor.getParent().isPresent()) {
51 ancestor = ancestor.getParent().get();
52 level++;
53 }
54 return level;
55 }
56
57
58
59
60 @Override
61 public List<Node<T>> getPath() {
62 Node<T> ancestor = this;
63 List<Node<T>> list = newArrayList();
64 list.add(ancestor);
65 while (ancestor.getParent().isPresent()) {
66 ancestor = ancestor.getParent().get();
67 list.add(ancestor);
68 }
69 return reverse(list);
70 }
71
72
73
74
75 @Override
76 public List<T> getElementPath() {
77 return transform(getPath(), new NodeElementFunction<T>());
78 }
79
80
81
82
83 @Override
84 public boolean isChild(Node<T> parent) {
85 checkNotNull(parent, "parent");
86 return parent.getChildren().contains(this);
87 }
88
89
90
91
92 @Override
93 public boolean isParent(Node<T> child) {
94 checkNotNull(child, "child");
95 return getChildren().contains(child);
96 }
97
98
99
100
101 @Override
102 public boolean isAncestor(Node<T> parent) {
103 checkNotNull(parent, "parent");
104 return getPath().contains(parent);
105 }
106
107 }