Class Singleton

java.lang.Object
cloud.opencode.base.core.singleton.Singleton

public final class Singleton extends Object
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 Type
    Method
    Description
    static void
    Clears all singletons 清除所有单例
    static boolean
    contains(Class<?> clazz)
    Checks if a singleton exists (by type) 检查单例是否存在(按类型)
    static boolean
    Checks if a singleton exists (by name) 检查单例是否存在(按名称)
    static <T> T
    get(Class<T> clazz)
    Gets a singleton instance (by type) 获取单例实例(按类型)
    static <T> T
    get(Class<T> clazz, Supplier<T> supplier)
    Gets a singleton instance, creates if absent 获取单例实例,不存在时创建
    static <T> T
    get(String name)
    Gets a singleton instance (by name) 获取单例实例(按名称)
    static <T> T
    get(String name, Supplier<T> supplier)
    Gets a singleton instance, creates if absent (by name) 获取单例实例,不存在时创建(按名称)
    static int
    Gets the count of all named singletons 获取所有命名单例数量
    static <T> void
    register(Class<T> clazz, T instance)
    Registers a singleton instance (by type) 注册单例实例(按类型)
    static void
    register(String name, Object instance)
    Registers a singleton instance (by name) 注册单例实例(按名称)
    static <T> T
    registerIfAbsent(Class<T> clazz, T instance)
    Registers a singleton instance (if absent) 注册单例实例(如果不存在)
    static <T> T
    registerIfAbsent(String name, T instance)
    Registers a singleton instance (if absent, by name) 注册单例实例(如果不存在,按名称)
    static void
    remove(Class<?> clazz)
    Removes a singleton instance (by type) 移除单例实例(按类型)
    static void
    remove(String name)
    Removes a singleton instance (by name) 移除单例实例(按名称)
    static int
    Gets the count of all type-based singletons 获取所有类型单例数量

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public static <T> T get(Class<T> clazz)
      Gets a singleton instance (by type) 获取单例实例(按类型)
    • get

      public static <T> T get(Class<T> clazz, Supplier<T> supplier)
      Gets a singleton instance, creates if absent 获取单例实例,不存在时创建
    • register

      public static <T> void register(Class<T> clazz, T instance)
      Registers a singleton instance (by type) 注册单例实例(按类型)
    • registerIfAbsent

      public static <T> T registerIfAbsent(Class<T> clazz, T instance)
      Registers a singleton instance (if absent) 注册单例实例(如果不存在)
    • remove

      public static void remove(Class<?> clazz)
      Removes a singleton instance (by type) 移除单例实例(按类型)
    • contains

      public static boolean contains(Class<?> clazz)
      Checks if a singleton exists (by type) 检查单例是否存在(按类型)
    • get

      public static <T> T get(String name)
      Gets a singleton instance (by name) 获取单例实例(按名称)
    • get

      public static <T> T get(String name, Supplier<T> supplier)
      Gets a singleton instance, creates if absent (by name) 获取单例实例,不存在时创建(按名称)
    • register

      public static void register(String name, Object instance)
      Registers a singleton instance (by name) 注册单例实例(按名称)
    • registerIfAbsent

      public static <T> T registerIfAbsent(String name, T instance)
      Registers a singleton instance (if absent, by name) 注册单例实例(如果不存在,按名称)
    • remove

      public static void remove(String name)
      Removes a singleton instance (by name) 移除单例实例(按名称)
    • contains

      public static boolean contains(String name)
      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 获取所有命名单例数量