283a66326a836a16515863eb41477366645780d4
[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 boolean hasChildNode(Node<E> childNode) {
91                 return children.contains(childNode);
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         public int getIndexOfChild(Node<E> childNode) {
98                 int childIndex = 0;
99                 for (Node<E> node: children) {
100                         if (node.equals(childNode)) {
101                                 return childIndex;
102                         }
103                         childIndex++;
104                 }
105                 return -1;
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         public int getIndexOfChild(E element) {
112                 int childIndex = 0;
113                 for (Node<E> node: children) {
114                         if (node.getElement().equals(element)) {
115                                 return childIndex;
116                         }
117                         childIndex++;
118                 }
119                 return -1;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public void removeChild(Node<E> childNode) {
126                 children.remove(childNode);
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         public void removeChild(E child) {
133                 for (Node<E> childNode: children) {
134                         if (child.equals(childNode.getElement())) {
135                                 children.remove(childNode);
136                                 break;
137                         }
138                 }
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         public void removeChild(int childIndex) {
145                 children.remove(childIndex);
146         }
147
148         /**
149          * {@inheritDoc}
150          */
151         public void removeAllChildren() {
152                 children.clear();
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         public Iterator<Node<E>> iterator() {
159                 return children.iterator();
160         }
161
162         /**
163          * {@inheritDoc}
164          */
165         public Node<E> findChild(E element) {
166                 for (Node<E> childNode: children) {
167                         Node<E> wantedNode = childNode.findChild(element);
168                         if (wantedNode != null) {
169                                 return wantedNode;
170                         }
171                 }
172                 if (this.element.equals(element)) {
173                         return this;
174                 }
175                 return null;
176         }
177
178 }