public abstract class

CachingMapDecorator

extends Object
implements Serializable Map<K, V>
java.lang.Object
   ↳ org.springframework.util.CachingMapDecorator<K, V>

Class Overview

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.

Summary

Public Constructors
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(Map<K, V> targetMap)
Create a CachingMapDecorator for the given Map.
CachingMapDecorator(Map<K, V> targetMap, boolean synchronize, boolean weak)
Create a CachingMapDecorator for the given Map.
Public Methods
void clear()
boolean containsKey(Object key)
boolean containsValue(Object value)
Set<Entry<K, V>> entrySet()
V get(Object key)
Get value for key.
boolean isEmpty()
Set<K> keySet()
V put(K key, V value)
Put an object into the cache, possibly wrapping it with a weak reference.
void putAll(Map<? extends K, ? extends V> map)
V remove(Object key)
int size()
String toString()
Collection<V> values()
Protected Methods
abstract V create(K key)
Create a value to cache for the given key.
boolean useWeakValue(K key, V value)
Decide whether to use a weak reference for the value of the given key-value pair.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.Map

Public Constructors

public CachingMapDecorator ()

Also: SpringCore

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

public CachingMapDecorator (boolean weak)

Also: SpringCore

Create a CachingMapDecorator, using an underlying synchronized Map.

Parameters
weak whether to use weak references for keys and values

public CachingMapDecorator (boolean weak, int size)

Also: SpringCore

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

public CachingMapDecorator (Map<K, V> targetMap)

Also: SpringCore

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

public CachingMapDecorator (Map<K, V> targetMap, boolean synchronize, boolean weak)

Also: SpringCore

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

Public Methods

public void clear ()

Also: SpringCore

public boolean containsKey (Object key)

Also: SpringCore

public boolean containsValue (Object value)

Also: SpringCore

public Set<Entry<K, V>> entrySet ()

Also: SpringCore

public V get (Object key)

Also: SpringCore

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.

See Also

public boolean isEmpty ()

Also: SpringCore

public Set<K> keySet ()

Also: SpringCore

public V put (K key, V value)

Also: SpringCore

Put an object into the cache, possibly wrapping it with a weak reference.

public void putAll (Map<? extends K, ? extends V> map)

Also: SpringCore

public V remove (Object key)

Also: SpringCore

public int size ()

Also: SpringCore

public String toString ()

public Collection<V> values ()

Also: SpringCore

Protected Methods

protected abstract V create (K key)

Also: SpringCore

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

protected boolean useWeakValue (K key, V value)

Also: SpringCore

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.