类 SingletonSupplier<T>
- java.lang.Object
- org.springframework.util.function.SingletonSupplier<T>
- 类型参数:
T
- the type of results supplied by this supplier
- 所有已实现的接口:
Supplier<T>
public class SingletonSupplier<T> extends Object implements Supplier<T>
ASupplier
decorator that caches a singleton result and makes it available fromget()
(nullable) andobtain()
(null-safe).A
SingletonSupplier
can be constructed viaof
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 returnednull
and caching the result.- 从以下版本开始:
- 5.1
- 作者:
- Juergen Hoeller
构造器概要
构造器 构造器 说明 SingletonSupplier(Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
Build aSingletonSupplier
with the given instance supplier and a default supplier for the case when the instance isnull
.SingletonSupplier(T instance, Supplier<? extends T> defaultSupplier)
Build aSingletonSupplier
with the given singleton instance and a default supplier for the case when the instance isnull
.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 T
get()
Get the shared singleton instance for this supplier.T
obtain()
Obtain the shared singleton instance for this supplier.static <T> SingletonSupplier<T>
of(Supplier<T> supplier)
Build aSingletonSupplier
with the given supplier.static <T> SingletonSupplier<T>
of(T instance)
Build aSingletonSupplier
with the given singleton instance.static <T> SingletonSupplier<T>
ofNullable(Supplier<T> supplier)
Build aSingletonSupplier
with the given supplier.static <T> SingletonSupplier<T>
ofNullable(T instance)
Build aSingletonSupplier
with the given singleton instance.
构造器详细资料
SingletonSupplier
public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier)
Build aSingletonSupplier
with the given singleton instance and a default supplier for the case when the instance isnull
.- 参数:
instance
- the singleton instance (potentiallynull
)defaultSupplier
- the default supplier as a fallback
SingletonSupplier
public SingletonSupplier(@Nullable Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
Build aSingletonSupplier
with the given instance supplier and a default supplier for the case when the instance isnull
.- 参数:
instanceSupplier
- the immediate instance supplierdefaultSupplier
- the default supplier as a fallback
方法详细资料
obtain
public T obtain()
Obtain the shared singleton instance for this supplier.- 返回:
- the singleton instance (never
null
) - 抛出:
IllegalStateException
- in case of no instance
of
public static <T> SingletonSupplier<T> of(T instance)
Build aSingletonSupplier
with the given singleton instance.- 参数:
instance
- the singleton instance (nevernull
)- 返回:
- the singleton supplier (never
null
)
ofNullable
@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance)
Build aSingletonSupplier
with the given singleton instance.- 参数:
instance
- the singleton instance (potentiallynull
)- 返回:
- the singleton supplier, or
null
if the instance wasnull
of
public static <T> SingletonSupplier<T> of(Supplier<T> supplier)
Build aSingletonSupplier
with the given supplier.- 参数:
supplier
- the instance supplier (nevernull
)- 返回:
- the singleton supplier (never
null
)
ofNullable
@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier)
Build aSingletonSupplier
with the given supplier.- 参数:
supplier
- the instance supplier (potentiallynull
)- 返回:
- the singleton supplier, or
null
if the instance supplier wasnull