Re: unchecked call to compareTo(T)
Mark wrote:
[...]
I cant do something like Comparable<Object> because then it only takes
objects..whereas I want I want it to accept anything that implements
Comparable.
How would accepting anything that implements Comparable be useful for you?
Having for example:
Comparable c1 = "";
Comparable c2 = 1;
both c1.compareTo(c2) and c2.compareTo(c1) will end in runtime exception.
You just can not simply compare things which are not comparable to each
other, even when they are all comparable to something (usually to
compatible type objects only).
The solution would be (and I suppose it's the best approach in your
case) applying more knowledge on data type held by Nodes. You can
achieve that by parameterizing both your code and Node class and than
use Node<T> and Comparable<T> (or somewhat like Comparable<? super T>)
instead.
You can also solve it using Comparator<Object> which will be able to
compare two distinct type objects applying your specific comparison
rules in compare() method. Unfortunately, this will most probably end
in _very awful_, not OO, implementation of compare(), similar to the
following one:
public int compare(Object o1, Object o2) {
if (o1 instanceof String)
return ((String)o1).compareTo((String)o2);
if (o1 instanceof Integer)
return ((Integer)o1).compareTo((Integer)o2);
...
So I presume you'll need to rethink a little your design again.
piotr