Interface StateStore

All Known Implementing Classes:
MemoryStateStore

public interface StateStore
Interface for a simple KV state store
  • Method Summary

    Modifier and Type
    Method
    Description
    default double
    add(String key, double delta)
     
    void
     
    default void
     
    default int
    dec(String key)
     
    default com.google.common.util.concurrent.AtomicDouble
    Returns the AtomicDouble accumulator for the given key, creating it lazily on first access.
    Returns the AtomicInteger counter for the given key, creating it lazily on first access.
    default <T> T
    getOrCreateState(String key, Supplier<T> factory)
    Returns the value for the given key, creating and storing it via factory on first access.
    <T> T
     
    default <T> T
    getState(String key, T defaultValue)
     
    default boolean
    has(String key)
     
    default int
    inc(String key)
     
    default boolean
    is(String key)
     
    default void
    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).
    default void
    set(String key)
     
    default void
    set(String key, boolean value)
     
    <T> void
    setState(String key, T value)
     
    default void
     
  • Method Details

    • setState

      <T> void setState(String key, T value)
    • getState

      <T> T getState(String key)
    • getState

      default <T> T getState(String key, T defaultValue)
    • clear

      void clear()
    • has

      default boolean has(String key)
    • clear

      default void clear(String key)
    • inc

      default int inc(String key)
    • dec

      default int dec(String key)
    • add

      default double add(String key, double delta)
    • reset

      default void reset(String key)
      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

      default void set(String key)
    • unset

      default void unset(String key)
    • set

      default void set(String key, boolean value)
    • is

      default boolean is(String key)
    • getOrCreateState

      default <T> T getOrCreateState(String key, Supplier<T> factory)
      Returns the value for the given key, creating and storing it via factory on first access. This is the lazy-creation seam behind getCounter(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 key
      factory - creates the value when absent
      Returns:
      the stored value (never null)
    • getCounter

      default AtomicInteger getCounter(String key)
      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 atomic getOrCreateState(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

      default com.google.common.util.concurrent.AtomicDouble getAccumulator(String key)
      Returns the AtomicDouble accumulator for the given key, creating it lazily on first access. Thread-safe for concurrent add operations under the same getOrCreateState(String, Supplier) atomicity condition as getCounter(String).
      Parameters:
      key - the state key
      Returns:
      the atomic accumulator (never null)