I have a class meant for holding numeric values that could be changed by the user through the GUI. The GUI has a TextField (of JavaFX) where they could write the desirable value.
Since I wanted this class to be flexible I made it using generics like this:
public class ConfigurableNumericField <T extends Number> {
private T value;
//...
public void setValue(T value){
this.value = value;
}
}
And I create an object like this:
ConfigurableNumericField <Integer> foo = new ConfigurableNumericField <> ( /*...*/ );
The problem is that the TextField returns a CharacterSequence (that could be casted to String). BUT even if all classes that inherit from Number have the valueOf method, Number does not have it.
So when I try to setValue() the String from the TextField to the class itself. I get a java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number.
How can I do this?
When generics you can make things so generic that they are not meaningful anymore.
In your case you need a setValue which will take a String so you can convert it to the appropriate type. The problem with this is there is no way to do this genericly as the generic type of
foois not available at runtime.I would have two subclasses, one for integers which uses a
Longand another which uses aDoubleorBigDecimal. This will support most of the Number types you are likely to need.