generics and Comparable compiler warning

Discussion in 'Java' started by neuneudr@yahoo.fr, Jan 14, 2009.

  1. Guest

    Hi again,

    I'm using the following class, which works fine for its purpose
    but gives a compiler warning.

    The following class declaration works:

    class Progress<V extends Comparable> implements
    Comparable<Progress<V>>

    but the following doesn't work :

    class Progress<V extends Comparable> implements Comparable<Progress<V
    extends Comparable>>

    Yet it's something "close" to that I guess I'd need to get rid of
    the compiler warning.

    Here's the class (simply remove the @Nullable annotation to compile if
    you're not using IntelliJ/@NotNull/@Nullable annotations).

    What do I need to do to (cleanly) get rid of the compiler warning?

    import org.jetbrains.annotations.Nullable;

    /**
    * The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
    * NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
    * "OPTIONAL" in this document are to be interpreted as described in
    * RFC 2119.
    *
    */
    public class Progress<V extends Comparable> implements
    Comparable<Progress<V>> {

    public final int progress;
    public final @Nullable V v;

    /**
    * You MUST either give a null object and a progress between 0 and
    100
    * (both included) or a non null object and a -1 progress value.
    *
    * @param progress
    * @param v
    */
    public Progress( final int progress, final @Nullable V v ) {
    if ( (( progress < 0 || progress > 100) && v == null )
    || (( progress >= 0 && progress <= 100 && v != null ))
    || (( v != null && progress != -1 )) )
    throwContractFailure();
    this.progress = progress;
    this.v = v;
    }

    public int compareTo( final Progress<V> o ) {
    if ( progress != -1 || o.progress != -1 ) {
    // sorting on values where there's still progress going on
    doesn't
    // really matter
    return progress > o.progress ? 1 : progress < o.progress ?
    -1 : 0;
    } else {
    // THE FOLLOWING LINE GENERATES A COMPILER WARNING:
    // "...uses unchecked or unsafe operation"
    return v.compareTo(o.v);
    }
    }

    private void throwContractFailure() {
    final String err = "The two arguments you gave are incorrect.
    Please"
    + "re-read this class's Javadoc.";
    throw new IllegalArgumentException( err );
    }

    }
     
    , Jan 14, 2009
    #1
    1. Advertising

  2. wrote:
    > Hi again,
    >
    > I'm using the following class, which works fine for its purpose
    > but gives a compiler warning.


    You never gave us the compiler warning, but I can guess...

    > The following class declaration works:
    >
    > class Progress<V extends Comparable> implements
    > Comparable<Progress<V>>


    Strictly speaking, the line you want is:
    class Progress<V extends Comparable<? super V>> implements
    Comparable<Progress<V>>

    I'm guessing the warning you see is trying to inform that you are using
    the raw type `Comparable.'

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jan 14, 2009
    #2
    1. Advertising

  3. Guest

    On Jan 14, 3:30 pm, Joshua Cranmer <> wrote:
    > wrote:
    > > Hi again,

    >
    > > I'm using the following class, which works fine for its purpose
    > > but gives a compiler warning.

    >
    > You never gave us the compiler warning, but I can guess...
    >
    > > The following class declaration works:

    >
    > > class Progress<V extends Comparable> implements
    > > Comparable<Progress<V>>

    >
    > Strictly speaking, the line you want is:
    > class Progress<V extends Comparable<? super V>> implements
    > Comparable<Progress<V>>


    Excellent! I still have quite a lot to learn when
    it comes to implementing class using generics (as
    opposed to simply using them).

    > I'm guessing the warning you see is trying to inform that you are using
    > the raw type `Comparable.'


    Oops my bad. Exactly, that was the error message.

    Thanks a lot,

    Driss
     
    , Jan 14, 2009
    #3
    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,119
    Bergholt
    Nov 30, 2004
  2. Christian Pontesegger

    Generics compiler warning

    Christian Pontesegger, Apr 27, 2006, in forum: Java
    Replies:
    3
    Views:
    6,539
    Christian Pontesegger
    May 2, 2006
  3. Ron Albright

    Generics & Comparable

    Ron Albright, Oct 5, 2006, in forum: Java
    Replies:
    3
    Views:
    447
    Hendrik Maryns
    Oct 6, 2006
  4. Roedy Green
    Replies:
    6
    Views:
    834
    Roedy Green
    May 22, 2008
  5. Bowbaq
    Replies:
    1
    Views:
    1,030
    Bowbaq
    Mar 25, 2010
Loading...

Share This Page