R
Ron Albright
I'm confused. This may not even make sense but in trying to figure it out
I think I've fried my brain beyond rational thought.
Can you generisize a class C such that the compareTo will only work for
any 2 generic types T1 & T2 given a common comparable ancestor of T1 & T2?
Something like this (only this doesn't work):
public class C<T extends Comparable<? super T>>
implements Comparable<DataElement<T>>
{
private T _val;
public C(T val)
{
_val = val;
}
public T getValue()
{
return(_val);
}
public int compareTo(C<T> obj)
{
return (getValue().compareTo(obj.getValue()));
}
}
where
import java.util.Date;
import java.sql.Timestamp;
public class CTest
{
@Test
public final void testCompareTo()
{
C<Date> dt = new C<Date>(new Date());
C<Timestamp> ts = new C<Timestamp>(new Timestamp(3));
dt.compareTo(ts);
ts.compareTo(dt);
}
}
I think I've fried my brain beyond rational thought.
Can you generisize a class C such that the compareTo will only work for
any 2 generic types T1 & T2 given a common comparable ancestor of T1 & T2?
Something like this (only this doesn't work):
public class C<T extends Comparable<? super T>>
implements Comparable<DataElement<T>>
{
private T _val;
public C(T val)
{
_val = val;
}
public T getValue()
{
return(_val);
}
public int compareTo(C<T> obj)
{
return (getValue().compareTo(obj.getValue()));
}
}
where
import java.util.Date;
import java.sql.Timestamp;
public class CTest
{
@Test
public final void testCompareTo()
{
C<Date> dt = new C<Date>(new Date());
C<Timestamp> ts = new C<Timestamp>(new Timestamp(3));
dt.compareTo(ts);
ts.compareTo(dt);
}
}