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
ConstructorsConstructorDescriptionCreates a newCachingSupplier.CachingSupplier(Supplier<? extends T> supplier) Creates a newCachingSupplier.CachingSupplier(T value) Creates a newCachingSupplier. -
Method Summary
Modifier and TypeMethodDescriptionReturns anOptionalSupplier.Determinismsuitable for thisCachingSupplier.final Tget()Returns the value thisCachingSupplierwill forever supply, computing it if necessary with the first invocation by using theSuppliersupplied at construction time.final booleanSets the value that will be returned forever afterwards by theget()method and returnstrueif and only if the value was previously unset.final StringtoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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- theSupplierthat will be used to supply the value that will be returned by all invocations of theget()method; may benullin which case theget()method will throw aNoSuchElementExceptionuntil, 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 thisCachingSupplierwill forever supply, computing it if necessary with the first invocation by using theSuppliersupplied at construction time.If the
Suppliersupplied at construction time returnsnullfrom itsSupplier.get()method, then this method will forever returnnullas well.- Specified by:
getin interfaceOptionalSupplier<T>- Specified by:
getin 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
Suppliersupplied 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 returnstrueif 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:
trueif and only if this assignment was permitted;falseotherwise- 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.Determinismsuitable 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:
determinismin interfaceOptionalSupplier<T>- Returns:
- one of
OptionalSupplier.Determinism.PRESENTorOptionalSupplier.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-nullStringrepresentation 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:
toStringin classObject- Returns:
- a
Stringrepresentation 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.
-