I may fiddle around with this, but here's one that works.

Provide a method for getting a Comparable Comparator in a typesafe way.
I had to change the Comparator to be untyped.
Hopefully this doesn't hork anything. Oh, now I worry...


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2664 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2009-12-04 23:59:42 +00:00
parent 0a5255f421
commit bfe3ef5096
+16 -2
View File
@@ -49,7 +49,7 @@ public class Comparators
/**
* A comparator that compares {@link Comparable} instances.
*/
public static final Comparator<Object> COMPARABLE = new Comparator<Object>() {
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<Object>)o1).compareTo(o2); // null-free
return ((Comparable)o1).compareTo(o2); // null-free
}
};
/**
* Returns the Comparator for Comparables, properly cast.
*
* <p>This example illustates the type-safe way to obtain a natural-ordering Comparator:
* <pre>
* Comparator&lt;Integer&gt; = Comparators.comparable();
* </pre>
*/
@SuppressWarnings("unchecked")
public static final <T extends Comparable> Comparator<T> comparable ()
{
return (Comparator<T>) COMPARABLE;
}
/**
* Compares two bytes, returning 1, 0, or -1.
* TODO: remove when Java finally has this method in Byte.