org.spicefactory.lib.reflect.util
Class Cache<K,V>

java.lang.Object
  extended by org.spicefactory.lib.reflect.util.Cache<K,V>

public class Cache<K,V>
extends Object

Cache implementation optimized for concurrent access to objects which are expensive to create.

It uses a Factory which can be specified by applications to create new objects whenever there is a request for a key for which no instance has been created yet.

This implementation is based on the principles outlined in the chapter "Building an efficient, scalable result cache" from the book "Java Concurrency in Practice" by Brian Goetz.

Author:
Jens Halm

Nested Class Summary
static interface Cache.Factory<K,V>
          A Factory is used by cache instances to create objects whenever there is a request for a key for which no instance has been created yet.
 
Constructor Summary
Cache(Cache.Factory<K,V> factory)
          Creates a new Cache instance.
 
Method Summary
 V get(K key)
          Returns the object for the specified key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(Cache.Factory<K,V> factory)
Creates a new Cache instance. The specified factory instance will be used whenever there is a request for a key for which no instance has been created yet.

Parameters:
factory - the factory to use for creating new instances
Method Detail

get

public V get(K key)
Returns the object for the specified key. If no such object exists, a new one will be created using the factory that was passed to the constructor of this cache.

Parameters:
key - the key to return the corresponding instance for
Returns:
the instance which is mapped to the specified key