Class MemoryStateStore

java.lang.Object
com.wualabs.qtsurfer.engine.core.state.MemoryStateStore
All Implemented Interfaces:
StateStore

public class MemoryStateStore extends Object implements StateStore
KV on memory State store implementation
  • Constructor Details

    • MemoryStateStore

      public MemoryStateStore()
  • Method Details

    • setState

      public <T> void setState(String key, T value)
      Sets a state value for the given key. If value is null, the key is removed from the store. Uses a ConcurrentHashMap for thread-safe access.
      Specified by:
      setState in interface StateStore
      Parameters:
      key - the state key
      value - the value to store, or null to remove the key
    • getState

      public <T> T getState(String key)
      Specified by:
      getState in interface StateStore
    • getOrCreateState

      public <T> T getOrCreateState(String key, Supplier<T> factory)
      Atomic lazy creation via ConcurrentHashMap.computeIfAbsent(K, Function): two threads racing the first inc()/add() on a key are guaranteed the SAME counter/accumulator instance. The interface's default (check-then-put) could hand each racer its own instance, losing the increments applied to the discarded one — despite the accessors' documented thread-safety.
      Specified by:
      getOrCreateState in interface StateStore
      Parameters:
      key - the state key
      factory - creates the value when absent
      Returns:
      the stored value (never null)
    • clear

      public void clear()
      Specified by:
      clear in interface StateStore
    • toString

      public String toString()
      Overrides:
      toString in class Object