Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics & Comparable

Reply
Thread Tools

Generics & Comparable

 
 
Ron Albright
Guest
Posts: n/a
 
      10-05-2006
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);
}
}
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      10-05-2006

"Ron Albright" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Ron Albright
Guest
Posts: n/a
 
      10-05-2006
On Thu, 05 Oct 2006 20:58:55 +0000, Oliver Wong wrote:

>
> 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.
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      10-06-2006
-----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+7xMGD3itQRAp4oAKCDDZBihxGXokic5zLYKB 1gGwGgXwCfb6yg
Y9cPiEaV5zc0sJ388XYZG3Q=
=4goT
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics, Comparable and asbtract classes Bowbaq Java 1 03-25-2010 04:17 PM
generics and Comparable compiler warning neuneudr@yahoo.fr Java 2 01-14-2009 04:03 PM
Generics puzzle with implements Comparable Roedy Green Java 6 05-22-2008 08:41 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Generics and Comparable Bergholt Java 3 11-30-2004 03:31 AM



Advertisments