add tree data structure
[jSite2.git] / src / net / pterodactylus / util / data / Node.java
diff --git a/src/net/pterodactylus/util/data/Node.java b/src/net/pterodactylus/util/data/Node.java
new file mode 100644 (file)
index 0000000..7737d50
--- /dev/null
@@ -0,0 +1,87 @@
+
+package net.pterodactylus.util.data;
+
+import java.util.Iterator;
+
+/**
+ * A node that can be stored in a {@link Tree}. A node has exactly one parent
+ * (which is <code>null</code> if the node is the {@link Tree#getRootNode()}
+ * of the tree) and an arbitrary amount of child nodes.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @param <E>
+ *            The type of the element to store
+ */
+public interface Node<E> extends Iterable<Node<E>> {
+
+       /**
+        * Returns the parent node of the node.
+        * 
+        * @return The parent node
+        */
+       public Node<E> getParent();
+
+       /**
+        * Returns the element that is stored in the node.
+        * 
+        * @return The node’s element
+        */
+       public E getElement();
+
+       /**
+        * Adds an element as a child to this node and returns the created node.
+        * 
+        * @param child
+        *            The child node’s element
+        * @return The created child node
+        */
+       public Node<E> addChild(E child);
+
+       /**
+        * Returns the number of children this node has.
+        * 
+        * @return The number of children
+        */
+       public int size();
+
+       /**
+        * Returns the child at the given index.
+        * 
+        * @param index
+        *            The index of the child
+        * @return The child at the given index
+        */
+       public Node<E> getChild(int index);
+
+       /**
+        * Remove the given child node from this node. If the given node is not a
+        * child of this node, nothing happens.
+        * 
+        * @param childNode
+        *            The child node to remove
+        */
+       public void removeChild(Node<E> childNode);
+
+       /**
+        * Removes the child node that contains the given element. The element in
+        * the node is checked using {@link Object#equals(Object)}.
+        * 
+        * @param child
+        *            The child element to remove
+        */
+       public void removeChild(E child);
+
+       /**
+        * Removes the child at the given index.
+        * 
+        * @param childIndex
+        *            The index of the child to remove
+        */
+       public void removeChild(int childIndex);
+
+       /**
+        * {@inheritDoc}
+        */
+       public Iterator<Node<E>> iterator();
+
+}
\ No newline at end of file