I have a container which can persist values of different types implementing the Comparable
interface. I need to compare those values as follows:
UpperLowerContainer values;
//initializing the container
Comparable<?> upper = (Comparable<?>) values.getUpper();
Comparable<?> lower = (Comparable<?>) values.getLower();
if (upper.compareTo(lower) < 0){ //This is not compiled
//do some
}
The code is not compiled because of the obvious reason. The capture of the lower
's wildcard cannot be cast to the capture of upper
's wildacrd and vice versa.
But how can I solve that issue? Any idea?
UPD: The type of the values stored in the container are the same. There is a validation before storing them into it.