add sortChildren
[jSite2.git] / src / net / pterodactylus / util / data / NodeImpl.java
index 2d57876..266cea1 100644 (file)
@@ -2,6 +2,8 @@
 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;
 
@@ -12,7 +14,7 @@ import java.util.List;
  * @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;
@@ -87,13 +89,37 @@ class NodeImpl<E> implements Node<E> {
        /**
         * {@inheritDoc}
         */
-       public boolean hasChildNode(Node<E> childNode) {
+       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) {
@@ -148,6 +174,13 @@ class NodeImpl<E> implements Node<E> {
        /**
         * {@inheritDoc}
         */
+       public void removeAllChildren() {
+               children.clear();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public Iterator<Node<E>> iterator() {
                return children.iterator();
        }
@@ -168,4 +201,29 @@ class NodeImpl<E> implements Node<E> {
                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