java - Can I convert the following code to use generics? -
i'm converting application use java 1.5 , have found following method:
/** * compare 2 comparables, treat nulls -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 make method take 2 comparables of same type, possible convert , how?
i thought following trick:
protected static <t extends comparable> int nullcompare(t o1, t o2) {
but has failed rid of warning in intellij "unchecked call 'compareto(t)' member of raw type 'java.lang.comparable'" on line:
return o1.compareto(o2);
change to:
protected static <t extends comparable<t>> int nullcompare(t o1, t o2) {
you need because comparable generic type.
Comments
Post a Comment