接口 ContextStore
public interface ContextStore
Storage layer abstraction for tool execution context.
This interface defines the storage contract for context objects. It supports two retrieval modes:
- By type only:
get(Class<T>)- suitable for singleton scenarios - By key + type:
get(String, Class<T>)- suitable for multi-instance scenarios
This design allows handling both simple cases (one UserContext) and complex cases (multiple UserContext instances for different users).
Implementations can be:
- Simple in-memory Map storage (
DefaultContextStore) - Custom storage backends (Redis, database, etc.)
Example usage:
// Single instance per type
DatabaseConfig config = store.get(DatabaseConfig.class);
// Multiple instances of same type
UserContext admin = store.get("admin", UserContext.class);
UserContext guest = store.get("guest", UserContext.class);
- 另请参阅:
-
方法概要
修饰符和类型方法说明booleanChecks whether any object of the specified type exists (regardless of key).booleanChecks whether an object with the specified key and type exists.<T> TRetrieves an object by type only (without key).<T> TRetrieves an object by key and type.
-
方法详细资料
-
get
Retrieves an object by key and type.This method allows storing multiple instances of the same type with different keys. Keys can be user IDs, session IDs, or any other identifier that distinguishes instances.
Example:
// Store multiple UserContext instances store.register("user123", new UserContext("user123")); store.register("user456", new UserContext("user456")); // Retrieve specific instance UserContext user123 = store.get("user123", UserContext.class);- 类型参数:
T- The object type- 参数:
key- The key identifying the specific instancetype- The class type to retrieve- 返回:
- The object instance, or null if not found
-
get
Retrieves an object by type only (without key).This is a convenience method for singleton scenarios where only one instance of a type exists. If multiple instances exist, implementations may:
- Return the "default" instance (implementation-defined)
- Return the first registered instance
- Return null and require explicit key
Example:
// Single DatabaseConfig instance DatabaseConfig config = store.get(DatabaseConfig.class);- 类型参数:
T- The object type- 参数:
type- The class type to retrieve- 返回:
- The object instance, or null if not found
-
contains
Checks whether an object with the specified key and type exists.- 参数:
key- The key identifying the instancetype- The class type to check- 返回:
- true if the object exists, false otherwise
-
contains
Checks whether any object of the specified type exists (regardless of key).- 参数:
type- The class type to check- 返回:
- true if at least one object of this type exists, false otherwise
-