Re: Discussion of why java.lang.Number does not implement Comparable
Sideswipe wrote:
Actually, what I had in mind was this:
public abstract class Number implements Comparable ....
// dont actually implement compareTo()
public class Float extends Number {
public int compareTo(Object otherNumber) {
if(otherNumber == Float.NaN)
throw new ArithmaticException("NaN is not comparable");
if(otherNumber instanceof Integer)
return this.floatValue() -
((Integer)otherNumber).intValue();
}
}
This is not the slickest example, but that's what I had in mind. And,
yes, the specific case is that I have a mixed collection of Numbers
that I need to order. What I argue for is to let all the subclasses
sort their own comparison problems out. I can see from further
postings that the issue is contentious enough to simply allow Sun's
implementation to stand.
Remember that you can sort using a Comparator<Number>. All you need is a
total order that makes sense in your context, among the things you
expect to find in your collection. You can limit it to the types you
intend to mix. If you don't expect to see a NaN, you can throw an
exception if you hit one.
Patricia