I'm converting an application to use Java 1.5 and have found the following method:
/**
* Compare two Comparables, treat nulls as -infinity.
* @param o1
* @param o2
* @return -1 if o1<o2, 0 if o1==o2, 1 if o1>o2
*/
protected static int nullCompare(Comparable o1, Comparable o2) {
if (o1 == null) {
if (o2 == null) {
return 0;
} else {
return -1;
}
} else if (o2 == null) {
return 1;
} else {
return o1.compareTo(o2);
}
}
Ideally I would like to make the method take two Comparables of the same type, is it possible to convert this and how?
I thought the following would do the trick:
protected static <T extends Comparable> int nullCompare(T o1, T o2) {
but it has failed to get rid of a warning in IntelliJ "Unchecked call to 'compareTo(T)' as a member of raw type 'java.lang.Comparable'" on the line:
return o1.compareTo(o2);