266cea1f49533e958b57c5101c88abd103cf0160
[jSite2.git] / src / net / pterodactylus / util / data / NodeImpl.java
1
2 package net.pterodactylus.util.data;
3
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.Iterator;
8 import java.util.List;
9
10 /**
11  * Implementation of the {@link Node} interface.
12  * 
13  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
14  * @param <E>
15  *            The type of the element to store
16  */
17 class NodeImpl<E extends Comparable<E>> implements Node<E> {
18
19         /** The parent node of this node. */
20         private final Node<E> parentNode;
21
22         /** The element contained in this node. */
23         private final E element;
24
25         /** The child nodes of this node. */
26         private final List<Node<E>> children = new ArrayList<Node<E>>();
27
28         /**
29          * Creates a new root node.
30          */
31         NodeImpl() {
32                 this.parentNode = null;
33                 this.element = null;
34         }
35
36         /**
37          * Creates a new node with the given parent and element.
38          * 
39          * @param parentNode
40          *            The parent of this node
41          * @param element
42          *            The element of this node
43          */
44         NodeImpl(Node<E> parentNode, E element) {
45                 if ((parentNode == null) || (element == null)) {
46                         throw new NullPointerException("null is not allowed as parent or element");
47                 }
48                 this.parentNode = parentNode;
49                 this.element = element;
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         public Node<E> getParent() {
56                 return parentNode;
57         }
58
59         /**
60          * {@inheritDoc}
61          */
62         public E getElement() {
63                 return element;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         public Node<E> addChild(E child) {
70                 Node<E> childNode = new NodeImpl<E>(this, child);
71                 children.add(childNode);
72                 return childNode;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public int size() {
79                 return children.size();
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         public Node<E> getChild(int childIndex) {
86                 return children.get(childIndex);
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         public Node<E> getChild(E element) {
93                 for (Node<E> childNode: children) {
94                         if (childNode.getElement().equals(element)) {
95                                 return childNode;
96                         }
97                 }
98                 return null;
99         }
100
101         /**
102          * {@inheritDoc}
103          */
104         public boolean hasChild(Node<E> childNode) {
105                 return children.contains(childNode);
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         public boolean hasChild(E element) {
112                 for (Node<E> childNode: children) {
113                         if (childNode.getElement().equals(element)) {
114                                 return true;
115                         }
116                 }
117                 return false;
118         }
119
120         /**
121          * {@inheritDoc}
122          */
123         public int getIndexOfChild(Node<E> childNode) {
124                 int childIndex = 0;
125                 for (Node<E> node: children) {
126                         if (node.equals(childNode)) {
127                                 return childIndex;
128                         }
129                         childIndex++;
130                 }
131                 return -1;
132         }
133
134         /**
135          * {@inheritDoc}
136          */
137         public int getIndexOfChild(E element) {
138                 int childIndex = 0;
139                 for (Node<E> node: children) {
140                         if (node.getElement().equals(element)) {
141                                 return childIndex;
142                         }
143                         childIndex++;
144                 }
145                 return -1;
146         }
147
148         /**
149          * {@inheritDoc}
150          */
151         public void removeChild(Node<E> childNode) {
152                 children.remove(childNode);
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         public void removeChild(E child) {
159                 for (Node<E> childNode: children) {
160                         if (child.equals(childNode.getElement())) {
161                                 children.remove(childNode);
162                                 break;
163                         }
164                 }
165         }
166
167         /**
168          * {@inheritDoc}
169          */
170         public void removeChild(int childIndex) {
171                 children.remove(childIndex);
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         public void removeAllChildren() {
178                 children.clear();
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         public Iterator<Node<E>> iterator() {
185                 return children.iterator();
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         public Node<E> findChild(E element) {
192                 for (Node<E> childNode: children) {
193                         Node<E> wantedNode = childNode.findChild(element);
194                         if (wantedNode != null) {
195                                 return wantedNode;
196                         }
197                 }
198                 if (this.element.equals(element)) {
199                         return this;
200                 }
201                 return null;
202         }
203
204         /**
205          * {@inheritDoc}
206          */
207         public void sortChildren() {
208                 Collections.sort(children);
209         }
210
211         /**
212          * {@inheritDoc}
213          */
214         public void sortChildren(Comparator<Node<E>> comparator) {
215                 Collections.sort(children, comparator);
216         }
217
218         //
219         // INTERFACE Comparable
220         //
221
222         /**
223          * {@inheritDoc}
224          */
225         public int compareTo(Node<E> otherNode) {
226                 return element.compareTo(otherNode.getElement());
227         }
228
229 }