Interface MemoizingBiFunction<T,U,R>

Type Parameters:
T - the first input type
U - the second input type
R - the result type
All Superinterfaces:
BiFunction<T,U,R>
All Known Implementing Classes:
DefaultMemoizingBiFunction

public interface MemoizingBiFunction<T,U,R> extends BiFunction<T,U,R>
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 Type
    Method
    Description
    void
    Clears all cached values.
    default <K1> @NonNull MemoizingFunction<K1,R>
    compose(@NonNull Function<? super K1,Map.Entry<? extends T,? extends U>> before)
    Creates a MemoizingFunction by composing this memoizing bi-function with an input transformation.
    void
    forEach(Consumer<? super R> consumer)
    Applies the given consumer to all currently cached values.
    boolean
    isCached(T t, U u)
    Returns true if the function has already cached a value for the given inputs.
    remove(T t, U u)
    Removes the cached result for the given input pair, if present.

    Methods inherited from interface java.util.function.BiFunction

    andThen, apply
  • Method Details

    • isCached

      boolean isCached(T t, U u)
      Returns true if the function has already cached a value for the given inputs.
    • remove

      R remove(T t, U u)
      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

      void forEach(Consumer<? super R> consumer)
      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 a MemoizingFunction 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 a Map.Entry).
      Type Parameters:
      K1 - the type of the composed input
      Parameters:
      before - function that transforms the input into a pair of arguments
      Returns:
      a composed MemoizingFunction