Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Generics compiler warning

Thread view: 
Christian Pontesegger - 27 Apr 2006 17:29 GMT
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
Oliver Wong - 27 Apr 2006 18:15 GMT
> Hi,
>
[quoted text clipped - 26 lines]
>
> 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
Christian Pontesegger - 02 May 2006 17:48 GMT
> "Christian Pontesegger" <christian.pontesegger@web.de> wrote in message
>> 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.

>    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
Hendrik Maryns - 28 Apr 2006 09:53 GMT
Christian Pontesegger schreef:
> Hi,
>
[quoted text clipped - 14 lines]
>        Comparable))
>             return ((Comparable) o1).compareTo((Comparable) o2);

Here you use the non-generic Comparable.  As you don’t know the classes,
there is no way you can use Comparable<SomeClass>, so the only way out I
see is     @SuppressWarnings("unchecked")

>   // compare anything else converted to string
>   return o1.toString().compareTo(o2.toString());
[quoted text clipped - 7 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.