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