org.faktorips.runtime.caching
Class Memoizer<K,V>

java.lang.Object
  extended by org.faktorips.runtime.caching.Memoizer<K,V>
All Implemented Interfaces:
IComputable<K,V>

public class Memoizer<K,V>
extends 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 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
Memoizer(IComputable<K,V> computable)
          The constructor to create a memoizer with default values for the internal ConcurrentHashMap
Memoizer(IComputable<K,V> computable, int initSize, float loadFactor, int concurrencyLevel)
          This constructor needs next to the IComputable also the initial size, the load factor and the concurrency level.
 
Method Summary
 V compute(K key)
          Compute an object of type V identified by the key of type K
 Class<? super V> getValueClass()
          Getting the Class of the value this computable produces.
static RuntimeException launderThrowable(Throwable t)
          Coerce an unchecked Throwable to a RuntimeException

If the Throwable is an Error, throw it; if it is a RuntimeException return it, otherwise throw IllegalStateException

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Memoizer

public Memoizer(IComputable<K,V> computable)
The constructor to create a memoizer with default values for the internal ConcurrentHashMap

Parameters:
computable - the IComputable to load new items

Memoizer

public Memoizer(IComputable<K,V> computable,
                int initSize,
                float loadFactor,
                int concurrencyLevel)
This constructor needs next to the IComputable also the initial size, the load factor and the concurrency level. These parameters are only for tuning purpose and are directly forwarded to the internal ConcurrentHashMap.

Parameters:
computable - The IComputable to load new items
initSize - the initial size @see ConcurrentHashMap
loadFactor - the load factor @see ConcurrentHashMap
concurrencyLevel - the concurrency level @see ConcurrentHashMap
Method Detail

compute

public V compute(K key)
          throws InterruptedException
Description copied from interface: IComputable
Compute an object of type V identified by the key of type K

Specified by:
compute in interface IComputable<K,V>
Parameters:
key - the key to identify the object
Returns:
the computed Object of type V
Throws:
InterruptedException - When computation was interrupted

launderThrowable

public static RuntimeException launderThrowable(Throwable t)
Coerce an unchecked Throwable to a RuntimeException

If the Throwable is an Error, throw it; if it is a RuntimeException return it, otherwise throw IllegalStateException


getValueClass

public Class<? super V> getValueClass()
Description copied from interface: IComputable
Getting the Class of the value this computable produces.

Specified by:
getValueClass in interface IComputable<K,V>


Copyright © 2015. All rights reserved.