Generics compiler warning

  • Thread starter Christian Pontesegger
  • Start date
C

Christian Pontesegger

Hi,

I was trying to create a generic compare method for objects of different
type. If both objects are of the same type and implement Comparable then
use the compareTo method. Else use their toString method and compare
their results.

My code looks like this:


private int compare(Object o1, Object o2) {
if (o1 == null)
return -1;
if (o2 == null)
return 11;

if ((o1.getClass().equals(o2.getClass())) && (o1 instanceof
Comparable))
return ((Comparable) o1).compareTo((Comparable) o2);

// compare anything else converted to string
return o1.toString().compareTo(o2.toString());
}


It works as expected, but I get this annoying compiler warning

"Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable<T> should be
parameterized"

How should I code my method in a clean way?

thanks
Christian Pontesegger
 
O

Oliver Wong

Christian Pontesegger said:
Hi,

I was trying to create a generic compare method for objects of different
type. If both objects are of the same type and implement Comparable then
use the compareTo method. Else use their toString method and compare their
results.

My code looks like this:


private int compare(Object o1, Object o2) {
if (o1 == null)
return -1;
if (o2 == null)
return 11;

if ((o1.getClass().equals(o2.getClass())) && (o1 instanceof
Comparable))
return ((Comparable) o1).compareTo((Comparable) o2);

// compare anything else converted to string
return o1.toString().compareTo(o2.toString());
}


It works as expected, but I get this annoying compiler warning

"Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable<T> should be
parameterized"

How should I code my method in a clean way?

Perhaps you could use method overloading, one for where the two
arguments implement Comparable, and one for where only one, or none of them,
implement Comparable.

<code>
private <T extends Comparable<T>> int compare(T o1, T o2) {
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 11;
}

if (o1.getClass().equals(o2.getClass())) {
return ((T) o1).compareTo((T) o2);
}
return compare((Object) o1, (Object) o2);
}

private int compare(Object o1, Object o2) {
if (o1 == null) {
return -1;
}
if (o2 == null) {
return 11;
}
// compare anything else converted to string
return o1.toString().compareTo(o2.toString());
}
</code>

- Oliver
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christian Pontesegger schreef:
Hi,

I was trying to create a generic compare method for objects of different
type. If both objects are of the same type and implement Comparable then
use the compareTo method. Else use their toString method and compare
their results.

My code looks like this:


private int compare(Object o1, Object o2) {
if (o1 == null)
return -1;
if (o2 == null)
return 11;

if ((o1.getClass().equals(o2.getClass())) && (o1 instanceof
Comparable))
return ((Comparable) o1).compareTo((Comparable) o2);

Here you use the non-generic Comparable. As you don’t know the classes,
so the only way out I said:
// compare anything else converted to string
return o1.toString().compareTo(o2.toString());
}


It works as expected, but I get this annoying compiler warning

"Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable<T> should be
parameterized"

How should I code my method in a clean way?

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEUdgSe+7xMGD3itQRAtqZAJsHp+hmfK40WHgFEYJxRY4bZv0UAwCfRD4D
QE3Bq+pGZ2H7ijhXO18O5D0=
=5efh
-----END PGP SIGNATURE-----
 
C

Christian Pontesegger

Perhaps you could use method overloading, one for where the two
arguments implement Comparable, and one for where only one, or none of
them, implement Comparable.

Simple as that. Thanks for opening my eyes :)

Christian
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top