I
- the type of the input to the calculationO
- the type of the output of the calculationpublic class Memoizer<I,O> extends java.lang.Object implements Computable<I,O>
This is not a fully functional cache, there is no way of limiting or removing results once they have been generated. However, it is possible to get the implementation to regenerate the result for a given parameter, if an error was thrown during the previous calculation, by setting the option during the construction of the class. If this is not set the class will return the cached exception.
Thanks should go to Brian Goetz, Tim Peierls and the members of JCP JSR-166 Expert Group for coming up with the original implementation of the class. It was also published within Java Concurrency in Practice as a sample.
Constructor and Description |
---|
Memoizer(Computable<I,O> computable)
Constructs a Memoizer for the provided Computable calculation.
|
Memoizer(Computable<I,O> computable,
boolean recalculate)
Constructs a Memoizer for the provided Computable calculation, with the option of whether a Computation that
experiences an error should recalculate on subsequent calls or return the same cached exception.
|
Memoizer(java.util.function.Function<I,O> function)
Constructs a Memoizer for the provided Function calculation.
|
Memoizer(java.util.function.Function<I,O> function,
boolean recalculate)
Constructs a Memoizer for the provided Function calculation, with the option of whether a Function that
experiences an error should recalculate on subsequent calls or return the same cached exception.
|
Modifier and Type | Method and Description |
---|---|
O |
compute(I arg)
This method will return the result of the calculation and cache it, if it has not previously been calculated.
|
public Memoizer(Computable<I,O> computable)
If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.
computable
- the computation whose results should be memorizedpublic Memoizer(Computable<I,O> computable, boolean recalculate)
computable
- the computation whose results should be memorizedrecalculate
- determines whether the computation should be recalculated on subsequent calls if the previous call
failedpublic Memoizer(java.util.function.Function<I,O> function)
If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.
function
- the function whose results should be memorizedpublic Memoizer(java.util.function.Function<I,O> function, boolean recalculate)
function
- the computation whose results should be memorizedrecalculate
- determines whether the computation should be recalculated on subsequent calls if the previous call
failedpublic O compute(I arg) throws java.lang.InterruptedException
This cache will also cache exceptions that occur during the computation if the recalculate
parameter in the
constructor was set to false
, or not set. Otherwise, if an exception happened on the previous calculation,
the method will attempt again to generate a value.
compute
in interface Computable<I,O>
arg
- the argument for the calculationjava.lang.InterruptedException
- thrown if the calculation is interruptedCopyright © 2010 - 2023 Adobe. All Rights Reserved