java.lang.Object
org.microbean.invoke.CachingSupplier<T>
- Type Parameters:
T
- The type of object returned by theget()
method
- All Implemented Interfaces:
Supplier<T>
,OptionalSupplier<T>
An
OptionalSupplier
that computes the value it will
return from its get()
method when that method is first
invoked, and that returns that computed value for all subsequent
invocations of that method.- Author:
- Laird Nelson
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.microbean.invoke.OptionalSupplier
OptionalSupplier.Determinism
-
Constructor Summary
ConstructorDescriptionCreates a newCachingSupplier
.CachingSupplier
(Supplier<? extends T> supplier) Creates a newCachingSupplier
.CachingSupplier
(T value) Creates a newCachingSupplier
. -
Method Summary
Modifier and TypeMethodDescriptionReturns anOptionalSupplier.Determinism
suitable for thisCachingSupplier
.final T
get()
Returns the value thisCachingSupplier
will forever supply, computing it if necessary with the first invocation by using theSupplier
supplied at construction time.final boolean
Sets the value that will be returned forever afterwards by theget()
method and returnstrue
if and only if the value was previously unset.final String
toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.microbean.invoke.OptionalSupplier
exceptionally, handle, ifPresent, ifPresentOrElse, optional, optional, optional, orElse, orElseGet, orElseThrow, orElseThrow, stream
-
Constructor Details
-
CachingSupplier
public CachingSupplier()Creates a newCachingSupplier
. -
CachingSupplier
Creates a newCachingSupplier
.An invocation of this constructor will result in the
set(Object)
method always returningfalse
. -
CachingSupplier
Creates a newCachingSupplier
.- Parameters:
supplier
- theSupplier
that will be used to supply the value that will be returned by all invocations of theget()
method; may benull
in which case theget()
method will throw aNoSuchElementException
until, at least, theset(Object)
method is called; must be safe for concurrent use by multiple threads and must be side-effect free- See Also:
-
-
Method Details
-
get
Returns the value thisCachingSupplier
will forever supply, computing it if necessary with the first invocation by using theSupplier
supplied at construction time.If the
Supplier
supplied at construction time returnsnull
from itsSupplier.get()
method, then this method will forever returnnull
as well.- Specified by:
get
in interfaceOptionalSupplier<T>
- Specified by:
get
in interfaceSupplier<T>
- Returns:
- the value, which may very well be
null
- See Also:
- Idempotency:
- This method's idempotency and determinism are
determined by the idempotency and determinisim of the
Supplier
supplied at construction time. - Nullability:
- This method may return
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
set
Sets the value that will be returned forever afterwards by theget()
method and returnstrue
if and only if the value was previously unset.- Parameters:
newValue
- the new value that will be returned by theget()
method forever afterwards; may benull
- Returns:
true
if and only if this assignment was permitted;false
otherwise- See Also:
- Idempotency:
- This method is idempotent but not deterministic.
- Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
determinism
Returns anOptionalSupplier.Determinism
suitable for thisCachingSupplier
.In most cases, this method returns
OptionalSupplier.Determinism.PRESENT
. In the case that the zero-argument constructor was invoked, and theset(Object)
method has not yet been called, this method will returnOptionalSupplier.Determinism.DETERMINISTIC
. Once theset(Object)
method has been called, this method will returnOptionalSupplier.Determinism.PRESENT
.- Specified by:
determinism
in interfaceOptionalSupplier<T>
- Returns:
- one of
OptionalSupplier.Determinism.PRESENT
orOptionalSupplier.Determinism.DETERMINISTIC
- Idempotency:
- This method is idempotent and deterministic.
- Nullability:
- This method does not return
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-
toString
Returns a non-null
String
representation of thisCachingSupplier
.The format of the return value is deliberately unspecified and subject to change without notice from version to version of this class.
- Overrides:
toString
in classObject
- Returns:
- a
String
representation of thisCachingSupplier
- Idempotency:
- This method is idempotent but not deterministic
until a value has been supplied, either via the
CachingSupplier(Object)
orCachingSupplier(Supplier)
constructors or theset(Object)
method. - Nullability:
- This method never returns
null
. - Thread Safety:
- This method is safe for concurrent use by multiple threads.
-