Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics and Comparable

Reply
Thread Tools

Generics and Comparable

 
 
Bergholt
Guest
Posts: n/a
 
      11-27-2004
G'day all.

Porting some code over to Java 5 (which almost always makes the code
more concise - excellent), I've run into an unchecked warning which I
can't get rid of. It occurs in the conversion of the original code
below, which is basically a compare function which compares two Objects
(of arbitrary type). If the Objects are both of type Comparable, they
will be compared using compareTo, otherwise it's as defined:

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);
}
else if (!(obj1 instanceof Comparable) && !(obj2 instanceof
Comparable))
{
return 0;
}
else if (!(obj1 instanceof Comparable))
{
return -1;
}
else if (!(obj2 instanceof Comparable))
{
return 1;
}

// Can never get here.
assert false;
return 0;
}

This is because the 'natural' ordering when comparing a built-in
Comparable with null or a non-Comparable object is wrong for my
application.

The compile warning that I get is:
warning: [unchecked] unchecked call to compareTo(T) as a member of the
raw type java.lang.Comparable

Fair enough, because it is a call from the raw type. Can anyone think
of a nice way to get rid of this warning? I can obviously get by
suppressing the warning, but I'd rather not.

Thanks,

Bergholt.

 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      11-28-2004
"Bergholt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> G'day all.
>
> Porting some code over to Java 5 (which almost always makes the code
> more concise - excellent), I've run into an unchecked warning which I
> can't get rid of. It occurs in the conversion of the original code
> below, which is basically a compare function which compares two Objects
> (of arbitrary type). If the Objects are both of type Comparable, they
> will be compared using compareTo, otherwise it's as defined:
>
> 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.

Also, why is getValue(MySet) returning an Object
and not something narrower?

/snip/
> The compile warning that I get is:
> warning: [unchecked] unchecked call to compareTo(T) as a member of the
> raw type java.lang.Comparable
>
> Fair enough, because it is a call from the raw type. Can anyone think
> of a nice way to get rid of this warning? I can obviously get by
> suppressing the warning, but I'd rather not.
>
> Thanks,
>
> Bergholt.
>



 
Reply With Quote
 
 
 
 
Bergholt
Guest
Posts: n/a
 
      11-29-2004
xarax wrote:
> "Bergholt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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.

 
Reply With Quote
 
Bergholt
Guest
Posts: n/a
 
      11-30-2004
xarax wrote:
> "Bergholt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > If the Objects are both of type Comparable, they
> > will be compared using compareTo, otherwise it's as defined:
> >
> > 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.


The old compareTo takes an Object parameter, so it makes no difference
either way. The new compareTo takes a parameter of type T, but of
course I can't check instanceof Comparable<Comparable> because the type
doesn't exist at run-time, so there's no way to get the compiler to
realise that the code is completely type safe.

> Also, why is getValue(MySet) returning an Object
> and not something narrower?


Because it can return null, or Double or Integer, or any user-defined
type. This is used for sorting the rows of a table on an arbitrary
column, and the only superclass I can guarantee each value in each
column will have is Object.

Bergholt.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics, Comparable and asbtract classes Bowbaq Java 1 03-25-2010 04:17 PM
generics and Comparable compiler warning neuneudr@yahoo.fr Java 2 01-14-2009 04:03 PM
Generics puzzle with implements Comparable Roedy Green Java 6 05-22-2008 08:41 PM
Generics & Comparable Ron Albright Java 3 10-06-2006 09:14 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments