I can't figure out how to do this. I'm converting my existing code to
use generics but I'm stuck on this Comparator. Any ideas of how to get
this to work.
The line:
return comparable1.compareTo(comparable2);
is the one giving me problems (javac output below). How do I get the "?
extends Object" part of comparable2?
Thanx. SD
-------------------
NotificationManagerComparator.java:29:
compareTo(capture of ? extends java.lang.Object) in
java.lang.Comparable<capture of ? extends java.lang.Object>
cannot be applied to
(java.lang.Comparable<capture of ? extends java.lang.Object>)
return comparable1.compareTo(comparable2);
^
-------------------
class NotificationManagerComparator
implements java.util.Comparator<Comparable<? extends Object>>
{
public int compare(
Comparable<? extends Object> comparable1,
Comparable<? extends Object> comparable2)
{
final Class class1 = comparable1.getClass();
final Class class2 = comparable2.getClass();
if(class1 == class2)
{
return comparable1.compareTo(comparable2);
}
else
return class1.getName().compareTo(class2.getName());
}
public boolean equals(Object obj)
{
if(getClass() == obj.getClass())
return this == obj;
else
return false;
}
}
Oliver Wong - 16 May 2006 23:04 GMT
>I can't figure out how to do this. I'm converting my existing code to
> use generics but I'm stuck on this Comparator. Any ideas of how to get
[quoted text clipped - 35 lines]
> return class1.getName().compareTo(class2.getName());
> }
Try changing your signature to:
public <T> int compare(Comparable<T> comparable1, Comparable<T> comparable2)
{
> public boolean equals(Object obj)
> {
[quoted text clipped - 3 lines]
> return false;
> }
This code essentially does the same thing as Object.equals' method, so
there's no point on overriding it.
> }
- Oliver
Roedy Green - 22 May 2006 06:32 GMT
On Tue, 16 May 2006 05:26:30 GMT, SD
<thow_away_email_111@personal.fishh2o.com> wrote, quoted or indirectly
quoted someone who said :
>compareTo(capture of ? extends java.lang.Object) in
that's not how you do it. You want to narrow the possibilities for
the parms. That's why you are using generics.
See http://mindprod.com/jgloss/comparable.html
http://mindprod.com/jgloss/comparator.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.