Generics and Comparable

Discussion in 'Java' started by Bergholt, Nov 27, 2004.

  1. Bergholt

    Bergholt Guest

    G'day all.

    Porting some code over to Java 5 (which almost always makes the code
    more concise - excellent), I've run into an unchecked warning which I
    can't get rid of. It occurs in the conversion of the original code
    below, which is basically a compare function which compares two Objects
    (of arbitrary type). If the Objects are both of type Comparable, they
    will be compared using compareTo, otherwise it's as defined:

    int compare(MySet set1, MySet set2)
    {
    Object obj1 = this.getValue(set1);
    Object obj2 = this.getValue(set2);

    if (obj1 instanceof Comparable && obj2 instanceof Comparable)
    {
    Comparable cmp1 = (Comparable)obj1;
    return cmp1.compareTo(obj2);
    }
    else if (!(obj1 instanceof Comparable) && !(obj2 instanceof
    Comparable))
    {
    return 0;
    }
    else if (!(obj1 instanceof Comparable))
    {
    return -1;
    }
    else if (!(obj2 instanceof Comparable))
    {
    return 1;
    }

    // Can never get here.
    assert false;
    return 0;
    }

    This is because the 'natural' ordering when comparing a built-in
    Comparable with null or a non-Comparable object is wrong for my
    application.

    The compile warning that I get is:
    warning: [unchecked] unchecked call to compareTo(T) as a member of the
    raw type java.lang.Comparable

    Fair enough, because it is a call from the raw type. Can anyone think
    of a nice way to get rid of this warning? I can obviously get by
    suppressing the warning, but I'd rather not.

    Thanks,

    Bergholt.
    Bergholt, Nov 27, 2004
    #1
    1. Advertising

  2. Bergholt

    xarax Guest

    "Bergholt" <> wrote in message
    news:...
    > G'day all.
    >
    > Porting some code over to Java 5 (which almost always makes the code
    > more concise - excellent), I've run into an unchecked warning which I
    > can't get rid of. It occurs in the conversion of the original code
    > below, which is basically a compare function which compares two Objects
    > (of arbitrary type). If the Objects are both of type Comparable, they
    > will be compared using compareTo, otherwise it's as defined:
    >
    > int compare(MySet set1, MySet set2)
    > {
    > Object obj1 = this.getValue(set1);
    > Object obj2 = this.getValue(set2);
    >
    > if (obj1 instanceof Comparable && obj2 instanceof Comparable)
    > {
    > Comparable cmp1 = (Comparable)obj1;
    > return cmp1.compareTo(obj2);


    Why did you not cast obj2 to a Comparable and
    pass that to compareTo()? You've already tested
    it with instanceof.

    Also, why is getValue(MySet) returning an Object
    and not something narrower?

    /snip/
    > The compile warning that I get is:
    > warning: [unchecked] unchecked call to compareTo(T) as a member of the
    > raw type java.lang.Comparable
    >
    > Fair enough, because it is a call from the raw type. Can anyone think
    > of a nice way to get rid of this warning? I can obviously get by
    > suppressing the warning, but I'd rather not.
    >
    > Thanks,
    >
    > Bergholt.
    >
    xarax, Nov 28, 2004
    #2
    1. Advertising

  3. Bergholt

    Bergholt Guest

    xarax wrote:
    > "Bergholt" <> wrote in message
    > news:...
    > >
    > > int compare(MySet set1, MySet set2)
    > > {
    > > Object obj1 = this.getValue(set1);
    > > Object obj2 = this.getValue(set2);
    > >
    > > if (obj1 instanceof Comparable && obj2 instanceof Comparable)
    > > {
    > > Comparable cmp1 = (Comparable)obj1;
    > > return cmp1.compareTo(obj2);

    >
    > Why did you not cast obj2 to a Comparable and
    > pass that to compareTo()? You've already tested
    > it with instanceof.


    Well, the compareTo method in the (pre-1.5) Comparable interface only
    takes an Object, so what's the point in casting it? Still, it makes no
    difference to the warning. In 5, compareTo(Comparable<?>) is a method
    of Comparable<Comparable>. But this still says the cast (to
    Comparable<Comparable>) is unchecked, and of course you can't check
    that at runtime anyway.

    > Also, why is getValue(MySet) returning an Object
    > and not something narrower?


    Because this is sorting values as displayed in a JTable. getValue may
    return null, or any standard type (String, Double, Integer), or a
    user-defined type. The only common supertype of these is Object. Not
    Comparable, because some of them may not _be_ comparable. So they
    should go at one end of the list.

    Bergholt.
    Bergholt, Nov 29, 2004
    #3
  4. Bergholt

    Bergholt Guest

    xarax wrote:
    > "Bergholt" <> wrote in message
    > news:...
    > > If the Objects are both of type Comparable, they
    > > will be compared using compareTo, otherwise it's as defined:
    > >
    > > int compare(MySet set1, MySet set2)
    > > {
    > > Object obj1 = this.getValue(set1);
    > > Object obj2 = this.getValue(set2);
    > >
    > > if (obj1 instanceof Comparable && obj2 instanceof Comparable)
    > > {
    > > Comparable cmp1 = (Comparable)obj1;
    > > return cmp1.compareTo(obj2);

    >
    > Why did you not cast obj2 to a Comparable and
    > pass that to compareTo()? You've already tested
    > it with instanceof.


    The old compareTo takes an Object parameter, so it makes no difference
    either way. The new compareTo takes a parameter of type T, but of
    course I can't check instanceof Comparable<Comparable> because the type
    doesn't exist at run-time, so there's no way to get the compiler to
    realise that the code is completely type safe.

    > Also, why is getValue(MySet) returning an Object
    > and not something narrower?


    Because it can return null, or Double or Integer, or any user-defined
    type. This is used for sorting the rows of a table on an arbitrary
    column, and the only superclass I can guarantee each value in each
    column will have is Object.

    Bergholt.
    Bergholt, Nov 30, 2004
    #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. Juergen Berchtel
    Replies:
    1
    Views:
    5,967
    John C. Bollinger
    May 20, 2005
  2. Ron Albright

    Generics & Comparable

    Ron Albright, Oct 5, 2006, in forum: Java
    Replies:
    3
    Views:
    392
    Hendrik Maryns
    Oct 6, 2006
  3. Roedy Green
    Replies:
    6
    Views:
    781
    Roedy Green
    May 22, 2008
  4. Replies:
    2
    Views:
    383
  5. Bowbaq
    Replies:
    1
    Views:
    966
    Bowbaq
    Mar 25, 2010
Loading...

Share This Page