Package com.tnsai.coordination.resources
Class SharedContext
java.lang.Object
com.tnsai.coordination.resources.SharedContext
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 Summary
ConstructorsConstructorDescriptionCreates a new empty SharedContext.SharedContext(Map<String, Object> initial) Creates a SharedContext with initial data. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all data from the context.booleanChecks if the context contains a key.<T> TRetrieves a value by key with type casting.<T> TgetOrDefault(String key, Class<T> type, T defaultValue) Retrieves a value by key, returning a default if not found or type mismatch.voidStores a value for the given key.voidRemoves a value by key.intsize()Returns the number of entries in the context.snapshot()Returns an immutable snapshot of the current context data.
-
Constructor Details
-
SharedContext
public SharedContext()Creates a new empty SharedContext. -
SharedContext
Creates a SharedContext with initial data.- Parameters:
initial- initial key-value pairs- Throws:
NullPointerException- if initial is null
-
-
Method Details
-
put
Stores a value for the given key.- Parameters:
key- the keyvalue- the value (must not be null)- Throws:
NullPointerException- if key or value is null
-
get
Retrieves a value by key with type casting.- Type Parameters:
T- the expected type- Parameters:
key- the key to look uptype- the expected type class- Returns:
- the value, or null if not found or type mismatch
- Throws:
NullPointerException- if key or type is null
-
getOrDefault
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 uptype- the expected type classdefaultValue- 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
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
Removes a value by key.- Parameters:
key- the key to remove- Throws:
NullPointerException- if key is null
-
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
-