Class DefaultMemoizingFunction<K,V>

java.lang.Object
io.fluxcapacitor.common.DefaultMemoizingFunction<K,V>
Type Parameters:
K - the type of input keys
V - the type of values produced by applying the delegate function
All Implemented Interfaces:
MemoizingFunction<K,V>, Function<K,V>

public class DefaultMemoizingFunction<K,V> extends Object implements MemoizingFunction<K,V>
A default implementation of the MemoizingFunction interface that provides caching functionality for computed function results. The results are cached by key, and can be optionally configured with a time-based expiration policy.

This class uses a ConcurrentHashMap internally for thread-safe storage of cached entries.

  • Constructor Details

    • DefaultMemoizingFunction

      public DefaultMemoizingFunction(Function<K,V> delegate)
  • Method Details

    • apply

      public V apply(K key)
      Specified by:
      apply in interface Function<K,V>
    • clear

      public void clear()
      Description copied from interface: MemoizingFunction
      Removes all cached values from this function.
      Specified by:
      clear in interface MemoizingFunction<K,V>
    • remove

      public V remove(K key)
      Description copied from interface: MemoizingFunction
      Removes and returns the cached result for the given key, if present.
      Specified by:
      remove in interface MemoizingFunction<K,V>
      Parameters:
      key - the key whose cached result should be removed
      Returns:
      the previously cached value, or null if not present
    • isCached

      public boolean isCached(K key)
      Description copied from interface: MemoizingFunction
      Checks if the given key has a cached value.
      Specified by:
      isCached in interface MemoizingFunction<K,V>
      Parameters:
      key - the key to test
      Returns:
      true if the value is cached, otherwise false
    • forEach

      public void forEach(Consumer<? super V> consumer)
      Description copied from interface: MemoizingFunction
      Applies the given consumer to all currently cached values.
      Specified by:
      forEach in interface MemoizingFunction<K,V>
      Parameters:
      consumer - a consumer to operate on cached values