org.springframework.util
Class CachingMapDecorator

java.lang.Object
  extended by org.springframework.util.CachingMapDecorator
All Implemented Interfaces:
java.io.Serializable, java.util.Map

public class CachingMapDecorator
extends java.lang.Object
implements java.util.Map, 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 also 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
protected static java.lang.Object NULL_VALUE
           
 
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 targetMap)
          Create a CachingMapDecorator for the given Map.
CachingMapDecorator(java.util.Map 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)
           
protected  java.lang.Object create(java.lang.Object key)
          Create a value to cache for the given key.
 java.util.Set entrySet()
           
 java.lang.Object get(java.lang.Object key)
          Get value for key.
 boolean isEmpty()
           
 java.util.Set keySet()
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Put an object into the cache, possibly wrapping it with a weak reference.
 void putAll(java.util.Map map)
           
 java.lang.Object remove(java.lang.Object key)
           
 int size()
           
 java.lang.String toString()
           
protected  boolean useWeakValue(java.lang.Object key, java.lang.Object value)
          Decide whether use a weak reference for the value of the given key-value pair.
 java.util.Collection values()
           
 
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

protected static java.lang.Object NULL_VALUE
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 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 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

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map

putAll

public void putAll(java.util.Map map)
Specified by:
putAll in interface java.util.Map

clear

public void clear()
Specified by:
clear in interface java.util.Map

keySet

public java.util.Set keySet()
Specified by:
keySet in interface java.util.Map

values

public java.util.Collection values()
Specified by:
values in interface java.util.Map

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map

put

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

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

useWeakValue

protected boolean useWeakValue(java.lang.Object key,
                               java.lang.Object value)
Decide whether 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 java.lang.Object 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
See Also:
create(Object)

create

protected java.lang.Object create(java.lang.Object 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


Copyright © 2002-2008 The Spring Framework.