public class SwitchBindings extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
SwitchBindings.SwitchBuilder<T,R> |
| Constructor and Description |
|---|
SwitchBindings() |
| Modifier and Type | Method and Description |
|---|---|
static <T,R> SwitchBindings.SwitchBuilder<T,R> |
switchBinding(ObservableValue<T> observable,
Class<R> bindingType)
Creates builder for a binding that works like a switch-case in java.
|
public static <T,R> SwitchBindings.SwitchBuilder<T,R> switchBinding(ObservableValue<T> observable, Class<R> bindingType)
Creates builder for a binding that works like a switch-case in java.
Example:
IntegerProperty base = new SimpleIntegerProperty();
ObservableValue<String> result = switchBinding(base, String.class)
.bindCase(3, i -> "three")
.bindCase(10, i -> "ten")
.bindCase(1, i -> "one")
.bindDefault(() -> "nothing")
.build();
this is the equivalent without observables:
int base = ...;
switch(base) {
case 3 :
return "three";
case 10:
return "ten";
case 1:
return "one";
default:
return "nothing";
}
There are two differences between this switch binding and the switch statement in java:
In the java switch statement only a limited number of types can be used. This binding has no such limitation. You can use every type in the observable that has a properly overwritten Object.equals(Object) and Object.hashCode() method.
There is no “fall through” and therefore no “break” is needed. Only the callback for the matching case is executed.
See the switch documentation for more information.
observable - the base observable that is used in the switch statementbindingType - the type of the created observable.T - the generic type of the base observable.R - the generic type of the returned observable.