Package aQute.bnd.memoize
Interface CloseableMemoize<S extends AutoCloseable>
- Type Parameters:
S
- Type of the value returned.
- All Superinterfaces:
AutoCloseable
,Memoize<S>
,Supplier<S>
- All Known Implementing Classes:
CloseableMemoizingSupplier
Closeable memoizing supplier.
This type extends Memoize
and AutoCloseable
.
-
Method Summary
Modifier and TypeMethodDescriptionCall the consumer with the value of this memoizing supplier.static <T extends AutoCloseable>
CloseableMemoize<T>closeableSupplier
(Supplier<? extends T> supplier) Creates an AutoCloseable supplier which memoizes the AutoCloseable value returned by the specified supplier.get()
Get the memoized AutoCloseable value.If a value is memoized, call the consumer with the value of this memoizing supplier.boolean
isClosed()
Returns whether this memoizing supplier is closed.Methods inherited from interface java.lang.AutoCloseable
close
-
Method Details
-
closeableSupplier
static <T extends AutoCloseable> CloseableMemoize<T> closeableSupplier(Supplier<? extends T> supplier) Creates an AutoCloseable supplier which memoizes the AutoCloseable value returned by the specified supplier.When the returned supplier is called to get a value, it will call the specified supplier at most once to obtain a value.
When
close()
is called on the returned supplier, it will callclose()
on the memoized value, if present, and dereference the value. Afterclose()
is called on the returned supplier, theget()
andaccept(Consumer)
methods of the returned supplier will throw anIllegalStateException
.- Type Parameters:
T
- Type of the value returned by the supplier.- Parameters:
supplier
- The source supplier. Must not benull
. The supplier should not return anull
value. If the supplier does return anull
value, the returned supplier will be marked closed and itsget()
andaccept(Consumer)
methods will throw anIllegalStateException
.- Returns:
- A memoized supplier wrapping the specified supplier.
-
isClosed
boolean isClosed()Returns whether this memoizing supplier is closed.- Returns:
true
If this memoizing supplier is closed; otherwisefalse
.
-
get
S get()Get the memoized AutoCloseable value.- Specified by:
get
in interfaceMemoize<S extends AutoCloseable>
- Specified by:
get
in interfaceSupplier<S extends AutoCloseable>
- Returns:
- The memoized AutoCloseable value.
- Throws:
IllegalStateException
- If this memoizing supplieris closed
.
-
accept
Call the consumer with the value of this memoizing supplier.This method will block closing this memoizing supplier while the consumer is executing.
- Specified by:
accept
in interfaceMemoize<S extends AutoCloseable>
- Parameters:
consumer
- The consumer to accept the value of this memoizing supplier. Must not benull
.- Returns:
- This memoizing supplier.
- Throws:
IllegalStateException
- If this memoizing supplieris closed
.
-
ifPresent
If a value is memoized, call the consumer with the value of this memoizing supplier. Otherwise do nothing.This method will block closing this memoizing supplier while the consumer is executing.
- Specified by:
ifPresent
in interfaceMemoize<S extends AutoCloseable>
- Parameters:
consumer
- The consumer to accept the value of this memoizing supplier if a value is memoized. Must not benull
if a value is memoized.- Returns:
- This memoizing supplier.
-