Class ReactiveState<V>

java.lang.Object
space.maxus.flare.react.ReactiveState<V>
Type Parameters:
V - The type of the value this state holds
All Implemented Interfaces:
ReactiveNotifier<V>
Direct Known Subclasses:
ComposableReactiveState

@ThreadSafe public class ReactiveState<V> extends Object implements ReactiveNotifier<V>
A ReactiveState is a thread-safe wrapper over a value, that can be hooked to, so you can listen to value updates.
  • Constructor Details

    • ReactiveState

      public ReactiveState(@Nullable V origin)
      Constructs a reactive state with the given value
      Parameters:
      origin - Initial value of this state. May be null.
    • ReactiveState

      public ReactiveState()
      Constructs a reactive state with no value
  • Method Details

    • getSubscriberList

      @NotNull public @NotNull SubscriberList<V> getSubscriberList()
      Description copied from interface: ReactiveNotifier
      Returns this notifier's subscriber list
      Specified by:
      getSubscriberList in interface ReactiveNotifier<V>
      Returns:
      This notifier's subscriber list
    • connect

      public void connect(@NotNull @NotNull ReactiveState<V> other)
      Connects this state to another state, meaning that whenever the other state changes, this state will be updated with its new value.
      Parameters:
      other - The other state to connect to
    • subscribeUpdate

      public <S extends ReactiveSubscriber<V>> void subscribeUpdate(S subscriber)
      Subscribes a listener to this state, additionally populating it with current value
      Type Parameters:
      S - The type of the subscriber
      Parameters:
      subscriber - The subscriber to subscribe to this state
    • set

      public void set(@Nullable V newValue)
      Sets the value of this state
      Parameters:
      newValue - New value to be set
    • setOpt

      public void setOpt(@NotNull @NotNull Optional<V> optValue)
      Sets the value of this state to the given optional value or null
      Parameters:
      optValue - Value to be set
    • get

      @NotNull public V get()
      Gets the value of this state.
      Returns:
      The value of this state
      Throws:
      NullPointerException - if value is null
    • getOrNull

      @Nullable public V getOrNull()
      Gets the value of this state or null.
      Returns:
      The value of this state or null
    • getOptional

      @NotNull public @NotNull Optional<V> getOptional()
      Gets the value of this state as an optional.
      Returns:
      The value of this state as an optional
    • toString

      public String toString()
      Overrides:
      toString in class Object