Interface ClassToInstanceMap<B>

Type Parameters:
B - base type | 基础类型
All Superinterfaces:
Map<Class<? extends B>, B>
All Known Implementing Classes:
MutableClassToInstanceMap

public interface ClassToInstanceMap<B> extends Map<Class<? extends B>, B>
ClassToInstanceMap - Class to Instance Map Interface ClassToInstanceMap - 类实例映射接口

A type-safe map from Class objects to instances of that class.

从 Class 对象到该类实例的类型安全映射。

Features | 主要功能:

  • Type-safe retrieval - 类型安全检索
  • Type-safe storage - 类型安全存储
  • Extends Map interface - 扩展 Map 接口

Usage Examples | 使用示例:

ClassToInstanceMap<Object> map = MutableClassToInstanceMap.create();
map.putInstance(String.class, "hello");
map.putInstance(Integer.class, 42);

String str = map.getInstance(String.class);  // Type-safe
Integer num = map.getInstance(Integer.class);

Security | 安全性:

  • Thread-safe: Implementation-dependent - 取决于实现
  • Null-safe: Partial (getInstance returns null for missing) - 部分(getInstance对缺失返回null)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getInstance

      <T extends B> T getInstance(Class<T> type)
      Get the instance for the given type in a type-safe manner. 以类型安全的方式获取给定类型的实例。
      Type Parameters:
      T - the type | 类型
      Parameters:
      type - the class | 类
      Returns:
      the instance, or null if not found | 实例,未找到则返回 null
    • putInstance

      <T extends B> T putInstance(Class<T> type, T value)
      Put the instance for the given type in a type-safe manner. 以类型安全的方式放入给定类型的实例。
      Type Parameters:
      T - the type | 类型
      Parameters:
      type - the class | 类
      value - the instance | 实例
      Returns:
      the previous instance, or null if none | 之前的实例,如果没有则返回 null