Package io.fluxcapacitor.common
Interface MemoizingBiFunction<T,U,R>
- Type Parameters:
T
- the first input typeU
- the second input typeR
- the result type
- All Superinterfaces:
BiFunction<T,
U, R>
- All Known Implementing Classes:
DefaultMemoizingBiFunction
A
BiFunction
that memoizes (caches) its results based on a pair of input arguments. The result is stored per
distinct (T, U) key pair and reused for identical subsequent calls.
Use io.fluxcapacitor.javaclient.common.ClientUtils#memoize(BiFunction)
to create an instance.
MemoizingBiFunction<String, Integer, String> repeater =
ClientUtils.memoize((s, i) -> s.repeat(i));
String result = repeater.apply("a", 3); // computes "aaa"
boolean cached = repeater.isCached("a", 3); // true
repeater.remove("a", 3); // evicts the cached result
Time-limited memoization is also supported:
MemoizingBiFunction<String, Locale, Translation> translator =
ClientUtils.memoize(this::translate, Duration.ofMinutes(10));
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears all cached values.default <K1> @NonNull MemoizingFunction
<K1, R> Creates aMemoizingFunction
by composing this memoizing bi-function with an input transformation.void
Applies the given consumer to all currently cached values.boolean
Returnstrue
if the function has already cached a value for the given inputs.Removes the cached result for the given input pair, if present.Methods inherited from interface java.util.function.BiFunction
andThen, apply
-
Method Details
-
isCached
Returnstrue
if the function has already cached a value for the given inputs. -
remove
Removes the cached result for the given input pair, if present.- Returns:
- the previously cached result or
null
-
clear
void clear()Clears all cached values. -
forEach
Applies the given consumer to all currently cached values. -
compose
@NonNull default <K1> @NonNull MemoizingFunction<K1,R> compose(@NonNull @NonNull Function<? super K1, Map.Entry<? extends T, ? extends U>> before) Creates aMemoizingFunction
by composing this memoizing bi-function with an input transformation. This is useful when you want to adapt this function to take a single composite key (like aMap.Entry
).- Type Parameters:
K1
- the type of the composed input- Parameters:
before
- function that transforms the input into apair
of arguments- Returns:
- a composed
MemoizingFunction
-