All Superinterfaces:
ObservableState, Observer<Boolean>

public interface State extends ObservableState
A class encapsulating a boolean state.
 State state = State.state();

 ObservableState observable = state.observable();

 observer.addConsumer(this::onStateChange);

 state.set(true);
 state.set(false);

 boolean value = state.is();
A factory for State instances.

Listener management (add/remove) and state modifications are thread-safe

See Also:
  • 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 an ObservableState notified when the state changes (default) or is set, depending on the Value.Notify policy
      Returns:
      an ObservableState notified each time the state changes or is set
      See Also:
    • value

      Value<Boolean> value()
      Returns:
      a Value instance representing this state
    • link

      void link(State originalState)
      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 as originalState.
      Parameters:
      originalState - the original state to link this state to
      Throws:
      IllegalStateException - in case the states are already linked or if a cycle is detected
      IllegalArgumentException - in case the original state is not valid according to this states validators
    • unlink

      void unlink(State originalState)
      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

      boolean addValidator(Value.Validator<? super Boolean> validator)
      Adds a validator to this State. 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

      boolean removeValidator(Value.Validator<? super Boolean> validator)
      Removes the given validator from this value
      Parameters:
      validator - the validator
      Returns:
      true if this value contained the specified validator
    • state

      static State state()
      Creates a new 'false' State instance.
      Returns:
      a new State instance
    • state

      static State state(boolean value)
      Creates a new State instance.
      Parameters:
      value - the initial state value
      Returns:
      a new State instance
    • builder

      static State.Builder builder()
      Returns:
      a new State.Builder instance
    • present

      static <T> ObservableState present(Observable<T> observable)
      Returns:
      an ObservableState active when the given observable has a value present, determined by Observable.optional().
    • contains

      static <T> State contains(ValueSet<T> valueSet, T value)
      Creates a State synchronized with whether the given value is contained in the given ValueSet. 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)
      The initial state reflects whether the value is currently contained in the set.

      The synchronization is maintained via a weak listener on the ValueSet, meaning:

      • As long as the returned State is reachable, the synchronization remains active
      • Once the returned State is no longer reachable and is garbage collected, the synchronization is automatically cleaned up
       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 set
      value - the value
      Returns:
      a state synchronized with the set's containment of the value
    • and

      static ObservableState and(ObservableState... observableStates)
      Creates a new ObservableState instance using AND.
      Parameters:
      observableStates - the state observers to base this state combination on
      Returns:
      a new ObservableState instance
    • and

      static ObservableState and(Collection<? extends ObservableState> observableStates)
      Creates a new ObservableState instance using AND.
      Parameters:
      observableStates - the state observers to base this state combination on
      Returns:
      a new ObservableState instance
    • or

      static ObservableState or(ObservableState... observableStates)
      Creates a new ObservableState instance using OR.
      Parameters:
      observableStates - the state observers to base this state combination on
      Returns:
      a new ObservableState instance
    • or

      static ObservableState or(Collection<? extends ObservableState> observableStates)
      Creates a new ObservableState instance using OR.
      Parameters:
      observableStates - the state observers to base this state combination on
      Returns:
      a new ObservableState instance
    • group

      static State.Group group(State... states)
      Creates a new State.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

      static State.Group group(Collection<State> states)
      Creates a new State.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: