Top Description Fields Constructors Methods
org.springframework.util.function

public Class SingletonSupplier<T>

extends Object
implements Supplier<T>
Class Inheritance
All Implemented Interfaces
java.util.function.Supplier
Type Parameters
<T>
the type of results supplied by this supplier
Imports
java.util.concurrent.locks.Lock, .ReentrantLock, java.util.function.Supplier, org.springframework.lang.Nullable, org.springframework.util.Assert

A java.util.function.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.

Authors
Juergen Hoeller, Yanming Zhou
Since
5.1

Field Summary

Modifier and TypeField and Description
private final Supplier<? extends T>
private final Supplier<? extends T>
private volatile T
private final Lock
writeLock

Guards access to write operations on the singletonInstance field.

Constructor Summary

AccessConstructor and Description
public
SingletonSupplier(T
the singleton instance (potentially null)
instance
,
Supplier<? extends T>
the default supplier as a fallback
defaultSupplier
)

Build a SingletonSupplier with the given singleton instance and a default supplier for the case when the instance is null.

public
SingletonSupplier(Supplier<? extends T>
the immediate instance supplier
instanceSupplier
,
Supplier<? extends T>
the default supplier as a fallback
defaultSupplier
)

Build a SingletonSupplier with the given instance supplier and a default supplier for the case when the instance is null.

private
SingletonSupplier(Supplier<? extends T> supplier)

private
SingletonSupplier(T singletonInstance)

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

the singleton instance (or null if none)
get
()

Implements java.util.function.Supplier.get.

Get the shared singleton instance for this supplier.

public T

Returns:

the singleton instance (never null)
obtain
()

Obtain the shared singleton instance for this supplier.

public static <T> SingletonSupplier<T>

Returns:

the singleton supplier (never null)
of
(T
the singleton instance (never null)
instance
)

Build a SingletonSupplier with the given singleton instance.

public static <T> SingletonSupplier<T>

Returns:

the singleton supplier (never null)
of
(Supplier<T>
the instance supplier (never null)
supplier
)

Build a SingletonSupplier with the given supplier.

public static <T> SingletonSupplier<T>

Returns:

the singleton supplier, or null if the instance was null
ofNullable
(T
the singleton instance (potentially null)
instance
)

Build a SingletonSupplier with the given singleton instance.

public static <T> SingletonSupplier<T>

Returns:

the singleton supplier, or null if the instance supplier was null
ofNullable
(Supplier<T>
the instance supplier (potentially null)
supplier
)

Build a SingletonSupplier with the given supplier.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultSupplierback to summary
private final Supplier<? extends T> defaultSupplier
Annotations
@Nullable
instanceSupplierback to summary
private final Supplier<? extends T> instanceSupplier
Annotations
@Nullable
singletonInstanceback to summary
private volatile T singletonInstance
Annotations
@Nullable
writeLockback to summary
private final Lock writeLock

Guards access to write operations on the singletonInstance field.

Constructor Detail

SingletonSupplierback to summary
public 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.

Parameters
instance:T

the singleton instance (potentially null)

defaultSupplier:Supplier<? extends T>

the default supplier as a fallback

SingletonSupplierback to summary
public 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.

Parameters
instanceSupplier:Supplier<? extends T>

the immediate instance supplier

defaultSupplier:Supplier<? extends T>

the default supplier as a fallback

SingletonSupplierback to summary
private SingletonSupplier(Supplier<? extends T> supplier)
SingletonSupplierback to summary
private SingletonSupplier(T singletonInstance)

Method Detail

getback to summary
public T get()

Implements java.util.function.Supplier.get.

Get the shared singleton instance for this supplier.

Returns:T

the singleton instance (or null if none)

Annotations
@Override
@Nullable
obtainback to summary
public T obtain()

Obtain the shared singleton instance for this supplier.

Returns:T

the singleton instance (never null)

Exceptions
IllegalStateException:
in case of no instance
ofback to summary
public static <T> SingletonSupplier<T> of(T instance)

Build a SingletonSupplier with the given singleton instance.

Parameters
instance:T

the singleton instance (never null)

Returns:SingletonSupplier<T>

the singleton supplier (never null)

ofback to summary
public static <T> SingletonSupplier<T> of(Supplier<T> supplier)

Build a SingletonSupplier with the given supplier.

Parameters
supplier:Supplier<T>

the instance supplier (never null)

Returns:SingletonSupplier<T>

the singleton supplier (never null)

ofNullableback to summary
public static <T> SingletonSupplier<T> ofNullable(T instance)

Build a SingletonSupplier with the given singleton instance.

Parameters
instance:T

the singleton instance (potentially null)

Returns:SingletonSupplier<T>

the singleton supplier, or null if the instance was null

Annotations
@Nullable
ofNullableback to summary
public static <T> SingletonSupplier<T> ofNullable(Supplier<T> supplier)

Build a SingletonSupplier with the given supplier.

Parameters
supplier:Supplier<T>

the instance supplier (potentially null)

Returns:SingletonSupplier<T>

the singleton supplier, or null if the instance supplier was null

Annotations
@Nullable