Interface Memoize<S>
-
- Type Parameters:
S
- Type of the value returned.
- All Superinterfaces:
java.util.function.Supplier<S>
- All Known Subinterfaces:
CloseableMemoize<S>
- All Known Implementing Classes:
CloseableMemoizingSupplier
,MemoizingSupplier
,ReferenceMemoizingSupplier
,RefreshingMemoizingSupplier
public interface Memoize<S> extends java.util.function.Supplier<S>
Memoizing supplier.This type extends
Supplier
and adds apeek()
method as well as some monadic methods.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Memoize<S>
accept(java.util.function.Consumer<? super S> consumer)
Call the consumer with the value of this memoized supplier.default Memoize<S>
filter(java.util.function.Predicate<? super S> predicate)
Filter this memoized supplier to a new memoized supplier.default <R> Memoize<R>
flatMap(java.util.function.Function<? super S,? extends java.util.function.Supplier<? extends R>> mapper)
Flat map this memoized supplier to a new memoized supplier.default <R> Memoize<R>
map(java.util.function.Function<? super S,? extends R> mapper)
Map this memoized supplier to a new memoized supplier.S
peek()
Peek the value.static <T> Memoize<T>
referenceSupplier(java.util.function.Supplier<? extends T> supplier, java.util.function.Function<? super T,? extends java.lang.ref.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(java.util.function.Supplier<? extends T> supplier, long time_to_live, java.util.concurrent.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>supplier(java.util.function.Function<? super T,? extends R> function, T argument)
Creates a supplier which memoizes the value returned by the specified function applied to the specified argument.static <T> Memoize<T>
supplier(java.util.function.Supplier<? extends T> supplier)
Creates a supplier which memoizes the value returned by the specified supplier.
-
-
-
Method Detail
-
supplier
static <T> Memoize<T> supplier(java.util.function.Supplier<? extends T> 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 memoized supplier wrapping the specified supplier.
-
supplier
static <T,R> Memoize<R> supplier(java.util.function.Function<? super T,? extends R> function, T argument)
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 memoized supplier wrapping the specified function and argument.
-
refreshingSupplier
static <T> Memoize<T> refreshingSupplier(java.util.function.Supplier<? extends T> supplier, long time_to_live, java.util.concurrent.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 memoized supplier wrapping the specified supplier.
-
referenceSupplier
static <T> Memoize<T> referenceSupplier(java.util.function.Supplier<? extends T> supplier, java.util.function.Function<? super T,? extends java.lang.ref.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 prior 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 memoized supplier wrapping the specified supplier.
-
peek
S peek()
Peek the value.This method will not result in a call to the source supplier.
- Returns:
- The value if a value is memoized; otherwise
null
.
-
map
default <R> Memoize<R> map(java.util.function.Function<? super S,? extends R> mapper)
Map this memoized supplier to a new memoized supplier.- Type Parameters:
R
- Type of the value returned by the new memoized supplier.- Parameters:
mapper
- The function to map the value of this memoized supplier. Must not benull
.- Returns:
- A new memoized supplier which memoizes the value returned by the specified function.
-
flatMap
default <R> Memoize<R> flatMap(java.util.function.Function<? super S,? extends java.util.function.Supplier<? extends R>> mapper)
Flat map this memoized supplier to a new memoized supplier.- Type Parameters:
R
- Type of the value returned by the new memoized supplier.- Parameters:
mapper
- The function to flat map the value of this memoized supplier to a supplier. Must not benull
. The returned supplier must not benull
.- Returns:
- A new memoized supplier which memoizes the value returned by the supplier returned by the specified function.
-
filter
default Memoize<S> filter(java.util.function.Predicate<? super S> predicate)
Filter this memoized supplier to a new memoized supplier.- Parameters:
predicate
- The predicate to test the value of this memoized supplier. Must not benull
.- Returns:
- A new memoized supplier which memoizes the value returned by this
supplier if the predicate accepts the value or
null
otherwise.
-
-