java.lang.Object
tech.guilhermekaua.spigotboot.inventoryapi.internal.state.MutableStateImpl<T>
Type Parameters:
T - the value type
All Implemented Interfaces:
IdentifiableToken, MutableState<T>, State<T>, StateToken

@Internal public final class MutableStateImpl<T> extends Object implements MutableState<T>, IdentifiableToken
Mutable per-context state token; values live in the session's StateStore. The initial value is computed per context on the first read; an explicitly stored null is kept (not re-initialized) via a private sentinel.
  • Constructor Details

    • MutableStateImpl

      public MutableStateImpl(@NotNull @NotNull View owner, @NotNull @NotNull TokenTable table, @NotNull @NotNull Function<ViewContext,T> initialValue)
      Creates and registers the token.
      Parameters:
      owner - the view declaring the token
      table - the owner's token table
      initialValue - the per-context initial value factory
      Throws:
      IllegalStateException - when the table is already frozen
  • Method Details

    • id

      public int id()
      Returns the table-assigned token id, the index into the per-session state store.
      Returns:
      the token id
    • tokenId

      public int tokenId()
      Specified by:
      tokenId in interface IdentifiableToken
      Returns:
      the token id assigned by the owning view's token table
    • get

      @Nullable public T get(@NotNull @NotNull ViewContext context)
      Description copied from interface: State
      Reads this token's value for the given context. Callable from any thread, but values are only coherent on the main thread.
      Specified by:
      get in interface State<T>
      Parameters:
      context - an active context of the owning view
      Returns:
      the current value, possibly null
    • set

      public void set(@NotNull @NotNull ViewContext context, @Nullable T value)
      Description copied from interface: MutableState
      Writes this token's value for the given context and marks watchers dirty.
      Specified by:
      set in interface MutableState<T>
      Parameters:
      context - an active context of the owning view
      value - the new value, possibly null
    • update

      public void update(@NotNull @NotNull ViewContext context, @NotNull @NotNull UnaryOperator<T> fn)
      Description copied from interface: MutableState
      Read-modify-write convenience: applies fn to the current value and stores the result. Main thread only.
      Specified by:
      update in interface MutableState<T>
      Parameters:
      context - an active context of the owning view
      fn - the function producing the new value from the current one; invoked exactly once on the current value. Side effects inside fn are safe (unlike SharedState.update(java.util.function.UnaryOperator<T>))