Interface Memoize<S>
-
- Type Parameters:
S
- Type of the value returned.
- All Superinterfaces:
java.util.function.Supplier<S>
- All Known Subinterfaces:
CloseableMemoize<S>
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 memoizing supplier.default Memoize<S>
filter(java.util.function.Predicate<? super S> predicate)
Filter this memoizing supplier to a new memoizing supplier.default <R> Memoize<R>
flatMap(java.util.function.Function<? super S,? extends java.util.function.Supplier<? extends R>> mapper)
Flat map this memoizing supplier to a new memoizing supplier.S
get()
Get the memoized value.default Memoize<S>
ifPresent(java.util.function.Consumer<? super S> consumer)
If a value is memoized, call the consumer with the value of this memoizing supplier.boolean
isPresent()
If a value is memoized, returntrue
.default <R> Memoize<R>
map(java.util.function.Function<? super S,? extends R> mapper)
Map this memoizing supplier to a new memoizing supplier.S
peek()
Peek the memoized value, if any.static <T> Memoize<T>
predicateSupplier(java.util.function.Supplier<? extends T> supplier, java.util.function.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(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 memoizing 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 memoizing 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 memoizing 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 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(java.util.function.Supplier<? extends T> supplier, java.util.function.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
-
get
S get()
Get the memoized value.- Specified by:
get
in interfacejava.util.function.Supplier<S>
- Returns:
- The memoized value.
-
peek
S peek()
Peek the memoized value, if any.This method will not result in a call to the source supplier.
- Returns:
- The memoized value if a value is memoized; otherwise
null
.
-
isPresent
boolean isPresent()
If a value is memoized, returntrue
. Otherwise returnfalse
.This method will not result in a call to the source supplier.
- Returns:
true
if a value is memoized; otherwisefalse
.
-
map
default <R> Memoize<R> map(java.util.function.Function<? super S,? extends R> mapper)
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
default <R> Memoize<R> flatMap(java.util.function.Function<? super S,? extends java.util.function.Supplier<? extends R>> mapper)
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
default Memoize<S> filter(java.util.function.Predicate<? super S> predicate)
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
default Memoize<S> accept(java.util.function.Consumer<? super S> consumer)
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.
-
ifPresent
default Memoize<S> ifPresent(java.util.function.Consumer<? super S> consumer)
If a value is memoized, call the consumer with the value of this memoizing supplier. Otherwise do nothing.- 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.
-
-