From 94b25a5e46b7522e7f21ab9f1a355981f477d850 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 12 Aug 2004 02:37:04 +0000 Subject: [PATCH] Added a REVERSE_COMPARABLE comparator, removed the obsolete STRING comparator. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1481 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/Comparators.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/Comparators.java b/projects/samskivert/src/java/com/samskivert/util/Comparators.java index dc54c96e..2086621f 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Comparators.java +++ b/projects/samskivert/src/java/com/samskivert/util/Comparators.java @@ -27,11 +27,30 @@ import java.util.Comparator; */ public class Comparators { + /** + * A comparator that can be used to reverse the results of another + * comparator. + */ + public static class ReversingComparator implements Comparator + { + public ReversingComparator (Comparator reversable) + { + _reversable = reversable; + } + + // documentation inherited from interface Comparator + public int compare (Object o1, Object o2) + { + return _reversable.compare(o2, o1); // switching the order + } + + protected Comparator _reversable; + } + /** * A comparator that compares {@link Comparable} instances. */ - public static final Comparator COMPARABLE = new Comparator() - { + public static final Comparator COMPARABLE = new Comparator() { public int compare (Object o1, Object o2) { if (o1 == o2) { @@ -45,7 +64,9 @@ public class Comparators }; /** - * A comparator that compares {@link String} instances. + * A comparator that imposes a reverse ordering on {@link Comparable} + * instances. */ - public static final Comparator STRING = COMPARABLE; + public static final Comparator REVERSE_COMPARABLE = + new ReversingComparator(COMPARABLE); }