|
Groovy Documentation | |||||||
| FRAMES NO FRAMES | ||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||
java.lang.Objectgroovy.transform.Memoized
@java.lang.annotation.Retention(RetentionPolicy.SOURCE)
@java.lang.annotation.Target({ ElementType.METHOD })
@GroovyASTTransformationClass("org.codehaus.groovy.transform.MemoizedASTTransformation")
public @interface Memoized
Method annotation that creates a cache for the results of the execution of the annotated method. Whenever the method is called, the mapping between the parameters and the return value is preserved in a cache making subsequent calls with the same arguments fast.
Example usage:
class MemoizedExample {
@Memoized
int sum(int n1, int n2) {
println "$n1 + $n2 = ${n1 + n2}"
n1 + n2
}
}
which becomes (approximately):
class MemoizedExample {
private final Closure memoizedSum = { int n1, int n2 ->
private$method$memoizedSum(n1,n2)
}.memoize()
int sum(int n1, int n2) {
memoizedSum(n1, n2)
}
private private$method$mzmoizeSum(int n1, int n2) {
println "$n1 + $n2 = ${n1 + n2}"
n1 + n2
}
}
Upon execution of this code:
def instance = new MemoizedExample() println instance.sum(1, 2) println instance.sum(1, 2) println instance.sum(2, 3) println instance.sum(2, 3)The following will be output:
1 + 2 = 3 3 3 2 + 3 = 5 5 5
| Optional Element Summary | |
|---|---|
int |
maxCacheSize
The maximum size the cache can grow to. |
int |
protectedCacheSize
Number of cached return values to protect from garbage collection. |
| Method Summary |
|---|
| Methods inherited from class java.lang.Object | |
|---|---|
| java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
| Element Detail |
|---|
public int maxCacheSize
public int protectedCacheSize
Groovy Documentation