Generics and Comparable

B

Bergholt

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.
 
X

xarax

Bergholt said:
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.
 
B

Bergholt

xarax said:
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
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.
 
B

Bergholt

xarax said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top