Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Generics and Comparator best practices

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should Foo be written like version 1 or version 2 below? What are the considerations for choosing between the two? Are there any upsides or downsides for one or the other?

class Foo implements Comparator{
...
public int compare(Object o1, Object o2) {
...
}
}

class Foo implements Comparator<Foo>{
...
public int compare(Foo foo1, Foo foo2) {
...
}
}

Opinions welcome.

Brian
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics will give you more typesafety.

Your second example looks a little bit strange, though: a Comparator typically shouldn't compare instances of itself - that sounds more like a Comparable.
 
Brian Buckley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. As you point out, my original example should have read like below. The second has the type safety. So why ever use the first anymore?

class Foo implements Comparable{
...
public int compareTo(Object o) {
...
}
}

class Foo implements Comparable<Foo>{
...
public int compareTo(Foo foo) {
...
}
}

Brian
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, to me the only reason not to use generics in that case would be that the code had to compile with Java 1.4...
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the signature of compareTo prior to Tiger is 'public int compareTo(Object o)', which should answer your question.
In Tiger it's been changed to 'public int compareTo(T o)' allowing your second option.

You have to ask yourself whether you will ever have your class in a situation where it may have to be compared to other types that cannot be resolved to Foo in some way and what you want to do if that happens.
Will you be content with the runtime error generated by the JVM or would you like to do your own error handling inside the method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic