Generics & Comparable

Discussion in 'Java' started by Ron Albright, Oct 5, 2006.

  1. Ron Albright

    Ron Albright Guest

    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);
    }
    }
     
    Ron Albright, Oct 5, 2006
    #1
    1. Advertising

  2. Ron Albright

    Oliver Wong Guest

    "Ron Albright" <> wrote in message
    news:p...
    > 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
     
    Oliver Wong, Oct 5, 2006
    #2
    1. Advertising

  3. Ron Albright

    Ron Albright Guest

    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.
     
    Ron Albright, Oct 6, 2006
    #3
  4. -----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-----
     
    Hendrik Maryns, Oct 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bergholt

    Generics and Comparable

    Bergholt, Nov 27, 2004, in forum: Java
    Replies:
    3
    Views:
    6,055
    Bergholt
    Nov 30, 2004
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,008
    John C. Bollinger
    May 20, 2005
  3. Roedy Green
    Replies:
    6
    Views:
    804
    Roedy Green
    May 22, 2008
  4. Replies:
    2
    Views:
    398
  5. Bowbaq
    Replies:
    1
    Views:
    991
    Bowbaq
    Mar 25, 2010
Loading...

Share This Page