Interface SharedState


public interface SharedState
Thread-safe key-value store for sharing state between scripts.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all stored key-value pairs.
    boolean
    Checks whether a value is stored under the given key.
    get(String key)
    Retrieves the value stored under the given key.
    default <T> T
    get(String key, Class<T> type)
    Retrieves and casts the value stored under the given key.
    void
    put(String key, Object value)
    Stores a value under the given key, replacing any existing value.
    Removes the value stored under the given key.
    Returns an immutable snapshot of all key-value pairs currently stored.
  • Method Details

    • put

      void put(String key, Object value)
      Stores a value under the given key, replacing any existing value.

      The store holds a bounded number of distinct keys. Once full it keeps accepting overwrites of keys it already has but drops writes that would introduce a new one, so a runaway script cannot exhaust the host's heap. Reaching the bound is a bug in the writer, not a state to design around.

      Parameters:
      key - the key to store the value under
      value - the value to store
    • get

      Object get(String key)
      Retrieves the value stored under the given key.
      Parameters:
      key - the key to look up
      Returns:
      the stored value, or null if not present
    • get

      default <T> T get(String key, Class<T> type)
      Retrieves and casts the value stored under the given key.

      This is the prescribed typed-key recovery idiom for the script blackboard: the store remains heterogeneous-by-design, but the lookup recovers the narrow type at the call site via the supplied class token.

      Type Parameters:
      T - the expected value type
      Parameters:
      key - the key to look up
      type - the expected class of the value
      Returns:
      the stored value cast to T, or null if not present or not an instance of type
    • remove

      Object remove(String key)
      Removes the value stored under the given key.
      Parameters:
      key - the key to remove
      Returns:
      the previously stored value, or null if not present
    • containsKey

      boolean containsKey(String key)
      Checks whether a value is stored under the given key.
      Parameters:
      key - the key to check
      Returns:
      true if a value exists for the key
    • snapshot

      Map<String,Object> snapshot()
      Returns an immutable snapshot of all key-value pairs currently stored.
      Returns:
      an unmodifiable map of all entries
    • clear

      void clear()
      Removes all stored key-value pairs.