org.springframework.util
Class CachingMapDecorator<K,V>

java.lang.Object
  extended by org.springframework.util.CachingMapDecorator<K,V>
All Implemented Interfaces:
java.io.Serializable, java.util.Map<K,V>
Direct Known Subclasses:
AbstractCachingLabeledEnumResolver.LabeledEnumCache

public abstract class CachingMapDecorator<K,V>
extends java.lang.Object
implements java.util.Map<K,V>, java.io.Serializable

A simple decorator for a Map, encapsulating the workflow for caching expensive values in a target Map. Supports caching weak or strong keys.

This class is an abstract template. Caching Map implementations should subclass and override the create(key) method which encapsulates expensive creation of a new object.

Since:
1.2.2
Author:
Keith Donald, Juergen Hoeller
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
private static java.lang.Object NULL_VALUE
           
private  boolean synchronize
           
private  java.util.Map<K,java.lang.Object> targetMap
           
private  boolean weak
           
 
Constructor Summary
CachingMapDecorator()
          Create a CachingMapDecorator with strong keys, using an underlying synchronized Map.
CachingMapDecorator(boolean weak)
          Create a CachingMapDecorator, using an underlying synchronized Map.
CachingMapDecorator(boolean weak, int size)
          Create a CachingMapDecorator with initial size, using an underlying synchronized Map.
CachingMapDecorator(java.util.Map<K,V> targetMap)
          Create a CachingMapDecorator for the given Map.
CachingMapDecorator(java.util.Map<K,V> targetMap, boolean synchronize, boolean weak)
          Create a CachingMapDecorator for the given Map.
 
Method Summary
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
private  boolean containsValueOrReference(java.lang.Object value)
           
protected abstract  V create(K key)
          Create a value to cache for the given key.
private  java.util.Set<java.util.Map.Entry<K,V>> entryCopy()
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
           
 V get(java.lang.Object key)
          Get value for key.
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
 V put(K key, V value)
          Put an object into the cache, possibly wrapping it with a weak reference.
 void putAll(java.util.Map<? extends K,? extends V> map)
           
 V remove(java.lang.Object key)
           
 int size()
           
 java.lang.String toString()
           
private  V unwrapReturnValue(java.lang.Object value)
           
protected  boolean useWeakValue(K key, V value)
          Decide whether to use a weak reference for the value of the given key-value pair.
 java.util.Collection<V> values()
           
private  java.util.Collection<V> valuesCopy()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

NULL_VALUE

private static java.lang.Object NULL_VALUE

targetMap

private final java.util.Map<K,java.lang.Object> targetMap

synchronize

private final boolean synchronize

weak

private final boolean weak
Constructor Detail

CachingMapDecorator

public CachingMapDecorator()
Create a CachingMapDecorator with strong keys, using an underlying synchronized Map.


CachingMapDecorator

public CachingMapDecorator(boolean weak)
Create a CachingMapDecorator, using an underlying synchronized Map.

Parameters:
weak - whether to use weak references for keys and values

CachingMapDecorator

public CachingMapDecorator(boolean weak,
                           int size)
Create a CachingMapDecorator with initial size, using an underlying synchronized Map.

Parameters:
weak - whether to use weak references for keys and values
size - the initial cache size

CachingMapDecorator

public CachingMapDecorator(java.util.Map<K,V> targetMap)
Create a CachingMapDecorator for the given Map.

The passed-in Map won't get synchronized explicitly, so make sure to pass in a properly synchronized Map, if desired.

Parameters:
targetMap - the Map to decorate

CachingMapDecorator

public CachingMapDecorator(java.util.Map<K,V> targetMap,
                           boolean synchronize,
                           boolean weak)
Create a CachingMapDecorator for the given Map.

The passed-in Map won't get synchronized explicitly unless you specify "synchronize" as "true".

Parameters:
targetMap - the Map to decorate
synchronize - whether to synchronize on the given Map
weak - whether to use weak references for values
Method Detail

size

public int size()
Specified by:
size in interface java.util.Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<K,V>

containsValueOrReference

private boolean containsValueOrReference(java.lang.Object value)

remove

public V remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<K,V>

unwrapReturnValue

private V unwrapReturnValue(java.lang.Object value)

putAll

public void putAll(java.util.Map<? extends K,? extends V> map)
Specified by:
putAll in interface java.util.Map<K,V>

clear

public void clear()
Specified by:
clear in interface java.util.Map<K,V>

keySet

public java.util.Set<K> keySet()
Specified by:
keySet in interface java.util.Map<K,V>

values

public java.util.Collection<V> values()
Specified by:
values in interface java.util.Map<K,V>

valuesCopy

private java.util.Collection<V> valuesCopy()

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface java.util.Map<K,V>

entryCopy

private java.util.Set<java.util.Map.Entry<K,V>> entryCopy()

put

public V put(K key,
             V value)
Put an object into the cache, possibly wrapping it with a weak reference.

Specified by:
put in interface java.util.Map<K,V>
See Also:
useWeakValue(Object, Object)

useWeakValue

protected boolean useWeakValue(K key,
                               V value)
Decide whether to use a weak reference for the value of the given key-value pair.

Parameters:
key - the candidate key
value - the candidate value
Returns:
true in order to use a weak reference; false otherwise.

get

public V get(java.lang.Object key)
Get value for key. Creates and caches value if it doesn't already exist in the cache.

This implementation is not synchronized: This is highly concurrent but does not guarantee unique instances in the cache, as multiple values for the same key could get created in parallel. Consider overriding this method to synchronize it, if desired.

Specified by:
get in interface java.util.Map<K,V>
See Also:
create(Object)

create

protected abstract V create(K key)
Create a value to cache for the given key. Called by get if there is no value cached already.

Parameters:
key - the cache key
See Also:
get(Object)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object