- 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 interfaceA state which combines a number of states, either ANDing or ORing those together when determining its own state.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 State.Combinationand(ObservableState... observableStates) Creates a newState.Combinationinstance usingConjunction.AND.static State.Combinationand(Collection<? extends ObservableState> observableStates) Creates a newState.Combinationinstance usingConjunction.AND.static State.Builderbuilder()static State.Combinationcombination(Conjunction conjunction, ObservableState... observableStates) Creates a newState.Combinationinstance.static State.Combinationcombination(Conjunction conjunction, Collection<? extends ObservableState> observableStates) Creates a newState.Combinationinstance.static 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 each time the state changesstatic State.Combinationor(ObservableState... observableStates) Creates a newState.Combinationinstance usingConjunction.OR.static State.Combinationor(Collection<? extends ObservableState> observableStates) Creates a newState.Combinationinstance usingConjunction.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.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 each time the state changes- Returns:
- an
ObservableStatenotified each time the state changes
-
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().
-
combination
Creates a newState.Combinationinstance.- Parameters:
conjunction- the conjunction to useobservableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
combination
static State.Combination combination(Conjunction conjunction, Collection<? extends ObservableState> observableStates) Creates a newState.Combinationinstance.- Parameters:
conjunction- the conjunction to useobservableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
and
Creates a newState.Combinationinstance usingConjunction.AND.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
and
Creates a newState.Combinationinstance usingConjunction.AND.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
or
Creates a newState.Combinationinstance usingConjunction.OR.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
or
Creates a newState.Combinationinstance usingConjunction.OR.- Parameters:
observableStates- the state observers to base this state combination on- Returns:
- a new
State.Combinationinstance
-
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:
-