T
- the type of results supplied by this supplierpublic class SingletonSupplier<T>
extends java.lang.Object
implements java.util.function.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(java.util.function.Supplier<? extends T> instanceSupplier,
java.util.function.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,
java.util.function.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(java.util.function.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(java.util.function.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, java.util.function.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 java.util.function.Supplier<? extends T> instanceSupplier, java.util.function.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 fallback@Nullable public T get()
get
in interface java.util.function.Supplier<T>
null
if none)public T obtain()
null
)java.lang.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 null
public static <T> SingletonSupplier<T> of(java.util.function.Supplier<T> supplier)
SingletonSupplier
with the given supplier.supplier
- the instance supplier (never null
)null
)@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable java.util.function.Supplier<T> supplier)
SingletonSupplier
with the given supplier.supplier
- the instance supplier (potentially null
)null
if the instance supplier was null