S
SD
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;
}
}
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;
}
}