While we're at it, I wonder if it might be better to remove a lot of the
"checking" going on inside ComparableComparator.compare.  I'm thinking
something as simple as:

public int compare(Object o1, Object o2) {
  return ((Comparable)o1).compareTo(o2);
}

and letting Java and/or the underlying Comparable objects throw NPE or CCE
as necessary.

There is some precedence here.  java.util.Collections.reverseOrder()
returns a Comparator whose compare() method is implemented as:

public int compare(Object o1, Object o2) {
 Comparable c1 = (Comparable)o1;
 Comparable c2 = (Comparable)o2;
 return -c1.compareTo(c2);
}

(By the way, I think the way we throw ClassCastException in the case of
null in the current implementation is misleading.  The
Comparable/Comparator JavaDocs don't say "throw CCE if you can't do the
comparision", but rather, "throw CCE if the argument's *type* prevents you
from doing the comparision.")

On Wed, 8 Jan 2003, Stephen Colebourne wrote:

> From: "Rodney Waldhoff" <[EMAIL PROTECTED]>
>
> > I suggest that we replace the block above with simply:
> >
> >   return ((Comparable)o1).compareTo(o2);
>
> +1
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to