Using EasyBind with subclass of NumberProperty

176 Views Asked by At

Before introducing EasyBind -

DoubleBinding contentHeight = Bindings.createDoubleBinding(
    () -> getHeight() - getInsets().getTop() - getInsets().getBottom(),
    heightProperty(), insetsProperty());

After introducing EasyBind -

Binding<Double> contentHeight = EasyBind.combine(
    heightProperty(), insetsProperty(),
    (h, i) -> h.doubleValue() - i.getTop() - i.getBottom());

I'm somewhat uncomfortable regarding doubleValue() part. Every time I combine some subclass of NumberProperty, EasyBind passes Number instead of i.e. Double, Integer, ...

Is there some way to avoid doubleValue()?

1

There are 1 best solutions below

1
On BEST ANSWER

It's not EasyBind that's causing the need for you to call doubleValue() - it's a consequence of the JavaFX API.

EasyBind.combine() has a parameter list (ObservableValue<A>, ObservableValue<B>, BiFunction<A,B,R>), and returns a Binding<R>. For the first parameter you're passing in a DoubleProperty. The issue is that DoubleProperty (somewhat counter-intuitively) implements ObservableValue<Number>, not ObservableValue<Double>. The combine method invokes your BiFunction on the result of calling getValue() on the first two parameters: i.e. it calls getValue() on your DoubleProperty, which returns a Number, not a Double. Thus your BiFunction has to be a BiFunction<Number, Insets, Double> (mapping a Number and Insets to a Double).

You could consider implementing your heightProperty as a ObjectProperty<Double>, which would allow you to omit the call to doubleValue(); but it might make other parts of your application harder to code (specifically if you have other bindings to the height). I'm not sure I would consider the need to call doubleValue() a problem.