When's current API evaluates both 'then' and 'othwerise' on creation. As a binding equivalent of a ternary expression, where only one of the results is evaluated, When is missing the functionality for evaluating the 'then' and 'otherwise' "on demand". See JDK-8089579.
One option is adding public API that takes a Supplier<T> for 'then' and 'otherwise'. The result would be an ObjectBinding<T> that holds the value returned by Supplier#get at the moment of evaluation of the condition.
This approach allows an alternative to
boolean b = cond.get() ? getBooleanForTrue() : getBooleanForFalse();
cond.addListener((obs, ov, nv) -> b = nv ? getBooleanForTrue() : getBooleanForFalse());
by using
BooleanBinding b = new When(cond).then( () -> getBooleanForTrue() ).otherwise( () -> getBooleanForFalse() );
Something to think about would be if this new API can mix with the current one. That is, if 'then' takes a Supplier<T>, must 'otherwise' also take a Supplier<T>, or can it take an ObservableObjectValue<T> or T?
One option is adding public API that takes a Supplier<T> for 'then' and 'otherwise'. The result would be an ObjectBinding<T> that holds the value returned by Supplier#get at the moment of evaluation of the condition.
This approach allows an alternative to
boolean b = cond.get() ? getBooleanForTrue() : getBooleanForFalse();
cond.addListener((obs, ov, nv) -> b = nv ? getBooleanForTrue() : getBooleanForFalse());
by using
BooleanBinding b = new When(cond).then( () -> getBooleanForTrue() ).otherwise( () -> getBooleanForFalse() );
Something to think about would be if this new API can mix with the current one. That is, if 'then' takes a Supplier<T>, must 'otherwise' also take a Supplier<T>, or can it take an ObservableObjectValue<T> or T?
- relates to
-
JDK-8089579 'then()' is executed while 'when()' produces 'false'
- Open
-
JDK-8199514 Refactor binding.When
- In Progress