Class SingletonSupplier<T>
- java.lang.Object
- org.springframework.util.function.SingletonSupplier<T>
- Type Parameters:
T- the type of results supplied by this supplier
- All Implemented Interfaces:
Supplier<T>
public class SingletonSupplier<T> extends Object implements Supplier<T>
ASupplierdecorator that caches a singleton result and makes it available fromget()(nullable) andobtain()(null-safe).A
SingletonSuppliercan be constructed viaoffactory 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 returnednulland caching the result.- Since:
- 5.1
- Author:
- Juergen Hoeller
Constructor Summary
Constructors Constructor Description SingletonSupplier(Supplier<? extends T> instanceSupplier, Supplier<? extends T> defaultSupplier)Build aSingletonSupplierwith the given instance supplier and a default supplier for the case when the instance isnull.SingletonSupplier(T instance, Supplier<? extends T> defaultSupplier)Build aSingletonSupplierwith the given singleton instance and a default supplier for the case when the instance isnull.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Get the shared singleton instance for this supplier.Tobtain()Obtain the shared singleton instance for this supplier.static <T> SingletonSupplier<T>of(Supplier<T> supplier)Build aSingletonSupplierwith the given supplier.static <T> SingletonSupplier<T>of(T instance)Build aSingletonSupplierwith the given singleton instance.static <T> SingletonSupplier<T>ofNullable(Supplier<T> supplier)Build aSingletonSupplierwith the given supplier.static <T> SingletonSupplier<T>ofNullable(T instance)Build aSingletonSupplierwith the given singleton instance.
Constructor Detail
SingletonSupplier
public SingletonSupplier(@Nullable T instance, Supplier<? extends T> defaultSupplier)
Build aSingletonSupplierwith the given singleton instance and a default supplier for the case when the instance isnull.- Parameters:
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 aSingletonSupplierwith the given instance supplier and a default supplier for the case when the instance isnull.- Parameters:
instanceSupplier- the immediate instance supplierdefaultSupplier- the default supplier as a fallback
Method Detail
obtain
public T obtain()
Obtain the shared singleton instance for this supplier.- Returns:
- the singleton instance (never
null) - Throws:
IllegalStateException- in case of no instance
of
public static <T> SingletonSupplier<T> of(T instance)
Build aSingletonSupplierwith the given singleton instance.- Parameters:
instance- the singleton instance (nevernull)- Returns:
- the singleton supplier (never
null)
ofNullable
@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable T instance)
Build aSingletonSupplierwith the given singleton instance.- Parameters:
instance- the singleton instance (potentiallynull)- Returns:
- the singleton supplier, or
nullif the instance wasnull
of
public static <T> SingletonSupplier<T> of(Supplier<T> supplier)
Build aSingletonSupplierwith the given supplier.- Parameters:
supplier- the instance supplier (nevernull)- Returns:
- the singleton supplier (never
null)
ofNullable
@Nullable public static <T> SingletonSupplier<T> ofNullable(@Nullable Supplier<T> supplier)
Build aSingletonSupplierwith the given supplier.- Parameters:
supplier- the instance supplier (potentiallynull)- Returns:
- the singleton supplier, or
nullif the instance supplier wasnull