Class Singleton
java.lang.Object
cloud.opencode.base.core.singleton.Singleton
Singleton Container - Global singleton instance registry
单例容器 - 全局单例实例注册表
Provides a thread-safe container for managing singleton instances by type or name.
提供线程安全的容器,用于按类型或名称管理单例实例。
Features | 主要功能:
- Type-based registry (get, register by Class) - 按类型注册
- Name-based registry (get, register by String) - 按名称注册
- Lazy initialization (get with Supplier) - 延迟初始化
- Thread-safe operations (ConcurrentHashMap) - 线程安全操作
Usage Examples | 使用示例:
// Type-based - 按类型
Singleton.register(DataSource.class, dataSource);
DataSource ds = Singleton.get(DataSource.class);
// Lazy initialization - 延迟初始化
Config config = Singleton.get(Config.class, () -> new Config());
// Name-based - 按名称
Singleton.register("myService", service);
MyService svc = Singleton.get("myService");
Security | 安全性:
- Thread-safe: Yes (ConcurrentHashMap) - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clears all singletons 清除所有单例static booleanChecks if a singleton exists (by type) 检查单例是否存在(按类型)static booleanChecks if a singleton exists (by name) 检查单例是否存在(按名称)static <T> TGets a singleton instance (by type) 获取单例实例(按类型)static <T> TGets a singleton instance, creates if absent 获取单例实例,不存在时创建static <T> TGets a singleton instance (by name) 获取单例实例(按名称)static <T> TGets a singleton instance, creates if absent (by name) 获取单例实例,不存在时创建(按名称)static intGets the count of all named singletons 获取所有命名单例数量static <T> voidRegisters a singleton instance (by type) 注册单例实例(按类型)static voidRegisters a singleton instance (by name) 注册单例实例(按名称)static <T> TregisterIfAbsent(Class<T> clazz, T instance) Registers a singleton instance (if absent) 注册单例实例(如果不存在)static <T> TregisterIfAbsent(String name, T instance) Registers a singleton instance (if absent, by name) 注册单例实例(如果不存在,按名称)static voidRemoves a singleton instance (by type) 移除单例实例(按类型)static voidRemoves a singleton instance (by name) 移除单例实例(按名称)static intsize()Gets the count of all type-based singletons 获取所有类型单例数量
-
Method Details
-
get
Gets a singleton instance (by type) 获取单例实例(按类型) -
get
-
register
Registers a singleton instance (by type) 注册单例实例(按类型) -
registerIfAbsent
Registers a singleton instance (if absent) 注册单例实例(如果不存在) -
remove
Removes a singleton instance (by type) 移除单例实例(按类型) -
contains
Checks if a singleton exists (by type) 检查单例是否存在(按类型) -
get
Gets a singleton instance (by name) 获取单例实例(按名称) -
get
-
register
-
registerIfAbsent
Registers a singleton instance (if absent, by name) 注册单例实例(如果不存在,按名称) -
remove
Removes a singleton instance (by name) 移除单例实例(按名称) -
contains
Checks if a singleton exists (by name) 检查单例是否存在(按名称) -
clear
public static void clear()Clears all singletons 清除所有单例 -
size
public static int size()Gets the count of all type-based singletons 获取所有类型单例数量 -
namedSize
public static int namedSize()Gets the count of all named singletons 获取所有命名单例数量
-