- All Superinterfaces:
ObservableState,Observer<Boolean>
A class encapsulating a boolean state.
A factory for
State state = State.state();
ObservableState observable = state.observable();
observer.addConsumer(this::onStateChange);
state.set(true);
state.set(false);
boolean value = state.is();
State instances.
Listener management (add/remove) and state modifications are thread-safe
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceBuilds aStateinstance.static interfaceAState.Groupdeactivates all other states when a state in the group is activated. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddValidator(Value.Validator<? super Boolean> validator) Adds a validator to thisState.static ObservableStateand(ObservableState... observableStates) Creates a newObservableStateinstance using AND.static ObservableStateand(Collection<? extends ObservableState> observableStates) Creates a newObservableStateinstance using AND.static State.Builderbuilder()static <T> Statestatic State.GroupCreates a newState.Groupinstance, which guarantees that only a single state within the group is active at a timestatic State.Groupgroup(Collection<State> states) Creates a newState.Groupinstance, which guarantees that only a single state within the group is active at a timevoidCreates a bidirectional link between this and the given original state, so that changes in one are reflected in the other.Returns anObservableStatenotified when the state changes (default) or is set, depending on theValue.Notifypolicystatic ObservableStateor(ObservableState... observableStates) Creates a newObservableStateinstance using OR.static ObservableStateor(Collection<? extends ObservableState> observableStates) Creates a newObservableStateinstance using OR.static <T> ObservableStatepresent(Observable<T> observable) booleanremoveValidator(Value.Validator<? super Boolean> validator) Removes the given validator from this valuevoidset(boolean value) Sets the valuestatic Statestate()Creates a new 'false'Stateinstance.static Statestate(boolean value) Creates a newStateinstance.voidtoggle()Toggles this statevoidUnlinks this state from the given original statevalue()Methods inherited from interface is.codion.common.reactive.state.ObservableState
addConsumer, addListener, addWeakConsumer, addWeakListener, is, not, observer, removeConsumer, removeListener, removeWeakConsumer, removeWeakListener
-
Method Details
-
set
void set(boolean value) Sets the value- Parameters:
value- the new value
-
toggle
void toggle()Toggles this state -
observable
ObservableState observable()Returns anObservableStatenotified when the state changes (default) or is set, depending on theValue.Notifypolicy- Returns:
- an
ObservableStatenotified each time the state changes or is set - See Also:
-
value
- Returns:
- a
Valueinstance representing this state
-
link
Creates a bidirectional link between this and the given original state, so that changes in one are reflected in the other. Note that after a call to this method this state is the same asoriginalState.- Parameters:
originalState- the original state to link this state to- Throws:
IllegalStateException- in case the states are already linked or if a cycle is detectedIllegalArgumentException- in case the original state is not valid according to this states validators
-
unlink
Unlinks this state from the given original state- Parameters:
originalState- the original value to unlink from this one- Throws:
IllegalStateException- in case the states are not linked
-
addValidator
Adds a validator to thisState. Adding the same validator again has no effect.- Parameters:
validator- the validator- Returns:
- true if this value did not already contain the specified validator
- Throws:
IllegalArgumentException- in case the current value is invalid according to the validator
-
removeValidator
Removes the given validator from this value- Parameters:
validator- the validator- Returns:
- true if this value contained the specified validator
-
state
Creates a new 'false'Stateinstance.- Returns:
- a new
Stateinstance
-
state
Creates a newStateinstance.- Parameters:
value- the initial state value- Returns:
- a new
Stateinstance
-
builder
- Returns:
- a new
State.Builderinstance
-
present
- Returns:
- an
ObservableStateactive when the given observable has a value present, determined byObservable.optional().
-
contains
Creates aStatesynchronized with whether the given value is contained in the givenValueSet. The state is bidirectionally synchronized:- When the value is added to or removed from the set, the state is updated accordingly
- When the state is set to
true, the value is added to the set (if not already present) - When the state is set to
false, the value is removed from the set (if present)
ValueSet<String> tags = ValueSet.valueSet(); State containsImportant = State.contains(tags, "important"); // State → Set containsImportant.set(true); assertTrue(tags.contains("important")); // Set → State tags.remove("important"); assertFalse(containsImportant.is());- Type Parameters:
T- the value type- Parameters:
valueSet- the value setvalue- the value- Returns:
- a state synchronized with the set's containment of the value
-
and
Creates a newObservableStateinstance using AND.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
ObservableStateinstance
-
and
Creates a newObservableStateinstance using AND.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
ObservableStateinstance
-
or
Creates a newObservableStateinstance using OR.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
ObservableStateinstance
-
or
Creates a newObservableStateinstance using OR.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
ObservableStateinstance
-
group
Creates a newState.Groupinstance, which guarantees that only a single state within the group is active at a time- Parameters:
states- the states to add to the group initially, not required- Returns:
- a new
State.Groupinstance - See Also:
-
group
Creates a newState.Groupinstance, which guarantees that only a single state within the group is active at a time- Parameters:
states- the states to add to the group initially- Returns:
- a new
State.Groupinstance - See Also:
-