From f52356f8351e099a243ee7b470a37f449bc88b1b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 26 May 2008 23:32:19 +0200 Subject: [PATCH] add sortChildren --- src/net/pterodactylus/util/data/Node.java | 16 ++++++++++++++- src/net/pterodactylus/util/data/NodeImpl.java | 29 ++++++++++++++++++++++++++- src/net/pterodactylus/util/data/Tree.java | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/net/pterodactylus/util/data/Node.java b/src/net/pterodactylus/util/data/Node.java index 560fc34..950ed99 100644 --- a/src/net/pterodactylus/util/data/Node.java +++ b/src/net/pterodactylus/util/data/Node.java @@ -1,6 +1,7 @@ package net.pterodactylus.util.data; +import java.util.Comparator; import java.util.Iterator; /** @@ -12,7 +13,7 @@ import java.util.Iterator; * @param * The type of the element to store */ -public interface Node extends Iterable> { +public interface Node> extends Iterable>, Comparable> { /** * Returns the parent node of the node. @@ -152,4 +153,17 @@ public interface Node extends Iterable> { */ public Node 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> comparator); + } \ No newline at end of file diff --git a/src/net/pterodactylus/util/data/NodeImpl.java b/src/net/pterodactylus/util/data/NodeImpl.java index 785fa1f..266cea1 100644 --- a/src/net/pterodactylus/util/data/NodeImpl.java +++ b/src/net/pterodactylus/util/data/NodeImpl.java @@ -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 * The type of the element to store */ -class NodeImpl implements Node { +class NodeImpl> implements Node { /** The parent node of this node. */ private final Node parentNode; @@ -199,4 +201,29 @@ class NodeImpl implements Node { return null; } + /** + * {@inheritDoc} + */ + public void sortChildren() { + Collections.sort(children); + } + + /** + * {@inheritDoc} + */ + public void sortChildren(Comparator> comparator) { + Collections.sort(children, comparator); + } + + // + // INTERFACE Comparable + // + + /** + * {@inheritDoc} + */ + public int compareTo(Node otherNode) { + return element.compareTo(otherNode.getElement()); + } + } \ No newline at end of file diff --git a/src/net/pterodactylus/util/data/Tree.java b/src/net/pterodactylus/util/data/Tree.java index 5cbb990..634b8f7 100644 --- a/src/net/pterodactylus/util/data/Tree.java +++ b/src/net/pterodactylus/util/data/Tree.java @@ -27,7 +27,7 @@ package net.pterodactylus.util.data; * @param * The type of the element to store */ -public class Tree { +public class Tree> { /** The root node of the tree. */ private final Node rootNode = new NodeImpl(); -- 2.7.4