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>
public class Memoizer<K,V> extends java.lang.Object implements 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 aIComputable. It is extended by the soft reference mechanism so references could be garbage collected in case of memory needs.- Author:
- dirmeier
-
-
Constructor Summary
Constructors Constructor Description Memoizer(IComputable<K,V> computable)The constructor to create aMemoizerwith default values for the internalConcurrentHashMapMemoizer(IComputable<K,V> computable, int initSize, float loadFactor, int concurrencyLevel)This constructor needs next to theIComputablealso the initial size, the load factor and the concurrency level.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vcompute(K key)Compute an object of type V identified by the key of type Kjava.lang.Class<? super V>getValueClass()Getting theClassof the value this computable produces.static java.lang.RuntimeExceptionlaunderThrowable(java.lang.Throwable t)Coerce an unchecked Throwable to a RuntimeExceptionstatic <K,V>
Memoizer<K,V>of(java.lang.Class<? super V> valueClass, java.util.function.Function<K,V> function)Creates a newMemoizerfor the given value class using the givenFunctionto compute the values from keys.
-
-
-
Constructor Detail
-
Memoizer
public Memoizer(IComputable<K,V> computable)
The constructor to create aMemoizerwith default values for the internalConcurrentHashMap- Parameters:
computable- theIComputableto load new items- See Also:
the static factory method to avoid creating the and instead using a .
-
Memoizer
public Memoizer(IComputable<K,V> computable, int initSize, float loadFactor, int concurrencyLevel)
This constructor needs next to theIComputablealso 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- TheIComputableto load new itemsinitSize- the initial size @seeConcurrentHashMaploadFactor- the load factor @seeConcurrentHashMapconcurrencyLevel- the concurrency level @seeConcurrentHashMap
-
-
Method Detail
-
of
public static <K,V> Memoizer<K,V> of(java.lang.Class<? super V> valueClass, java.util.function.Function<K,V> function)
Creates a newMemoizerfor the given value class using the givenFunctionto 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
public V compute(K key) throws java.lang.InterruptedException
Description copied from interface:IComputableCompute an object of type V identified by the key of type K- Specified by:
computein interfaceIComputable<K,V>- Parameters:
key- the key to identify the object- Returns:
- the computed Object of type V
- Throws:
java.lang.InterruptedException- When computation was interrupted
-
launderThrowable
public static java.lang.RuntimeException launderThrowable(java.lang.Throwable t)
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
public java.lang.Class<? super V> getValueClass()
Description copied from interface:IComputableGetting theClassof the value this computable produces.- Specified by:
getValueClassin interfaceIComputable<K,V>
-
-