Top Description Fields Constructors Methods
org.apache.commons.lang3.concurrent

public Class Memoizer<I, O>

extends Object
implements Computable<I, O>
Class Inheritance
All Implemented Interfaces
org.apache.commons.lang3.concurrent.Computable
Type Parameters
<I>
the type of the input to the calculation
<O>
the type of the output of the calculation
Imports
java.util.concurrent.CancellationException, .ConcurrentHashMap, .ConcurrentMap, .ExecutionException, .Future, java.util.function.Function, org.apache.commons.lang3.exception.ExceptionUtils

Definition of an interface for a wrapper around a calculation that takes a single parameter and returns a result. The results for the calculation will be cached for future requests.

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 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.

Since
3.6

Field Summary

Modifier and TypeField and Description
private final ConcurrentMap<I, Future<O>>
private final Function<? super I, ? extends Future<O>>
private final boolean

Constructor Summary

AccessConstructor and Description
public
Memoizer(final Computable<I, O>
the computation whose results should be memorized
computable
)

Constructs a Memoizer for the provided Computable calculation.

public
Memoizer(final Computable<I, O>
the computation whose results should be memorized
computable
,
final boolean
determines whether the computation should be recalculated on subsequent calls if the previous call failed
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.

public
Memoizer(final Function<I, O>
the function whose results should be memorized
function
)

Constructs a Memoizer for the provided Function calculation.

public
Memoizer(final Function<I, O>
the computation whose results should be memorized
function
,
final boolean
determines whether the computation should be recalculated on subsequent calls if the previous call failed
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.

Method Summary

Modifier and TypeMethod and Description
public O

Returns:

the result of the calculation
compute
(final I
the argument for the calculation
arg
)

Implements org.apache.commons.lang3.concurrent.Computable.compute.

This method will return the result of the calculation and cache it, if it has not previously been calculated.

private RuntimeException

Returns:

a RuntimeException, Error or an IllegalStateException
launderException
(final Throwable
the throwable to laundered
throwable
)

This method launders a Throwable to either a RuntimeException, Error or any other Exception wrapped in an IllegalStateException.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private final ConcurrentMap<I, Future<O>> cache
mappingFunctionback to summary
private final Function<? super I, ? extends Future<O>> mappingFunction
recalculateback to summary
private final boolean recalculate

Constructor Detail

Memoizerback to summary
public Memoizer(final Computable<I, O> computable)

Constructs a Memoizer for the provided Computable calculation.

If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.

Parameters
computable:Computable<I, O>

the computation whose results should be memorized

Memoizerback to summary
public Memoizer(final Computable<I, O> computable, final 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.

Parameters
computable:Computable<I, O>

the computation whose results should be memorized

recalculate:boolean

determines whether the computation should be recalculated on subsequent calls if the previous call failed

Memoizerback to summary
public Memoizer(final Function<I, O> function)

Constructs a Memoizer for the provided Function calculation.

If a calculation throws an exception for any reason, this exception will be cached and returned for all future calls with the provided parameter.

Parameters
function:Function<I, O>

the function whose results should be memorized

Since
2.13.0
Memoizerback to summary
public Memoizer(final Function<I, O> function, final 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.

Parameters
function:Function<I, O>

the computation whose results should be memorized

recalculate:boolean

determines whether the computation should be recalculated on subsequent calls if the previous call failed

Since
2.13.0

Method Detail

computeback to summary
public O compute(final I arg) throws InterruptedException

Implements org.apache.commons.lang3.concurrent.Computable.compute.

This method will return the result of the calculation and cache it, if it has not previously been calculated.

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.

Parameters
arg:I

the argument for the calculation

Returns:O

the result of the calculation

Annotations
@Override
Exceptions
InterruptedException:
thrown if the calculation is interrupted
launderExceptionback to summary
private RuntimeException launderException(final Throwable throwable)

This method launders a Throwable to either a RuntimeException, Error or any other Exception wrapped in an IllegalStateException.

Parameters
throwable:Throwable

the throwable to laundered

Returns:RuntimeException

a RuntimeException, Error or an IllegalStateException