From 07800d16629fdba9f7d86d241528a145d9dc7c9b Mon Sep 17 00:00:00 2001 From: ray Date: Fri, 11 Nov 2005 01:17:56 +0000 Subject: [PATCH] - There is a standard Comparator for reversing Comparables, so let's use that everywhere; deprecated Comparators.REVERSE_COMPARABLE. - Added a note that the ReversingComparator is old news, too, but we're not ready to go 1.5 everywhere... - Added Comparators.LEXICAL_CASE_INSENSITIVE which treats all sorted objects as Strings that are sorted case insensitively. - Have TableSorter use standard Comparators. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1733 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/TableSorter.java | 17 ++------- .../java/com/samskivert/util/Comparators.java | 37 ++++++++++++++++--- .../java/com/samskivert/util/QuickSort.java | 2 +- .../samskivert/util/SortableArrayList.java | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/swing/TableSorter.java b/projects/samskivert/src/java/com/samskivert/swing/TableSorter.java index 716e9bc8..348f3b26 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/TableSorter.java +++ b/projects/samskivert/src/java/com/samskivert/swing/TableSorter.java @@ -13,6 +13,8 @@ import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.*; +import com.samskivert.util.Comparators; + /** * TableSorter is a decorator for TableModels; adding sorting * functionality to a supplied TableModel. TableSorter does @@ -75,17 +77,6 @@ public class TableSorter extends AbstractTableModel { private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED); - public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() { - public int compare(Object o1, Object o2) { - return ((Comparable) o1).compareTo(o2); - } - }; - public static final Comparator LEXICAL_COMPARATOR = new Comparator() { - public int compare(Object o1, Object o2) { - return o1.toString().compareTo(o2.toString()); - } - }; - public static JTable createSortedTable (TableModel model) { TableSorter sorter = new TableSorter(model); @@ -227,9 +218,9 @@ public class TableSorter extends AbstractTableModel { return comparator; } if (Comparable.class.isAssignableFrom(columnType)) { - return COMPARABLE_COMAPRATOR; + return Comparators.COMPARABLE; } - return LEXICAL_COMPARATOR; + return Comparators.LEXICAL_CASE_INSENSITIVE; } private Row[] getViewToModel() { diff --git a/projects/samskivert/src/java/com/samskivert/util/Comparators.java b/projects/samskivert/src/java/com/samskivert/util/Comparators.java index 2086621f..fd223a31 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Comparators.java +++ b/projects/samskivert/src/java/com/samskivert/util/Comparators.java @@ -30,6 +30,8 @@ public class Comparators /** * A comparator that can be used to reverse the results of another * comparator. + * TODO: deprecate this when we more globally move to 1.5: + * @use java.util.Collections.reverseOrder(Comparator c); */ public static class ReversingComparator implements Comparator { @@ -49,24 +51,49 @@ public class Comparators /** * A comparator that compares {@link Comparable} instances. + * Can you believe this isn't defined somewhere in the standard + * java libraries? */ public static final Comparator COMPARABLE = new Comparator() { public int compare (Object o1, Object o2) { - if (o1 == o2) { + if (o1 == o2) { // catches null == null return 0; - } - if (o1 == null) { + } else if (o1 == null) { return 1; + } else if (o2 == null) { + return -1; } - return ((Comparable)o1).compareTo(o2); + return ((Comparable)o1).compareTo(o2); // null-free + } + }; + + /** + * A comparator that compares the toString() value of all objects + * case insensitively. + */ + public static final Comparator LEXICAL_CASE_INSENSITIVE = new Comparator() { + public int compare (Object o1, Object o2) + { + if (o1 == o2) { // catches null == null + return 0; + } else if (o1 == null) { + return 1; + } else if (o2 == null) { + return -1; + } + // now that we've filtered all nulls, compare the toString()s + return String.CASE_INSENSITIVE_ORDER.compare( + o1.toString(), o2.toString()); } }; /** * A comparator that imposes a reverse ordering on {@link Comparable} * instances. + * + * @deprecated use java.util.Collections.reverseOrder() */ public static final Comparator REVERSE_COMPARABLE = - new ReversingComparator(COMPARABLE); + java.util.Collections.reverseOrder(); } diff --git a/projects/samskivert/src/java/com/samskivert/util/QuickSort.java b/projects/samskivert/src/java/com/samskivert/util/QuickSort.java index d4cb076f..b17e81e3 100644 --- a/projects/samskivert/src/java/com/samskivert/util/QuickSort.java +++ b/projects/samskivert/src/java/com/samskivert/util/QuickSort.java @@ -352,7 +352,7 @@ public class QuickSort */ public static void rsort (ArrayList a) { - sort(a, Comparators.REVERSE_COMPARABLE); + sort(a, java.util.Collections.reverseOrder()); } /** diff --git a/projects/samskivert/src/java/com/samskivert/util/SortableArrayList.java b/projects/samskivert/src/java/com/samskivert/util/SortableArrayList.java index 418ee6a2..5bfb26d5 100644 --- a/projects/samskivert/src/java/com/samskivert/util/SortableArrayList.java +++ b/projects/samskivert/src/java/com/samskivert/util/SortableArrayList.java @@ -61,7 +61,7 @@ public class SortableArrayList extends AbstractList */ public void rsort () { - sort(Comparators.REVERSE_COMPARABLE); + sort(java.util.Collections.reverseOrder()); } /**