EasyBind icon indicating copy to clipboard operation
EasyBind copied to clipboard

Would it make senese for FlatMapProperty to provide orElse(...) implementations?

Open ryanjaeb opened this issue 11 years ago • 2 comments

Hi,

If there's a better place to post inquiries like this, please let me know. Would it make sense for FlatMapProperty to have orElse(...) implementations that return Property<?> types? Consider the following usage of EasyBind:

private final ObjectProperty<SomeModel> model = new SimpleObjectProperty<>();

private BooleanProperty disabled;
private Property<Boolean> modelDisabled;

{
    disabled = new SimpleBooleanProperty();
    modelDisabled = EasyBind.monadic(model).selectProperty(SomeModel::disabledProperty);
    disabled.bindBidirectional(modelDisabled);
}

Assuming the above is a reasonable thing to do, I'd like to handle the null cases more explicitly. For example:

private final ObjectProperty<SomeModel> model = new SimpleObjectProperty<>();

private BooleanProperty disabled;
private Property<Boolean> modelDisabled;

{
    disabled = new SimpleBooleanProperty();
    modelDisabled = EasyBind.monadic(model).selectProperty(SomeModel::disabledProperty).orElse(false);
    disabled.bindBidirectional(modelDisabled);
}

Basically I'm trying to do bidirectional select bindings.

I'm also curious with regards to how a swap of SomeModel would be handled in my first example. I assume it would be treated as a change in the selected property, so the disabled property would be updated from modelDisabled. Is that correct?

ryanjaeb avatar Nov 29 '14 23:11 ryanjaeb

Hi Ryan,

I think we could define orElse(T) returning a Property<T> in a meaningful way. The task is to define the behavior of set(T) on the returned property. We could define p.orElse(t0).set(t) to be the same as p.set(t).

Anyway, doesn't the following serve your purpose equally well?

modelDisabled = EasyBind.monadic(model)
        .map(SomeModel::disabledProperty)
        .orElse(new SimpleBooleanProperty(false))
        .selectProperty(Function::identity);

The only problem would arise if you do modelDisabled.set(true) while model was null. That would overwrite your default value false to true.

TomasMikula avatar Nov 30 '14 22:11 TomasMikula

Hey Tomas,

Sorry for the slow response. Yes, the snippet you posted accomplishes what I was trying to do. Overwriting the default value isn't a big deal. I would normally have the state bound to some type of input control (ie: checkbox) and would ensure that control is disabled when model is null.

It doesn't make any difference to me if orElse(...) is able to return a Property<T> given the example you suggested.

BTW, EasyBind is a fantastic library!

ryanjaeb avatar Dec 16 '14 23:12 ryanjaeb