1
|
package eu.etaxonomy.cdm.common;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.List;
|
6
|
|
7
|
/**
|
8
|
* TODO move this class to another package.
|
9
|
* Represents a node of the Tree<T> class. The TreeNode<T,S> is also a container, and
|
10
|
* can be thought of as instrumentation to determine the location of the type T
|
11
|
* in the Tree<T>.
|
12
|
*/
|
13
|
public class TreeNode<T,S> {
|
14
|
|
15
|
public T data;
|
16
|
private S nodeId;
|
17
|
public List<TreeNode<T,S>> children;
|
18
|
|
19
|
|
20
|
public boolean containsChild(TreeNode<T,S> TreeNode){
|
21
|
boolean result = false;
|
22
|
Iterator<TreeNode<T,S>> it = this.children.iterator();
|
23
|
|
24
|
while (!result && it.hasNext()) {
|
25
|
if (it.next().data.equals(TreeNode.data)){
|
26
|
result = true;
|
27
|
}
|
28
|
}
|
29
|
return result;
|
30
|
}
|
31
|
|
32
|
public TreeNode<T,S> getChild(TreeNode<T,S> TreeNode) {
|
33
|
boolean found = false;
|
34
|
TreeNode<T,S> result = null;
|
35
|
Iterator<TreeNode<T,S>> it = children.iterator();
|
36
|
while (!found && it.hasNext()) {
|
37
|
result = it.next();
|
38
|
if (result.data.equals(TreeNode.data)){
|
39
|
found = true;
|
40
|
}
|
41
|
}
|
42
|
if (!found){
|
43
|
try {
|
44
|
throw new Exception("The node was not found in among children and that is a precondition of getChild(node) method");
|
45
|
} catch (Exception e) {
|
46
|
e.printStackTrace();
|
47
|
}
|
48
|
}
|
49
|
return result;
|
50
|
}
|
51
|
|
52
|
// /**
|
53
|
// * Convenience ctor to create a Node<T> with an instance of T.
|
54
|
// * @param data an instance of T.
|
55
|
// */
|
56
|
// public TreeNode(T data) {
|
57
|
// this.data = data;
|
58
|
// }
|
59
|
|
60
|
public TreeNode(S nodeId) {
|
61
|
super();
|
62
|
this.nodeId = nodeId;
|
63
|
}
|
64
|
|
65
|
public TreeNode() {
|
66
|
super();
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Return the children of TreeNode<T,S>. The Tree<T> is represented by a single
|
71
|
* root TreeNode<T,S> whose children are represented by a List<TreeNode<T,S>>. Each of
|
72
|
* these TreeNode<T,S> elements in the List can have children. The getChildren()
|
73
|
* method will return the children of a TreeNode<T,S>.
|
74
|
* @return the children of TreeNode<T,S>
|
75
|
*/
|
76
|
public List<TreeNode<T,S>> getChildren() {
|
77
|
if (this.children == null) {
|
78
|
return new ArrayList<TreeNode<T,S>>();
|
79
|
}
|
80
|
return this.children;
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Sets the children of a TreeNode<T,S> object. See docs for getChildren() for
|
85
|
* more information.
|
86
|
* @param children the List<TreeNode<T,S>> to set.
|
87
|
*/
|
88
|
public void setChildren(List<TreeNode<T,S>> children) {
|
89
|
this.children = children;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Returns the number of immediate children of this TreeNode<T,S>.
|
94
|
* @return the number of immediate children.
|
95
|
*/
|
96
|
public int getNumberOfChildren() {
|
97
|
if (children == null) {
|
98
|
return 0;
|
99
|
}
|
100
|
return children.size();
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Adds a child to the list of children for this TreeNode<T,S>. The addition of
|
105
|
* the first child will create a new List<TreeNode<T,S>>.
|
106
|
* @param child a TreeNode<T,S> object to set.
|
107
|
*/
|
108
|
public void addChild(TreeNode<T,S> child) {
|
109
|
if (children == null) {
|
110
|
children = new ArrayList<>();
|
111
|
}
|
112
|
children.add(child);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Inserts a TreeNode<T,S> at the specified position in the child list. Will
|
117
|
* throw an ArrayIndexOutOfBoundsException if the index does not exist.
|
118
|
* @param index the position to insert at.
|
119
|
* @param child the TreeNode<T,S> object to insert.
|
120
|
* @throws IndexOutOfBoundsException if thrown.
|
121
|
*/
|
122
|
public void insertChildAt(int index, TreeNode<T,S> child) throws IndexOutOfBoundsException {
|
123
|
if (index == getNumberOfChildren()) {
|
124
|
// this is really an append
|
125
|
addChild(child);
|
126
|
return;
|
127
|
} else {
|
128
|
children.get(index); //just to throw the exception, and stop here
|
129
|
children.add(index, child);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
public T getData() {
|
134
|
return this.data;
|
135
|
}
|
136
|
|
137
|
public void setData(T data) {
|
138
|
this.data = data;
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public String toString() {
|
143
|
StringBuilder sb = new StringBuilder();
|
144
|
|
145
|
sb.append("{").append(getData() == null ? "null" : getData().toString()).append(",[");
|
146
|
int i = 0;
|
147
|
for (TreeNode<T,S> e : getChildren()) {
|
148
|
if (i > 0) {
|
149
|
sb.append(",");
|
150
|
}
|
151
|
sb.append(e.getData().toString());
|
152
|
i++;
|
153
|
}
|
154
|
sb.append("]").append("}");
|
155
|
return sb.toString();
|
156
|
}
|
157
|
|
158
|
public S getNodeId() {
|
159
|
return nodeId;
|
160
|
}
|
161
|
public void setNodeId(S nodeId) {
|
162
|
this.nodeId = nodeId;
|
163
|
}
|
164
|
|
165
|
}
|