- 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 interface
Builds aState
instance.static interface
A state which combines a number of states, either ANDing or ORing those together when determining its own state.static interface
AState.Group
deactivates all other states when a state in the group is activated. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addValidator
(Value.Validator<? super Boolean> validator) Adds a validator to thisState
.static State.Combination
and
(ObservableState... observableStates) Creates a newState.Combination
instance usingConjunction.AND
.static State.Combination
and
(Collection<? extends ObservableState> observableStates) Creates a newState.Combination
instance usingConjunction.AND
.static State.Builder
builder()
static State.Combination
combination
(Conjunction conjunction, ObservableState... observableStates) Creates a newState.Combination
instance.static State.Combination
combination
(Conjunction conjunction, Collection<? extends ObservableState> observableStates) Creates a newState.Combination
instance.static State.Group
Creates a newState.Group
instance, which guarantees that only a single state within the group is active at a timestatic State.Group
group
(Collection<State> states) Creates a newState.Group
instance, which guarantees that only a single state within the group is active at a timevoid
Creates a bidirectional link between this and the given original state, so that changes in one are reflected in the other.Returns anObservableState
notified each time the state changesstatic State.Combination
or
(ObservableState... observableStates) Creates a newState.Combination
instance usingConjunction.OR
.static State.Combination
or
(Collection<? extends ObservableState> observableStates) Creates a newState.Combination
instance usingConjunction.OR
.boolean
removeValidator
(Value.Validator<? super Boolean> validator) Removes the given validator from this valuevoid
set
(boolean value) Sets the valuestatic State
state()
Creates a new 'false'State
instance.static State
state
(boolean value) Creates a newState
instance.void
toggle()
Toggles this statevoid
Unlinks 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 anObservableState
notified each time the state changes- Returns:
- an
ObservableState
notified each time the state changes
-
value
- Returns:
- a
Value
instance 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'State
instance.- Returns:
- a new
State
instance
-
state
Creates a newState
instance.- Parameters:
value
- the initial state value- Returns:
- a new
State
instance
-
builder
- Returns:
- a new
State.Builder
instance
-
combination
Creates a newState.Combination
instance.- Parameters:
conjunction
- the conjunction to useobservableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
combination
static State.Combination combination(Conjunction conjunction, Collection<? extends ObservableState> observableStates) Creates a newState.Combination
instance.- Parameters:
conjunction
- the conjunction to useobservableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
and
Creates a newState.Combination
instance usingConjunction.AND
.- Parameters:
observableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
and
Creates a newState.Combination
instance usingConjunction.AND
.- Parameters:
observableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
or
Creates a newState.Combination
instance usingConjunction.OR
.- Parameters:
observableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
or
Creates a newState.Combination
instance usingConjunction.OR
.- Parameters:
observableStates
- the state observers to base this state combination on- Returns:
- a new
State.Combination
instance
-
group
Creates a newState.Group
instance, 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.Group
instance - See Also:
-
group
Creates a newState.Group
instance, 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.Group
instance - See Also:
-