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 boolean hasChild(Node<E> childNode) {
91 return children.contains(childNode);
97 public boolean hasChild(E element) {
98 for (Node<E> childNode: children) {
99 if (childNode.getElement().equals(element)) {
109 public int getIndexOfChild(Node<E> childNode) {
111 for (Node<E> node: children) {
112 if (node.equals(childNode)) {
123 public int getIndexOfChild(E element) {
125 for (Node<E> node: children) {
126 if (node.getElement().equals(element)) {
137 public void removeChild(Node<E> childNode) {
138 children.remove(childNode);
144 public void removeChild(E child) {
145 for (Node<E> childNode: children) {
146 if (child.equals(childNode.getElement())) {
147 children.remove(childNode);
156 public void removeChild(int childIndex) {
157 children.remove(childIndex);
163 public void removeAllChildren() {
170 public Iterator<Node<E>> iterator() {
171 return children.iterator();
177 public Node<E> findChild(E element) {
178 for (Node<E> childNode: children) {
179 Node<E> wantedNode = childNode.findChild(element);
180 if (wantedNode != null) {
184 if (this.element.equals(element)) {