Interface StateStore
- All Known Implementing Classes:
MemoryStateStore
public interface StateStore
Interface for a simple KV state store
-
Method Summary
Modifier and TypeMethodDescriptiondefault doublevoidclear()default voiddefault intdefault com.google.common.util.concurrent.AtomicDoublegetAccumulator(String key) Returns the AtomicDouble accumulator for the given key, creating it lazily on first access.default AtomicIntegergetCounter(String key) Returns the AtomicInteger counter for the given key, creating it lazily on first access.default <T> TgetOrCreateState(String key, Supplier<T> factory) Returns the value for the given key, creating and storing it viafactoryon first access.<T> Tdefault <T> Tdefault booleandefault intdefault booleandefault voidResets the value at the given key based on its type: AtomicInteger and AtomicDouble are set to 0, Boolean values are unset (set to false).default voiddefault void<T> voiddefault void
-
Method Details
-
setState
-
getState
-
getState
-
clear
void clear() -
has
-
clear
-
inc
-
dec
-
add
-
reset
Resets the value at the given key based on its type: AtomicInteger and AtomicDouble are set to 0, Boolean values are unset (set to false). Other types are left unchanged. This polymorphic reset avoids removing the entry, preserving the type for future use.- Parameters:
key- the state key to reset
-
set
-
unset
-
set
-
is
-
getOrCreateState
Returns the value for the given key, creating and storing it viafactoryon first access. This is the lazy-creation seam behindgetCounter(String)/getAccumulator(String): the atomicity of those accessors' thread-safety promise is only as strong as this method.This DEFAULT implementation is check-then-put and therefore NOT atomic — two concurrent first accesses can each create a value and one wins, so increments applied to the loser are lost. Implementations backed by a concurrent map MUST override it with an atomic compute-if-absent (see
MemoryStateStore); only single-threaded stores may keep the default.- Parameters:
key- the state keyfactory- creates the value when absent- Returns:
- the stored value (never null)
-
getCounter
Returns the AtomicInteger counter for the given key, creating it lazily on first access. Thread-safe for concurrent increment/decrement operations WHEN the implementation provides an atomicgetOrCreateState(String, Supplier)— the historical inline check-then-put here could hand two racing threads two different counters, silently losing increments.- Parameters:
key- the state key- Returns:
- the atomic counter (never null)
-
getAccumulator
Returns the AtomicDouble accumulator for the given key, creating it lazily on first access. Thread-safe for concurrent add operations under the samegetOrCreateState(String, Supplier)atomicity condition asgetCounter(String).- Parameters:
key- the state key- Returns:
- the atomic accumulator (never null)
-