package net.pterodactylus.util.data;
+import java.util.Comparator;
import java.util.Iterator;
/**
* @param <E>
* The type of the element to store
*/
-public interface Node<E> extends Iterable<Node<E>> {
+public interface Node<E extends Comparable<E>> extends Iterable<Node<E>>, Comparable<Node<E>> {
/**
* Returns the parent node of the node.
*/
public Node<E> findChild(E element);
+ /**
+ * Sorts all children according to their natural order.
+ */
+ public void sortChildren();
+
+ /**
+ * Sorts all children with the given comparator.
+ *
+ * @param comparator
+ * The comparator used to sort the children
+ */
+ public void sortChildren(Comparator<Node<E>> comparator);
+
}
\ No newline at end of file
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;
* @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;
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
* @param <E>
* The type of the element to store
*/
-public class Tree<E> {
+public class Tree<E extends Comparable<E>> {
/** The root node of the tree. */
private final Node<E> rootNode = new NodeImpl<E>();