Class MemoizedFunction<T,R>

java.lang.Object
net.neoforged.neoforge.common.util.MemoizedFunction<T,R>
All Implemented Interfaces:
Function<T,R>

public class MemoizedFunction<T,R> extends Object implements Function<T,R>

A memoized proxy wrapping a function, with a clearable cache of values.

Once the function is called with a given key, the returned value is cached and reused until the cache is cleared.

See Also:
  • Field Details

    • function

      protected final Function<T,R> function
    • cache

      protected final Map<T,R> cache
  • Constructor Details

    • MemoizedFunction

      protected MemoizedFunction(Function<T,R> function, Map<T,R> cache)
  • Method Details

    • of

      public static <T,R> MemoizedFunction<T,R> of(Function<T,R> function)
      Wraps and memoizes the given function.
      Parameters:
      function - the function to be wrapped
      Returns:
      the memoized function
    • apply

      public R apply(T t)
      Specified by:
      apply in interface Function<T,R>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clear

      public void clear()
      Clears the cache of this memoized function.