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);
}
}
Oliver Wong - 05 Oct 2006 21:58 GMT
> 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.
[quoted text clipped - 39 lines]
> }
> }
This may sound harsh, but it looks like your "C" class is useless, as
this code seems to achieve what you're trying to do in your CTest class:
Date d = new Date();
Timestamp t = new Timestamp(3);
d.compareTo(t);
t.compareTo(d);
- Oliver
Ron Albright - 06 Oct 2006 00:37 GMT
> This may sound harsh, but it looks like your "C" class is useless, as
> this code seems to achieve what you're trying to do in your CTest class:
[quoted text clipped - 3 lines]
> d.compareTo(t);
> t.compareTo(d);
You're right it would be if there wasn't more to the actual class I'm
implementing. I just stripped it down to the essentials to illustrate the
problem here.
Hendrik Maryns - 06 Oct 2006 10:14 GMT
Ron Albright schreef:
> 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):
With this C:
public class C<T extends Comparable<? super T>>
implements Comparable<C<? extends T>>
{
private T val;
public C(T val)
{
this.val = val;
}
public T getValue()
{
return val;
}
public int compareTo(C<? extends T> obj)
{
return getValue().compareTo(obj.getValue());
}
}
the following works:
import java.util.Date;
import java.sql.Timestamp;
public class Ctest
{
public final void testCompareTo()
{
C<Date> dt = new C<Date>(new Date());
C<Date> dt2 = new C<Date>(new Date());
C<Timestamp> ts = new C<Timestamp>(new Timestamp(3));
C<Date> tsAsDate = new C<Date>(new Timestamp(4));
dt.compareTo(ts);
dt.compareTo(dt2);
// ts.compareTo(dt);
tsAsDate.compareTo(dt);
}
}
Of course the commented out line does not work, since Date is not a
subclass of TimeStamp, but rather the inverse.
You might want to consider whether you really need to know whether
TimeStamps are TimeStamps, or rather just Dates...
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html