xarax wrote:
> "Bergholt" <> wrote in message
> news: oups.com...
> >
> > int compare(MySet set1, MySet set2)
> > {
> > Object obj1 = this.getValue(set1);
> > Object obj2 = this.getValue(set2);
> >
> > if (obj1 instanceof Comparable && obj2 instanceof Comparable)
> > {
> > Comparable cmp1 = (Comparable)obj1;
> > return cmp1.compareTo(obj2);
>
> Why did you not cast obj2 to a Comparable and
> pass that to compareTo()? You've already tested
> it with instanceof.
Well, the compareTo method in the (pre-1.5) Comparable interface only
takes an Object, so what's the point in casting it? Still, it makes no
difference to the warning. In 5, compareTo(Comparable<?>) is a method
of Comparable<Comparable>. But this still says the cast (to
Comparable<Comparable>) is unchecked, and of course you can't check
that at runtime anyway.
> Also, why is getValue(MySet) returning an Object
> and not something narrower?
Because this is sorting values as displayed in a JTable. getValue may
return null, or any standard type (String, Double, Integer), or a
user-defined type. The only common supertype of these is Object. Not
Comparable, because some of them may not _be_ comparable. So they
should go at one end of the list.
Bergholt.
|