Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Node |
|
| 1.6;1.6 |
1 | package org.kuali.rice.core.util; | |
2 | ||
3 | import java.io.Serializable; | |
4 | import java.util.ArrayList; | |
5 | import java.util.List; | |
6 | ||
7 | /** | |
8 | * Represents a node of the Tree<T, K> class. The Node<T, K> is also a container, and | |
9 | * can be thought of as instrumentation to determine the location of the type T | |
10 | * in the Tree<T ,K>. | |
11 | */ | |
12 | public class Node<T, K> implements Serializable { | |
13 | private static final long serialVersionUID = -2650587832812603561L; | |
14 | ||
15 | private T data; | |
16 | private List<Node<T, K>> children; | |
17 | ||
18 | private K nodeLabel; | |
19 | private String nodeType; | |
20 | ||
21 | /** | |
22 | * Default constructor. | |
23 | */ | |
24 | public Node() { | |
25 | 0 | super(); |
26 | ||
27 | 0 | children = new ArrayList<Node<T, K>>(); |
28 | 0 | } |
29 | ||
30 | /** | |
31 | * Convenience constructor to create a Node<T, K> with an instance of T. | |
32 | * | |
33 | * @param data an instance of T. | |
34 | */ | |
35 | public Node(T data) { | |
36 | 0 | this(); |
37 | 0 | setData(data); |
38 | 0 | } |
39 | ||
40 | /** | |
41 | * Return the children of Node<T, K>. The Tree<T> is represented by a single | |
42 | * root Node<T, K> whose children are represented by a List<Node<T, K>>. Each of | |
43 | * these Node<T, K> elements in the List can have children. The getChildren() | |
44 | * method will return the children of a Node<T, K>. | |
45 | * | |
46 | * @return the children of Node<T, K> | |
47 | */ | |
48 | public List<Node<T, K>> getChildren() { | |
49 | 0 | if (this.children == null) { |
50 | 0 | return new ArrayList<Node<T, K>>(); |
51 | } | |
52 | 0 | return this.children; |
53 | } | |
54 | ||
55 | /** | |
56 | * Sets the children of a Node<T, K> object. See docs for getChildren() for | |
57 | * more information. | |
58 | * | |
59 | * @param children the List<Node<T, K>> to set. | |
60 | */ | |
61 | public void setChildren(List<Node<T, K>> children) { | |
62 | 0 | this.children = children; |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * Returns the number of immediate children of this Node<T, K>. | |
67 | * | |
68 | * @return the number of immediate children. | |
69 | */ | |
70 | public int getNumberOfChildren() { | |
71 | 0 | if (children == null) { |
72 | 0 | return 0; |
73 | } | |
74 | 0 | return children.size(); |
75 | } | |
76 | ||
77 | /** | |
78 | * Adds a child to the list of children for this Node<T, K>. The addition of | |
79 | * the first child will create a new List<Node<T, K>>. | |
80 | * | |
81 | * @param child a Node<T, K> object to set. | |
82 | */ | |
83 | public void addChild(Node<T, K> child) { | |
84 | 0 | if (children == null) { |
85 | 0 | children = new ArrayList<Node<T, K>>(); |
86 | } | |
87 | 0 | children.add(child); |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * Inserts a Node<T, K> at the specified position in the child list. Will | |
92 | * throw an ArrayIndexOutOfBoundsException if the index does not exist. | |
93 | * | |
94 | * @param index the position to insert at. | |
95 | * @param child the Node<T, K> object to insert. | |
96 | * @throws IndexOutOfBoundsException if thrown. | |
97 | */ | |
98 | public void insertChildAt(int index, Node<T, K> child) throws IndexOutOfBoundsException { | |
99 | 0 | if (index == getNumberOfChildren()) { |
100 | // this is really an append | |
101 | 0 | addChild(child); |
102 | 0 | return; |
103 | } else { | |
104 | 0 | children.get(index); //just to throw the exception, and stop here |
105 | 0 | children.add(index, child); |
106 | } | |
107 | 0 | } |
108 | ||
109 | /** | |
110 | * Remove the Node<T, K> element at index index of the List<Node<T, K>>. | |
111 | * | |
112 | * @param index the index of the element to delete. | |
113 | * @throws IndexOutOfBoundsException if thrown. | |
114 | */ | |
115 | public void removeChildAt(int index) throws IndexOutOfBoundsException { | |
116 | 0 | children.remove(index); |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * Data object contained in the node (a leaf) | |
121 | * | |
122 | * @return Object | |
123 | */ | |
124 | public T getData() { | |
125 | 0 | return this.data; |
126 | } | |
127 | ||
128 | /** | |
129 | * Setter for the nodes data | |
130 | * | |
131 | * @param data | |
132 | */ | |
133 | public void setData(T data) { | |
134 | 0 | this.data = data; |
135 | 0 | } |
136 | ||
137 | /** | |
138 | * Object containing the data for labeling the node (can be simple String) | |
139 | * | |
140 | * @return K | |
141 | */ | |
142 | public K getNodeLabel() { | |
143 | 0 | return nodeLabel; |
144 | } | |
145 | ||
146 | /** | |
147 | * Setter for the nodes label data | |
148 | * | |
149 | * @param nodeLabel | |
150 | */ | |
151 | public void setNodeLabel(K nodeLabel) { | |
152 | 0 | this.nodeLabel = nodeLabel; |
153 | 0 | } |
154 | ||
155 | /** | |
156 | * Indicates what type of node is being represented, used to give | |
157 | * a functional label for the node that can also be used for presentation | |
158 | * purposes | |
159 | * | |
160 | * @return String node type | |
161 | */ | |
162 | public String getNodeType() { | |
163 | 0 | return nodeType; |
164 | } | |
165 | ||
166 | /** | |
167 | * Setter for the node type String | |
168 | * | |
169 | * @param nodeType | |
170 | */ | |
171 | public void setNodeType(String nodeType) { | |
172 | 0 | this.nodeType = nodeType; |
173 | 0 | } |
174 | ||
175 | public String toString() { | |
176 | 0 | StringBuilder sb = new StringBuilder(); |
177 | 0 | sb.append("{").append(getData().toString()).append(",["); |
178 | 0 | int i = 0; |
179 | 0 | for (Node<T, K> e : getChildren()) { |
180 | 0 | if (i > 0) { |
181 | 0 | sb.append(","); |
182 | } | |
183 | 0 | sb.append(e.getData().toString()); |
184 | 0 | i++; |
185 | } | |
186 | 0 | sb.append("]").append("}"); |
187 | 0 | return sb.toString(); |
188 | } | |
189 | } | |
190 |