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 hasChildNode(Node<E> childNode) {
91 return children.contains(childNode);
97 public int getIndexOfChild(Node<E> childNode) {
99 for (Node<E> node: children) {
100 if (node.equals(childNode)) {
111 public int getIndexOfChild(E element) {
113 for (Node<E> node: children) {
114 if (node.getElement().equals(element)) {
125 public void removeChild(Node<E> childNode) {
126 children.remove(childNode);
132 public void removeChild(E child) {
133 for (Node<E> childNode: children) {
134 if (child.equals(childNode.getElement())) {
135 children.remove(childNode);
144 public void removeChild(int childIndex) {
145 children.remove(childIndex);
151 public void removeAllChildren() {
158 public Iterator<Node<E>> iterator() {
159 return children.iterator();
165 public Node<E> findChild(E element) {
166 for (Node<E> childNode: children) {
167 Node<E> wantedNode = childNode.findChild(element);
168 if (wantedNode != null) {
172 if (this.element.equals(element)) {