Class SharedContext

java.lang.Object
com.tnsai.coordination.resources.SharedContext

public class SharedContext extends Object
Thread-safe shared key-value store for group members.

SharedContext provides a simple concurrent map that group members can use to share data. It supports typed retrieval with default values and snapshot operations for safe iteration.

Usage Example


 SharedContext context = new SharedContext();

 // Store and retrieve values
 context.put("status", "processing");
 String status = context.get("status", String.class);

 // With defaults
 int count = context.getOrDefault("retryCount", Integer.class, 0);

 // Snapshot for safe iteration
 Map<String, Object> snapshot = context.snapshot();
 
  • Constructor Details

    • SharedContext

      public SharedContext()
      Creates a new empty SharedContext.
    • SharedContext

      public SharedContext(Map<String,Object> initial)
      Creates a SharedContext with initial data.
      Parameters:
      initial - initial key-value pairs
      Throws:
      NullPointerException - if initial is null
  • Method Details

    • put

      public void put(String key, Object value)
      Stores a value for the given key.
      Parameters:
      key - the key
      value - the value (must not be null)
      Throws:
      NullPointerException - if key or value is null
    • get

      public <T> T get(String key, Class<T> type)
      Retrieves a value by key with type casting.
      Type Parameters:
      T - the expected type
      Parameters:
      key - the key to look up
      type - the expected type class
      Returns:
      the value, or null if not found or type mismatch
      Throws:
      NullPointerException - if key or type is null
    • getOrDefault

      public <T> T getOrDefault(String key, Class<T> type, T defaultValue)
      Retrieves a value by key, returning a default if not found or type mismatch.
      Type Parameters:
      T - the expected type
      Parameters:
      key - the key to look up
      type - the expected type class
      defaultValue - the default value to return if not found
      Returns:
      the value, or defaultValue if not found or type mismatch
      Throws:
      NullPointerException - if key or type is null
    • contains

      public boolean contains(String key)
      Checks if the context contains a key.
      Parameters:
      key - the key to check
      Returns:
      true if the key exists
      Throws:
      NullPointerException - if key is null
    • remove

      public void remove(String key)
      Removes a value by key.
      Parameters:
      key - the key to remove
      Throws:
      NullPointerException - if key is null
    • snapshot

      public Map<String,Object> snapshot()
      Returns an immutable snapshot of the current context data.
      Returns:
      unmodifiable copy of the context
    • clear

      public void clear()
      Clears all data from the context.
    • size

      public int size()
      Returns the number of entries in the context.
      Returns:
      entry count