2 package net.pterodactylus.util.data;
4 import java.util.ArrayList;
5 import java.util.Iterator;
9 * Implementation of the {@link Node} interface.
11 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
13 * The type of the element to store
15 class NodeImpl<E> implements Node<E> {
17 /** The parent node of this node. */
18 private final Node<E> parentNode;
20 /** The element contained in this node. */
21 private final E element;
23 /** The child nodes of this node. */
24 private final List<Node<E>> children = new ArrayList<Node<E>>();
27 * Creates a new root node.
30 this.parentNode = null;
35 * Creates a new node with the given parent and element.
38 * The parent of this node
40 * The element of this node
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");
46 this.parentNode = parentNode;
47 this.element = element;
53 public Node<E> getParent() {
60 public E getElement() {
67 public Node<E> addChild(E child) {
68 Node<E> childNode = new NodeImpl<E>(this, child);
69 children.add(childNode);
77 return children.size();
83 public Node<E> getChild(int childIndex) {
84 return children.get(childIndex);
90 public Node<E> getChild(E element) {
91 for (Node<E> childNode: children) {
92 if (childNode.getElement().equals(element)) {
102 public boolean hasChild(Node<E> childNode) {
103 return children.contains(childNode);
109 public boolean hasChild(E element) {
110 for (Node<E> childNode: children) {
111 if (childNode.getElement().equals(element)) {
121 public int getIndexOfChild(Node<E> childNode) {
123 for (Node<E> node: children) {
124 if (node.equals(childNode)) {
135 public int getIndexOfChild(E element) {
137 for (Node<E> node: children) {
138 if (node.getElement().equals(element)) {
149 public void removeChild(Node<E> childNode) {
150 children.remove(childNode);
156 public void removeChild(E child) {
157 for (Node<E> childNode: children) {
158 if (child.equals(childNode.getElement())) {
159 children.remove(childNode);
168 public void removeChild(int childIndex) {
169 children.remove(childIndex);
175 public void removeAllChildren() {
182 public Iterator<Node<E>> iterator() {
183 return children.iterator();
189 public Node<E> findChild(E element) {
190 for (Node<E> childNode: children) {
191 Node<E> wantedNode = childNode.findChild(element);
192 if (wantedNode != null) {
196 if (this.element.equals(element)) {