Class Memoize

java.lang.Object
org.codehaus.groovy.runtime.memoize.Memoize

public abstract class Memoize extends Object
Implements memoize for Closures. It is supposed to be used by the Closure class itself to implement the memoize() family of methods.
  • Constructor Details

    • Memoize

      public Memoize()
  • Method Details

    • buildMemoizeFunction

      public static <V> Closure<V> buildMemoizeFunction(MemoizeCache<Object,Object> cache, Closure<V> closure)
      Creates a new closure delegating to the supplied one and memoizing all return values by the arguments. The supplied cache is used to store the memoized values and it is the cache's responsibility to put limits on the cache size or implement cache eviction strategy. The LRUCache, for example, allows to set the maximum cache size constraint and implements the LRU (Last Recently Used) eviction strategy. A copy made by dehydrate or rehydrate calls the dehydrated or rehydrated closure and shares the supplied cache.
      Type Parameters:
      V - The closure's return type
      Parameters:
      cache - A map to hold memoized return values
      closure - The closure to memoize
      Returns:
      A new memoized closure
    • buildMemoizeFunction

      public static <V> Closure<V> buildMemoizeFunction(Supplier<? extends MemoizeCache<Object,Object>> cacheFactory, Closure<V> closure)
      Creates a new closure delegating to the supplied one and memoizing all return values by the arguments. The supplied factory creates the cache that stores the memoized values, and it is the cache's responsibility to put limits on the cache size or implement cache eviction strategy. A copy made by dehydrate or rehydrate calls the dehydrated or rehydrated closure and starts with an empty cache created by the same factory, as the cached values were computed against the old owner, delegate and this object. The factory has to be serializable for the memoized closure to be serializable.
      Type Parameters:
      V - The closure's return type
      Parameters:
      cacheFactory - Creates the map to hold memoized return values
      closure - The closure to memoize
      Returns:
      A new memoized closure
    • buildSoftReferenceMemoizeFunction

      public static <V> Closure<V> buildSoftReferenceMemoizeFunction(int protectedCacheSize, MemoizeCache<Object,SoftReference<Object>> cache, Closure<V> closure)
      Creates a new closure delegating to the supplied one and memoizing all return values by the arguments. The memoizing closure will use SoftReferences to remember the return values allowing the garbage collector to reclaim the memory, if needed. The supplied cache is used to store the memoized values and it is the cache's responsibility to put limits on the cache size or implement cache eviction strategy. The LRUCache, for example, allows to set the maximum cache size constraint and implements the LRU (Last Recently Used) eviction strategy. If the protectedCacheSize argument is greater than 0 an optional LRU (Last Recently Used) cache of hard references is maintained to protect recently touched memoized values against eviction by the garbage collector. A copy made by dehydrate or rehydrate calls the dehydrated or rehydrated closure and shares the supplied cache. The memoized values are not serialized with the memoizing closure, which is deserialized with the cache emptied of them.
      Type Parameters:
      V - The closure's return type
      Parameters:
      protectedCacheSize - The number of hard references to keep in order to prevent some (LRU) memoized return values from eviction
      cache - A map to hold memoized return values
      closure - The closure to memoize
      Returns:
      A new memoized closure
    • buildSoftReferenceMemoizeFunction

      public static <V> Closure<V> buildSoftReferenceMemoizeFunction(int protectedCacheSize, Supplier<? extends MemoizeCache<Object,SoftReference<Object>>> cacheFactory, Closure<V> closure)
      Creates a new closure delegating to the supplied one and memoizing all return values by the arguments. The memoizing closure will use SoftReferences to remember the return values allowing the garbage collector to reclaim the memory, if needed. The supplied factory creates the cache that stores the memoized values, and it is the cache's responsibility to put limits on the cache size or implement cache eviction strategy. A copy made by dehydrate or rehydrate calls the dehydrated or rehydrated closure and starts with an empty cache created by the same factory and the same protected cache size, as the cached values were computed against the old owner, delegate and this object. If the protectedCacheSize argument is greater than 0 an optional LRU (Last Recently Used) cache of hard references is maintained to protect recently touched memoized values against eviction by the garbage collector.
      Type Parameters:
      V - The closure's return type
      Parameters:
      protectedCacheSize - The number of hard references to keep in order to prevent some (LRU) memoized return values from eviction
      cacheFactory - Creates the map to hold memoized return values
      closure - The closure to memoize
      Returns:
      A new memoized closure