R
Rhino
I am getting this warning:
Type safety: The method compareTo(Object) belongs to the raw type
Comparable.
References to generic type Comparable<T> should be parameterized
in the following code but I'm not sure how to rewrite the code so that the
error goes away (I don't want to disable the warning in the compiler). The
'//<===' comment indicates exactly where the warning occurs:
=================================================================
public class SimpleRange {
public static int NUMBER_OF_ELEMENTS = 2;
Comparable lowValue = null;
Comparable highValue = null;
public SimpleRange(Comparable firstObject, Comparable secondObject) {
if (firstObject == null) {
throw new IllegalArgumentException("The first Object cannot be
null.");
}
if (secondObject == null) {
throw new IllegalArgumentException("The second Object cannot be
null.");
}
/*
* Since only Comparable objects are permitted by this constructor,
compare them
* and store the lower one in the lowValue class variable. Store the
other one in the
* highValue class variable.
*/
try {
if (firstObject.compareTo(secondObject) > 0) { //<=== Warning
occurs here
this.highValue = firstObject;
this.lowValue = secondObject;
} else {
this.lowValue = firstObject;
this.highValue = secondObject;
}
} catch (ClassCastException cc_excp) {
throw new IllegalArgumentException("The first Object, " +
firstObject.toString() + ", belongs to class " +
firstObject.getClass().getName() + ". The second Object, " +
secondObject.toString() + ", belongs to class " +
secondObject.getClass().getName() + ". Both Objects implement the Comparable
interface but they cannot be compared with each other.");
}
}
}
=================================================================
How do I revise my code to prevent this warning?
I revised the method signature to say:
public SimpleRange(Comparable<Object> firstObject, Comparable<Object>
secondObject)
and this stopped the warning but then I wasn't sure how to invoke the
constructor; old invocations like:
SimpleRange myRange = new SimpleRange("cat", "dog");
stopped working when I did that.
I'm afraid I don't follow generics entirely yet....
Type safety: The method compareTo(Object) belongs to the raw type
Comparable.
References to generic type Comparable<T> should be parameterized
in the following code but I'm not sure how to rewrite the code so that the
error goes away (I don't want to disable the warning in the compiler). The
'//<===' comment indicates exactly where the warning occurs:
=================================================================
public class SimpleRange {
public static int NUMBER_OF_ELEMENTS = 2;
Comparable lowValue = null;
Comparable highValue = null;
public SimpleRange(Comparable firstObject, Comparable secondObject) {
if (firstObject == null) {
throw new IllegalArgumentException("The first Object cannot be
null.");
}
if (secondObject == null) {
throw new IllegalArgumentException("The second Object cannot be
null.");
}
/*
* Since only Comparable objects are permitted by this constructor,
compare them
* and store the lower one in the lowValue class variable. Store the
other one in the
* highValue class variable.
*/
try {
if (firstObject.compareTo(secondObject) > 0) { //<=== Warning
occurs here
this.highValue = firstObject;
this.lowValue = secondObject;
} else {
this.lowValue = firstObject;
this.highValue = secondObject;
}
} catch (ClassCastException cc_excp) {
throw new IllegalArgumentException("The first Object, " +
firstObject.toString() + ", belongs to class " +
firstObject.getClass().getName() + ". The second Object, " +
secondObject.toString() + ", belongs to class " +
secondObject.getClass().getName() + ". Both Objects implement the Comparable
interface but they cannot be compared with each other.");
}
}
}
=================================================================
How do I revise my code to prevent this warning?
I revised the method signature to say:
public SimpleRange(Comparable<Object> firstObject, Comparable<Object>
secondObject)
and this stopped the warning but then I wasn't sure how to invoke the
constructor; old invocations like:
SimpleRange myRange = new SimpleRange("cat", "dog");
stopped working when I did that.
I'm afraid I don't follow generics entirely yet....