简体   繁体   中英

Using EasyBind with subclass of NumberProperty

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 ie Double , Integer , ...

Is there some way to avoid doubleValue() ?

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: ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM