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

@Internal public final class SharedStateImpl<T> extends Object implements SharedState<T>, IdentifiableToken
Shared state token: one atomic value per view singleton, readable and writable from any thread. Every successful write invokes the flush hook so open sessions of the owning view repaint their watchers.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SharedStateImpl(@NotNull View owner, @NotNull TokenTable table, T initialValue)
    Creates and registers the token.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    flushHook(@Nullable Runnable hook)
    Wires the engine flush callback invoked after every set(T); assigned once by the engine on the view's first open.
    boolean
    Returns true if the engine has already wired a flush hook on this token.
    get()
    Returns the current shared value.
    int
    id()
    Returns the table-assigned token id.
    void
    set(T newValue)
    Atomically replaces the shared value and triggers a watcher flush for every open session of the owning view.
    int
     
    void
    update(@NotNull UnaryOperator<T> fn)
    Atomically updates the shared value with a compare-and-set loop; fn may run more than once under contention and must be side-effect free.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SharedStateImpl

      public SharedStateImpl(@NotNull @NotNull View owner, @NotNull @NotNull TokenTable table, @Nullable T initialValue)
      Creates and registers the token.
      Parameters:
      owner - the view declaring the token
      table - the owner's token table
      initialValue - the initial shared value, possibly null
      Throws:
      IllegalStateException - when the table is already frozen
  • Method Details

    • id

      public int id()
      Returns the table-assigned token id.
      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
    • flushHook

      @Internal public void flushHook(@Nullable @Nullable Runnable hook)
      Wires the engine flush callback invoked after every set(T); assigned once by the engine on the view's first open.
      Parameters:
      hook - the flush callback, or null to clear
    • flushHookWired

      @Internal public boolean flushHookWired()
      Returns true if the engine has already wired a flush hook on this token.
      Returns:
      true when a flush hook is present
    • get

      @Nullable public T get()
      Description copied from interface: SharedState
      Returns the current shared value.
      Specified by:
      get in interface SharedState<T>
      Returns:
      the current value, possibly null
    • set

      public void set(@Nullable T newValue)
      Description copied from interface: SharedState
      Atomically replaces the shared value and triggers a watcher flush for every open session of the owning view.
      Specified by:
      set in interface SharedState<T>
      Parameters:
      newValue - the new value, possibly null
    • update

      public void update(@NotNull @NotNull UnaryOperator<T> fn)
      Description copied from interface: SharedState
      Atomically updates the shared value with a compare-and-set loop; fn may run more than once under contention and must be side-effect free.
      Specified by:
      update in interface SharedState<T>
      Parameters:
      fn - the function producing the new value from the current one