类 SingletonSupplier<T>

java.lang.Object
cn.taketoday.util.function.SingletonSupplier<T>
类型参数:
T - the type of results supplied by this supplier
所有已实现的接口:
Supplier<T>

public class SingletonSupplier<T> extends Object implements Supplier<T>
A Supplier decorator that caches a singleton result and makes it available from get() (nullable) and obtain() (null-safe).

A SingletonSupplier can be constructed via of factory methods or via constructors that provide a default supplier as a fallback. This is particularly useful for method reference suppliers, falling back to a default supplier for a method that returned null and caching the result.

从以下版本开始:
3.0
作者:
Juergen Hoeller, TODAY 2021/3/25 11:38
  • 字段详细资料

    • singletonInstance

      @Nullable private volatile T singletonInstance
    • defaultSupplier

      @Nullable private final Supplier<? extends T> defaultSupplier
    • instanceSupplier

      @Nullable private final Supplier<? extends T> instanceSupplier
  • 构造器详细资料

    • SingletonSupplier

      public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier)
      Build a SingletonSupplier with the given singleton instance and a default supplier for the case when the instance is null.
      参数:
      instance - the singleton instance (potentially null)
      defaultSupplier - the default supplier as a fallback
    • SingletonSupplier

      public SingletonSupplier(@Nullable Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
      Build a SingletonSupplier with the given instance supplier and a default supplier for the case when the instance is null.
      参数:
      instanceSupplier - the immediate instance supplier
      defaultSupplier - the default supplier as a fallback
    • SingletonSupplier

      private SingletonSupplier(Supplier<? extends T> supplier)
    • SingletonSupplier

      private SingletonSupplier(T singletonInstance)
  • 方法详细资料

    • get

      @Nullable public T get()
      Get the shared singleton instance for this supplier.
      指定者:
      get 在接口中 Supplier<T>
      返回:
      the singleton instance (or null if none)
    • obtain

      public T obtain()
      Obtain the shared singleton instance for this supplier.
      返回:
      the singleton instance (never null)
      抛出:
      IllegalStateException - in case of no instance
    • valueOf

      public static <T> SingletonSupplier<T> valueOf(T instance)
      Build a SingletonSupplier with the given singleton instance.
      参数:
      instance - the singleton instance (never null)
      返回:
      the singleton supplier (never null)
    • ofNullable

      @Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance)
      Build a SingletonSupplier with the given singleton instance.
      参数:
      instance - the singleton instance (potentially null)
      返回:
      the singleton supplier, or null if the instance was null
    • from

      public static <T> SingletonSupplier<T> from(Supplier<T> supplier)
      Build a SingletonSupplier with the given supplier.
      参数:
      supplier - the instance supplier (never null)
      返回:
      the singleton supplier (never null)
    • ofNullable

      @Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier)
      Build a SingletonSupplier with the given supplier.
      参数:
      supplier - the instance supplier (potentially null)
      返回:
      the singleton supplier, or null if the instance supplier was null