Class MutableStateImpl<T>
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 Summary
ConstructorsConstructorDescriptionMutableStateImpl(@NotNull View owner, @NotNull TokenTable table, @NotNull Function<ViewContext, T> initialValue) Creates and registers the token. -
Method Summary
Modifier and TypeMethodDescriptionget(@NotNull ViewContext context) Reads this token's value for the given context.intid()Returns the table-assigned token id, the index into the per-session state store.voidset(@NotNull ViewContext context, T value) Writes this token's value for the given context and marks watchers dirty.inttokenId()voidupdate(@NotNull ViewContext context, @NotNull UnaryOperator<T> fn) Read-modify-write convenience: appliesfnto the current value and stores the result.
-
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 tokentable- the owner's token tableinitialValue- 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:
tokenIdin interfaceIdentifiableToken- Returns:
- the token id assigned by the owning view's token table
-
get
Description copied from interface:StateReads this token's value for the given context. Callable from any thread, but values are only coherent on the main thread. -
set
Description copied from interface:MutableStateWrites this token's value for the given context and marks watchers dirty.- Specified by:
setin interfaceMutableState<T>- Parameters:
context- an active context of the owning viewvalue- the new value, possiblynull
-
update
Description copied from interface:MutableStateRead-modify-write convenience: appliesfnto the current value and stores the result. Main thread only.- Specified by:
updatein interfaceMutableState<T>- Parameters:
context- an active context of the owning viewfn- the function producing the new value from the current one; invoked exactly once on the current value. Side effects inside fn are safe (unlikeSharedState.update(java.util.function.UnaryOperator<T>))
-