Regarding generics, I have been told that casting is bad, and there is usually some way to eliminate casting entirely, for the compiler to do its best work when checking my program. Adding a ChangeListener here to a ReadOnlyDoubleProperty seems to defeat that ideal. Is this a flaw in the API, or an exception to the rule, or is there actually some way to make this code look good?
nodeA.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue< ? extends Number > observable,
Number oldValue,
Number newValue ) {
final double ov = oldValue.doubleValue();
final double nv = newValue.doubleValue();
@SuppressWarnings("unchecked")
final ObservableValue<Double> ob = ( ObservableValue< Double > ) observable;
// do stuff
}
});
First, the most specific type parameter I can use on the ChangeListener is Number. I should be able to use Double! It's a DoubleExpression! Due to that issue, I have to unpack the arguments to the changed
method. Please help pare down the number of lines here. Lambdas could reduce the number of lines, but I'm asking specifically about the generics.