Generics & Comparable

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);
}
}
 
O

Oliver Wong

Ron Albright said:
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);
}
}

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
 
R

Ron Albright

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);

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.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFJh5le+7xMGD3itQRAp4oAKCDDZBihxGXokic5zLYKB1gGwGgXwCfb6yg
Y9cPiEaV5zc0sJ388XYZG3Q=
=4goT
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top