generics and Comparable compiler warning


N

neuneudr

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

}
 
Ad

Advertisements

J

Joshua Cranmer

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.'
 
Ad

Advertisements

N

neuneudr

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



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
 

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

Top