Interface Memoize<S>
- Type Parameters:
S
- Type of the value returned.
- All Superinterfaces:
Supplier<S>
- All Known Subinterfaces:
CloseableMemoize<S>
- All Known Implementing Classes:
CloseableMemoizingSupplier
,MemoizingSupplier
,PredicateMemoizingSupplier
,ReferenceMemoizingSupplier
,RefreshingMemoizingSupplier
This type extends Supplier
and adds a peek()
method as well
as some monadic methods.
-
Method Summary
Modifier and TypeMethodDescriptionCall the consumer with the value of this memoizing supplier.Filter this memoizing supplier to a new memoizing supplier.default <R> Memoize<R>
Flat map this memoizing supplier to a new memoizing supplier.default <R> Memoize<R>
Map this memoizing supplier to a new memoizing supplier.peek()
Peek the memoized value, if any.static <T> Memoize<T>
predicateSupplier
(Supplier<? extends T> supplier, Predicate<? super T> predicate) Creates a supplier which memoizes the first value returned by the specified supplier which is accepted by the specified predicate.static <T> Memoize<T>
referenceSupplier
(Supplier<? extends T> supplier, Function<? super T, ? extends Reference<? extends T>> reference) Creates a supplier which memoizes a reference object holding the value returned by the specified supplier.static <T> Memoize<T>
refreshingSupplier
(Supplier<? extends T> supplier, long time_to_live, TimeUnit unit) Creates a supplier which memoizes, for the specified time-to-live, the value returned by the specified supplier.static <T,
R> Memoize<R> Creates a supplier which memoizes the value returned by the specified function applied to the specified argument.static <T> Memoize<T>
Creates a supplier which memoizes the value returned by the specified supplier.
-
Method Details
-
supplier
Creates a supplier which memoizes the 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.
- Type Parameters:
T
- Type of the value returned by the supplier.- Parameters:
supplier
- The source supplier. Must not benull
.- Returns:
- A memoizing supplier wrapping the specified supplier.
-
supplier
Creates a supplier which memoizes the value returned by the specified function applied to the specified argument.When the returned supplier is called to get a value, it will call the specified function applied to the specified argument at most once to obtain a value.
- Type Parameters:
T
- Type of the value returned by the supplier.- Parameters:
function
- The source function. Must not benull
.argument
- The argument to the source function.- Returns:
- A memoizing supplier wrapping the specified function and argument.
-
refreshingSupplier
static <T> Memoize<T> refreshingSupplier(Supplier<? extends T> supplier, long time_to_live, TimeUnit unit) Creates a supplier which memoizes, for the specified time-to-live, the value returned by the specified supplier.When the returned supplier is called to get a value, it will call the specified supplier to obtain a new value if any prior obtained value is older than the specified time-to-live.
- Type Parameters:
T
- Type of the value returned by the supplier.- Parameters:
supplier
- The source supplier. Must not benull
.time_to_live
- The time-to-live for a value. Negative values are treated as zero.unit
- The time unit of the time-to-live value. Must not benull
.- Returns:
- A memoizing supplier wrapping the specified supplier.
-
referenceSupplier
static <T> Memoize<T> referenceSupplier(Supplier<? extends T> supplier, Function<? super T, ? extends Reference<? extends T>> reference) Creates a supplier which memoizes a reference object holding the value returned by the specified supplier.When the returned supplier is called to get a value, if the reference object holding any previously obtained value is cleared, then the specified supplier is called to obtain a new value and the specified reference function is called to wrap the new value in a reference object.
- 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 since a cleared reference also returnsnull
.reference
- A function which is called to wrap an object created by the specified supplier in a reference object. This allows the caller to control the reference type and whether a reference queue is used. The function must not returnnull
.- Returns:
- A memoizing supplier wrapping the specified supplier.
-
predicateSupplier
static <T> Memoize<T> predicateSupplier(Supplier<? extends T> supplier, Predicate<? super T> predicate) Creates a supplier which memoizes the first value returned by the specified supplier which is accepted by the specified predicate.When the returned supplier is called to get a value, it will call the specified supplier to obtain a value and then call the specified predicate to ask if the value is acceptable. If the value is not accepted by the predicate, the value is not memoized before it is returned. If the value is accepted by the predicate, the value is memoized before it is returned and the specified supplier and specified predicate will no longer be called.
- Type Parameters:
T
- Type of the value returned by the supplier.- Parameters:
supplier
- The source supplier. Must not benull
.predicate
- The accepting predicate. Must not benull
.- Returns:
- A memoizing supplier wrapping the specified supplier and specified predicate.
- Since:
- 1.1
-
peek
S peek()Peek the memoized value, if any.This method will not result in a call to the source supplier.
- Returns:
- The value if a value is memoized; otherwise
null
.
-
map
Map this memoizing supplier to a new memoizing supplier.- Type Parameters:
R
- Type of the value returned by the new memoizing supplier.- Parameters:
mapper
- The function to map the value of this memoizing supplier. Must not benull
.- Returns:
- A new memoizing supplier which memoizes the value returned by the specified function.
-
flatMap
Flat map this memoizing supplier to a new memoizing supplier.- Type Parameters:
R
- Type of the value returned by the new memoizing supplier.- Parameters:
mapper
- The function to flat map the value of this memoizing supplier to a supplier. Must not benull
. The returned supplier must not benull
.- Returns:
- A new memoizing supplier which memoizes the value returned by the supplier returned by the specified function.
-
filter
Filter this memoizing supplier to a new memoizing supplier.- Parameters:
predicate
- The predicate to test the value of this memoizing supplier. Must not benull
.- Returns:
- A new memoizing supplier which memoizes the value returned by
this supplier if the predicate accepts the value or
null
otherwise.
-
accept
Call the consumer with the value of this memoizing supplier.- Parameters:
consumer
- The consumer to accept the value of this memoizing supplier. Must not benull
.- Returns:
- This memoizing supplier.
-