Class SharedStateImpl<T>
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
ConstructorsConstructorDescriptionSharedStateImpl(@NotNull View owner, @NotNull TokenTable table, T initialValue) Creates and registers the token. -
Method Summary
Modifier and TypeMethodDescriptionvoidWires the engine flush callback invoked after everyset(T); assigned once by the engine on the view's first open.booleanReturnstrueif the engine has already wired a flush hook on this token.get()Returns the current shared value.intid()Returns the table-assigned token id.voidAtomically replaces the shared value and triggers a watcher flush for every open session of the owning view.inttokenId()voidupdate(@NotNull UnaryOperator<T> fn) Atomically updates the shared value with a compare-and-set loop;fnmay run more than once under contention and must be side-effect free.
-
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 tokentable- the owner's token tableinitialValue- the initial shared value, possiblynull- 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:
tokenIdin interfaceIdentifiableToken- Returns:
- the token id assigned by the owning view's token table
-
flushHook
Wires the engine flush callback invoked after everyset(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()Returnstrueif the engine has already wired a flush hook on this token.- Returns:
truewhen a flush hook is present
-
get
Description copied from interface:SharedStateReturns the current shared value.- Specified by:
getin interfaceSharedState<T>- Returns:
- the current value, possibly
null
-
set
Description copied from interface:SharedStateAtomically replaces the shared value and triggers a watcher flush for every open session of the owning view.- Specified by:
setin interfaceSharedState<T>- Parameters:
newValue- the new value, possiblynull
-
update
Description copied from interface:SharedStateAtomically updates the shared value with a compare-and-set loop;fnmay run more than once under contention and must be side-effect free.- Specified by:
updatein interfaceSharedState<T>- Parameters:
fn- the function producing the new value from the current one
-