类 Metadata

java.lang.Object
com.agentsflex.core.util.Metadata
所有已实现的接口:
Serializable
直接已知子类:
ChatOptions, ImageResponse, Message, Prompt, StoreOptions, StoreResult, VectorData

public class Metadata extends Object implements Serializable
A thread-safe, serializable metadata container for storing key-value pairs.

Key Features:

  • Thread-safe by default using ConcurrentHashMap
  • Defensive copying to protect internal state
  • Null-safe: returns empty map instead of null
  • Type-safe generic getter with fallback default
  • Immutable view exposure via asMap()

Usage Example:


   Metadata meta = new Metadata();
   meta.putMetadata("model", "gpt-4");
   meta.putMetadata("temperature", 0.7);

   String model = meta.getMetadata("model", String.class, "default");
   Map<String, Object> snapshot = meta.asMap(); // unmodifiable view
 
从以下版本开始:
2023
作者:
Michael Yang (fuhai999@gmail.com)
另请参阅:
  • 字段详细资料

  • 构造器详细资料

  • 方法详细资料

    • getMetadata

      public Object getMetadata(String key)
      Gets the value associated with the specified key.
      参数:
      key - the metadata key
      返回:
      the value, or null if not found
    • getMetadata

      public Object getMetadata(String key, Object defaultValue)
      Gets the value associated with the specified key, or returns defaultValue if not found.
      参数:
      key - the metadata key
      defaultValue - the value to return if key is absent
      返回:
      the value, or defaultValue if not found
    • getMetadata

      public <T> T getMetadata(String key, Class<T> type, T defaultValue)
      Type-safe getter with class check and fallback default.
      类型参数:
      T - the expected type
      参数:
      key - the metadata key
      type - the expected type of the value
      defaultValue - the value to return if key is absent or type mismatch
      返回:
      the casted value, or defaultValue if not found or type mismatch
    • putMetadata

      public void putMetadata(String key, Object value)
      Puts a metadata entry (replaces existing value if key exists).
      参数:
      key - the metadata key
      value - the metadata value (must be Serializable if serialization is used)
    • putMetadata

      public void putMetadata(Map<String,?> metadata)
      Puts all entries from the given map (replaces existing values for duplicate keys).
      参数:
      metadata - the map of metadata to add
    • putMetadataIfAbsent

      public Object putMetadataIfAbsent(String key, Object value)
      Puts a metadata entry only if the key is not already present.
      参数:
      key - the metadata key
      value - the metadata value
      返回:
      the previous value associated with the key, or null if there was no mapping
    • removeMetadata

      Removes the metadata entry for the specified key.
      参数:
      key - the metadata key
      返回:
      the previous value associated with the key, or null if there was no mapping
    • containsMetadata

      public boolean containsMetadata(String key)
      Checks if metadata contains the specified key.
      参数:
      key - the metadata key
      返回:
      true if the key exists, false otherwise
    • isEmpty

      public boolean isEmpty()
      Checks if this metadata container is empty.
      返回:
      true if no entries exist, false otherwise
    • sizeOfMetadata

      public int sizeOfMetadata()
      Returns the number of metadata entries.
      返回:
      the entry count
    • clearMetadata

      public void clearMetadata()
      Removes all metadata entries.
    • asMap

      public Map<String,Object> asMap()
      Returns an unmodifiable view of the internal metadata map.

      Changes to this Metadata instance will be reflected in the returned view, but attempts to modify the view directly will throw UnsupportedOperationException.

      返回:
      an unmodifiable map view (never null)
    • setMetadataMap

      public void setMetadataMap(Map<String,?> metadataMap)
      Replaces the internal metadata map with a defensive copy of the provided map.

      This method does not merge; it replaces all existing entries.

      参数:
      metadataMap - the new metadata map (may be null to clear)
    • getOrCreateMap

      Gets or creates the internal map with lazy initialization. Thread-safe via double-checked locking pattern.
      返回:
      the internal map instance
    • toString

      public String toString()
      覆盖:
      toString 在类中 Object
    • equals

      public boolean equals(Object o)
      覆盖:
      equals 在类中 Object
    • hashCode

      public int hashCode()
      覆盖:
      hashCode 在类中 Object
    • addMetadata

      @Deprecated public void addMetadata(String key, Object value)
      已过时。
      use putMetadata(String, Object) for clearer semantics. This method is retained for backward compatibility and will be removed in a future major version.
    • addMetadata

      @Deprecated public void addMetadata(Map<String,Object> metadata)
      已过时。
      use putMetadata(Map) for clearer semantics. This method is retained for backward compatibility and will be removed in a future major version.
    • getMetadataMap