T - the type of results supplied by this supplierpublic class SingletonSupplier<T> extends Object implements Supplier<T>
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.
| Constructor and Description |
|---|
SingletonSupplier(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. |
SingletonSupplier(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. |
| Modifier and Type | Method and Description |
|---|---|
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 a
SingletonSupplier with the given supplier. |
static <T> SingletonSupplier<T> |
of(T instance)
Build a
SingletonSupplier with the given singleton instance. |
static <T> SingletonSupplier<T> |
ofNullable(Supplier<T> supplier)
Build a
SingletonSupplier with the given supplier. |
static <T> SingletonSupplier<T> |
ofNullable(T instance)
Build a
SingletonSupplier with the given singleton instance. |
public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier)
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 fallbackpublic SingletonSupplier(@Nullable Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)
SingletonSupplier with the given instance supplier
and a default supplier for the case when the instance is null.instanceSupplier - the immediate instance supplierdefaultSupplier - the default supplier as a fallbackpublic T obtain()
null)IllegalStateException - in case of no instancepublic static <T> SingletonSupplier<T> of(T instance)
SingletonSupplier with the given singleton instance.instance - the singleton instance (never null)null)@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance)
SingletonSupplier with the given singleton instance.instance - the singleton instance (potentially null)null if the instance was nullpublic static <T> SingletonSupplier<T> of(Supplier<T> supplier)
SingletonSupplier with the given supplier.supplier - the instance supplier (never null)null)@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier)
SingletonSupplier with the given supplier.supplier - the instance supplier (potentially null)null if the instance supplier was null