接口 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:

  1. By type only: get(Class<T>) - suitable for singleton scenarios
  2. 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);
 
另请参阅:
  • 方法概要

    修饰符和类型
    方法
    说明
    boolean
    contains(Class<?> type)
    Checks whether any object of the specified type exists (regardless of key).
    boolean
    contains(String key, Class<?> type)
    Checks whether an object with the specified key and type exists.
    <T> T
    get(Class<T> type)
    Retrieves an object by type only (without key).
    <T> T
    get(String key, Class<T> type)
    Retrieves an object by key and type.
  • 方法详细资料

    • get

      <T> T get(String key, Class<T> type)
      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 instance
      type - The class type to retrieve
      返回:
      The object instance, or null if not found
    • get

      <T> T get(Class<T> type)
      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

      boolean contains(String key, Class<?> type)
      Checks whether an object with the specified key and type exists.
      参数:
      key - The key identifying the instance
      type - The class type to check
      返回:
      true if the object exists, false otherwise
    • contains

      boolean contains(Class<?> type)
      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