add tree data structure
[jSite2.git] / src / net / pterodactylus / util / data / NodeImpl.java
diff --git a/src/net/pterodactylus/util/data/NodeImpl.java b/src/net/pterodactylus/util/data/NodeImpl.java
new file mode 100644 (file)
index 0000000..a30f76e
--- /dev/null
@@ -0,0 +1,120 @@
+
+package net.pterodactylus.util.data;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Implementation of the {@link Node} interface.
+ * 
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @param <E>
+ *            The type of the element to store
+ */
+class NodeImpl<E> implements Node<E> {
+
+       /** The parent node of this node. */
+       private final Node<E> parentNode;
+
+       /** The element contained in this node. */
+       private final E element;
+
+       /** The child nodes of this node. */
+       private final List<Node<E>> children = new ArrayList<Node<E>>();
+
+       /**
+        * Creates a new root node.
+        */
+       NodeImpl() {
+               this.parentNode = null;
+               this.element = null;
+       }
+
+       /**
+        * Creates a new node with the given parent and element.
+        * 
+        * @param parentNode
+        *            The parent of this node
+        * @param element
+        *            The element of this node
+        */
+       NodeImpl(Node<E> parentNode, E element) {
+               if ((parentNode == null) || (element == null)) {
+                       throw new NullPointerException("null is not allowed as parent or element");
+               }
+               this.parentNode = parentNode;
+               this.element = element;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public Node<E> getParent() {
+               return parentNode;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public E getElement() {
+               return element;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public Node<E> addChild(E child) {
+               Node<E> childNode = new NodeImpl<E>(this, child);
+               children.add(childNode);
+               return childNode;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int size() {
+               return children.size();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public Node<E> getChild(int childIndex) {
+               return children.get(childIndex);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void removeChild(Node<E> childNode) {
+               children.remove(childNode);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void removeChild(E child) {
+               for (Node<E> childNode: children) {
+                       if (child.equals(childNode.getElement())) {
+                               children.remove(childNode);
+                               break;
+                       }
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void removeChild(int childIndex) {
+               children.remove(childIndex);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public Iterator<Node<E>> iterator() {
+               return children.iterator();
+       }
+
+}
\ No newline at end of file