Top Description Inners Fields Constructors Methods
org.codehaus.groovy.runtime.memoize

public abstract Class Memoize

extends Object
Class Inheritance
Imports
groovy.lang.Closure, java.lang.ref.ReferenceQueue, .SoftReference, java.util.Collections

Implements memoize for Closures. It is supposed to be used by the Closure class itself to implement the memoize() family of methods.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
Memoize.MemoizeNullValue

A place-holder for cached null values

private static class

Field Summary

Modifier and TypeField and Description
private static final Memoize.MemoizeNullValue
MEMOIZE_NULL

A place-holder for null values in cache

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <
The closure's return type
V
>
Closure<V>

Returns:

A new memoized closure
buildMemoizeFunction
(final MemoizeCache<Object, Object>
A map to hold memoized return values
cache
,
final Closure<V>
The closure to memoize
closure
)

Creates a new closure delegating to the supplied one and memoizing all return values by the arguments.

public static <
The closure's return type
V
>
Closure<V>

Returns:

A new memoized closure
buildSoftReferenceMemoizeFunction
(final int
The number of hard references to keep in order to prevent some (LRU) memoized return values from eviction
protectedCacheSize
,
final MemoizeCache<Object, SoftReference<Object>>
A map to hold memoized return values
cache
,
final Closure<V>
The closure to memoize
closure
)

Creates a new closure delegating to the supplied one and memoizing all return values by the arguments.

private static Object

Returns:

The key - a list holding all arguments
generateKey
(final Object[]
The arguments supplied to the closure invocation
args
)

Creates a key to use in the memoize cache

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MEMOIZE_NULLback to summary
private static final Memoize.MemoizeNullValue MEMOIZE_NULL

A place-holder for null values in cache

Constructor Detail

Memoizeback to summary
public Memoize()

Method Detail

buildMemoizeFunctionback to summary
public static <V> Closure<V> buildMemoizeFunction(final MemoizeCache<Object, Object> cache, final 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.

Parameters
<V>
The closure's return type
cache:MemoizeCache<Object, Object>

A map to hold memoized return values

closure:Closure<V>

The closure to memoize

Returns:Closure<V>

A new memoized closure

buildSoftReferenceMemoizeFunctionback to summary
public static <V> Closure<V> buildSoftReferenceMemoizeFunction(final int protectedCacheSize, final MemoizeCache<Object, SoftReference<Object>> cache, final 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.

Parameters
<V>
The closure's return type
protectedCacheSize:int

The number of hard references to keep in order to prevent some (LRU) memoized return values from eviction

cache:MemoizeCache<Object, SoftReference<Object>>

A map to hold memoized return values

closure:Closure<V>

The closure to memoize

Returns:Closure<V>

A new memoized closure

generateKeyback to summary
private static Object generateKey(final Object[] args)

Creates a key to use in the memoize cache

Parameters
args:Object[]

The arguments supplied to the closure invocation

Returns:Object

The key - a list holding all arguments

org.codehaus.groovy.runtime.memoize back to summary

private Class Memoize.MemoizeFunction<V>

extends Closure<V>
Class Inheritance
Known Direct Subclasses
org.codehaus.groovy.runtime.memoize.Memoize.SoftReferenceMemoizeFunction

Field Summary

Modifier and TypeField and Description
pack-priv final MemoizeCache<Object, Object>
pack-priv final Closure<V>
private static final long
Inherited from groovy.lang.Closure:
DELEGATE_FIRSTDELEGATE_ONLYDONEIDENTITYmaximumNumberOfParametersOWNER_FIRSTOWNER_ONLYparameterTypesSKIPTO_SELF

Constructor Summary

AccessConstructor and Description
pack-priv
MemoizeFunction(final MemoizeCache<Object, ?> cache, Closure<V> closure)

Method Summary

Modifier and TypeMethod and Description
public V
call(final Object... args)

Overrides groovy.lang.Closure.call.

Invokes the closure with given argument(s), returning any value if applicable.
private static MemoizeCache<K, V>
public V
doCall(final Object... args)

Inherited from groovy.lang.Closure:
andThenandThenSelfandThenSelfasWritablecallcallclonecomposecomposeSelfcomposeSelfcurrycurrydehydrategetDelegategetDirectivegetMaximumNumberOfParametersgetOwnergetParameterTypesgetPropertygetResolveStrategygetThisObjectisCaseleftShiftleftShiftmemoizememoizeAtLeastmemoizeAtMostmemoizeBetweenncurryncurryrcurryrcurryrehydraterightShiftrunsetDelegatesetDirectivesetPropertysetResolveStrategythrowRuntimeExceptiontrampolinetrampoline

Field Detail

cacheback to summary
pack-priv final MemoizeCache<Object, Object> cache
closureback to summary
pack-priv final Closure<V> closure
serialVersionUIDback to summary
private static final long serialVersionUID

Hides groovy.lang.Closure.serialVersionUID.

Constructor Detail

MemoizeFunctionback to summary
pack-priv MemoizeFunction(final MemoizeCache<Object, ?> cache, Closure<V> closure)

Method Detail

callback to summary
public V call(final Object... args)

Overrides groovy.lang.Closure.call.

Doc from groovy.lang.Closure.call.

Invokes the closure with given argument(s), returning any value if applicable.

Returns:V

The value if applicable or null if there is no return statement in the closure.

Annotations
@Override
coerceback to summary
private static MemoizeCache<K, V> coerce(MemoizeCache<Object, ?> cache)
doCallback to summary
public V doCall(final Object... args)
org.codehaus.groovy.runtime.memoize back to summary

private Class Memoize.MemoizeNullValue

extends Object
Class Inheritance

A place-holder for cached null values

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MemoizeNullValueback to summary
private MemoizeNullValue()

Method Detail

equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
org.codehaus.groovy.runtime.memoize back to summary

private Class Memoize.SoftReferenceMemoizeFunction<V>

extends Memoize.MemoizeFunction<V>
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final ProtectionStorage<K, V>
pack-priv final ReferenceQueue<T>
private static final long
Inherited from org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction:
cacheclosure

Constructor Summary

AccessConstructor and Description
pack-priv
SoftReferenceMemoizeFunction(final MemoizeCache<Object, SoftReference<Object>> cache, Closure<V> closure, ProtectionStorage<K, V> lruProtectionStorage, ReferenceQueue<T> queue)

Method Summary

Modifier and TypeMethod and Description
public V
call(final Object... args)

Overrides org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction.call.

Invokes the closure with given argument(s), returning any value if applicable.
private static void
cleanUpNullReferences(final MemoizeCache<Object, Object>
The cache to prune
cache
,
final ReferenceQueue<T>
A reference queue holding references to gc-evicted memoized values
queue
)

After the garbage collector has done its job, we need to clean the cache from references to all the evicted memoized values.

Inherited from org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction:
doCall

Field Detail

lruProtectionStorageback to summary
pack-priv final ProtectionStorage<K, V> lruProtectionStorage
queueback to summary
pack-priv final ReferenceQueue<T> queue
serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction.serialVersionUID.

Constructor Detail

SoftReferenceMemoizeFunctionback to summary
pack-priv SoftReferenceMemoizeFunction(final MemoizeCache<Object, SoftReference<Object>> cache, Closure<V> closure, ProtectionStorage<K, V> lruProtectionStorage, ReferenceQueue<T> queue)

Method Detail

callback to summary
public V call(final Object... args)

Overrides org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction.call.

Doc from groovy.lang.Closure.call.

Invokes the closure with given argument(s), returning any value if applicable.

Returns:V

The value if applicable or null if there is no return statement in the closure.

Annotations
@Override
cleanUpNullReferencesback to summary
private static void cleanUpNullReferences(final MemoizeCache<Object, Object> cache, final ReferenceQueue<T> queue)

After the garbage collector has done its job, we need to clean the cache from references to all the evicted memoized values.

Parameters
cache:MemoizeCache<Object, Object>

The cache to prune

queue:ReferenceQueue<T>

A reference queue holding references to gc-evicted memoized values