Package org.faktorips.runtime.caching
Class Memoizer<K,V>
java.lang.Object
org.faktorips.runtime.caching.Memoizer<K,V>
- All Implemented Interfaces:
IComputable<K,
V>
This Memoizer is implemented as suggested by Brian Goetz in Java Concurrency in Practice. It is a
thread safe caching mechanism that loads not stored object by calling a
IComputable
.
It is extended by the soft reference mechanism so references could be garbage collected in case
of memory needs.- Author:
- dirmeier
-
Constructor Summary
ConstructorsConstructorDescriptionMemoizer
(IComputable<K, V> computable) The constructor to create aMemoizer
with default values for the internalConcurrentHashMap
Memoizer
(IComputable<K, V> computable, int initSize, float loadFactor, int concurrencyLevel) This constructor needs next to theIComputable
also the initial size, the load factor and the concurrency level. -
Method Summary
Modifier and TypeMethodDescriptionCompute an object of type V identified by the key of type KGetting theClass
of the value this computable produces.static RuntimeException
Coerce an unchecked Throwable to a RuntimeExceptionstatic <K,
V> Memoizer<K, V>
-
Constructor Details
-
Memoizer
The constructor to create aMemoizer
with default values for the internalConcurrentHashMap
- Parameters:
computable
- theIComputable
to load new items- See Also:
-
Memoizer
This constructor needs next to theIComputable
also the initial size, the load factor and the concurrency level. These parameters are only for tuning purpose and are directly forwarded to the internalConcurrentHashMap
.- Parameters:
computable
- TheIComputable
to load new itemsinitSize
- the initial size @seeConcurrentHashMap
loadFactor
- the load factor @seeConcurrentHashMap
concurrencyLevel
- the concurrency level @seeConcurrentHashMap
-
-
Method Details
-
of
Creates a newMemoizer
for the given value class using the givenFunction
to compute the values from keys.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
valueClass
- the class of the valuesfunction
- the function to compute a value from a key- Returns:
- a new
IComputable
-
compute
Description copied from interface:IComputable
Compute an object of type V identified by the key of type K- Specified by:
compute
in interfaceIComputable<K,
V> - Parameters:
key
- the key to identify the object- Returns:
- the computed Object of type V
- Throws:
InterruptedException
- When computation was interrupted
-
launderThrowable
Coerce an unchecked Throwable to a RuntimeExceptionIf the Throwable is an Error, throw it; if it is a RuntimeException return it, otherwise throw IllegalStateException
-
getValueClass
Description copied from interface:IComputable
Getting theClass
of the value this computable produces.- Specified by:
getValueClass
in interfaceIComputable<K,
V>
-