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:
@@ -49,7 +49,7 @@ public class Comparators
|
|||||||
/**
|
/**
|
||||||
* A comparator that compares {@link Comparable} instances.
|
* 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")
|
@SuppressWarnings("unchecked")
|
||||||
public int compare (Object o1, Object o2)
|
public int compare (Object o1, Object o2)
|
||||||
{
|
{
|
||||||
@@ -60,10 +60,24 @@ public class Comparators
|
|||||||
} else if (o2 == null) {
|
} else if (o2 == null) {
|
||||||
return -1;
|
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<Integer> = 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.
|
* Compares two bytes, returning 1, 0, or -1.
|
||||||
* TODO: remove when Java finally has this method in Byte.
|
* TODO: remove when Java finally has this method in Byte.
|
||||||
|
|||||||
Reference in New Issue
Block a user