diff --git a/src/java/com/samskivert/util/Comparators.java b/src/java/com/samskivert/util/Comparators.java index eb59ddb0..d0eaca69 100644 --- a/src/java/com/samskivert/util/Comparators.java +++ b/src/java/com/samskivert/util/Comparators.java @@ -49,7 +49,7 @@ public class Comparators /** * A comparator that compares {@link Comparable} instances. */ - public static final Comparator COMPARABLE = new Comparator() { + public static final Comparator COMPARABLE = new Comparator() { @SuppressWarnings("unchecked") public int compare (Object o1, Object o2) { @@ -60,10 +60,24 @@ public class Comparators } else if (o2 == null) { return -1; } - return ((Comparable)o1).compareTo(o2); // null-free + return ((Comparable)o1).compareTo(o2); // null-free } }; + /** + * Returns the Comparator for Comparables, properly cast. + * + *

This example illustates the type-safe way to obtain a natural-ordering Comparator: + *

+     *    Comparator<Integer> = Comparators.comparable();
+     * 
+ */ + @SuppressWarnings("unchecked") + public static final Comparator comparable () + { + return (Comparator) COMPARABLE; + } + /** * Compares two bytes, returning 1, 0, or -1. * TODO: remove when Java finally has this method in Byte.