whitespace fixups
[jSite2.git] / src / net / pterodactylus / util / data / NodeImpl.java
index a30f76e..0f719c8 100644 (file)
@@ -2,17 +2,19 @@
 package net.pterodactylus.util.data;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 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> {
+class NodeImpl<E extends Comparable<E>> implements Node<E> {
 
        /** The parent node of this node. */
        private final Node<E> parentNode;
@@ -33,7 +35,7 @@ class NodeImpl<E> implements Node<E> {
 
        /**
         * Creates a new node with the given parent and element.
-        * 
+        *
         * @param parentNode
         *            The parent of this node
         * @param element
@@ -87,6 +89,65 @@ class NodeImpl<E> implements Node<E> {
        /**
         * {@inheritDoc}
         */
+       public Node<E> getChild(E element) {
+               for (Node<E> childNode : children) {
+                       if (childNode.getElement().equals(element)) {
+                               return childNode;
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public boolean hasChild(Node<E> childNode) {
+               return children.contains(childNode);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public boolean hasChild(E element) {
+               for (Node<E> childNode : children) {
+                       if (childNode.getElement().equals(element)) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getIndexOfChild(Node<E> childNode) {
+               int childIndex = 0;
+               for (Node<E> node : children) {
+                       if (node.equals(childNode)) {
+                               return childIndex;
+                       }
+                       childIndex++;
+               }
+               return -1;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getIndexOfChild(E element) {
+               int childIndex = 0;
+               for (Node<E> node : children) {
+                       if (node.getElement().equals(element)) {
+                               return childIndex;
+                       }
+                       childIndex++;
+               }
+               return -1;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void removeChild(Node<E> childNode) {
                children.remove(childNode);
        }
@@ -95,7 +156,7 @@ class NodeImpl<E> implements Node<E> {
         * {@inheritDoc}
         */
        public void removeChild(E child) {
-               for (Node<E> childNode: children) {
+               for (Node<E> childNode : children) {
                        if (child.equals(childNode.getElement())) {
                                children.remove(childNode);
                                break;
@@ -113,8 +174,56 @@ class NodeImpl<E> implements Node<E> {
        /**
         * {@inheritDoc}
         */
+       public void removeAllChildren() {
+               children.clear();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public Iterator<Node<E>> iterator() {
                return children.iterator();
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       public Node<E> findChild(E element) {
+               for (Node<E> childNode : children) {
+                       Node<E> wantedNode = childNode.findChild(element);
+                       if (wantedNode != null) {
+                               return wantedNode;
+                       }
+               }
+               if (this.element.equals(element)) {
+                       return this;
+               }
+               return null;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void sortChildren() {
+               Collections.sort(children);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void sortChildren(Comparator<Node<E>> comparator) {
+               Collections.sort(children, comparator);
+       }
+
+       //
+       // INTERFACE Comparable
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public int compareTo(Node<E> otherNode) {
+               return element.compareTo(otherNode.getElement());
+       }
+
 }
\ No newline at end of file