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