The Spring Framework

## org.springframework.util
Class CachingMapDecorator

    java.lang.Object
      org.springframework.util.CachingMapDecorator

All Implemented Interfaces:
  Serializable, Map

    public class CachingMapDecorator

extends Object
implements Map, 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
`Map.Entry<K,V>`

Field Summary
`protected static 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(Map targetMap)` Create a CachingMapDecorator for the given Map.
`CachingMapDecorator(Map targetMap, boolean synchronize, boolean weak)` Create a CachingMapDecorator for the given Map.

Method Summary
`void` `clear()`
`boolean` `containsKey(Object key)`
`boolean` `containsValue(Object value)`
`protected Object` `create(Object key)` Create a value to cache for the given key.
`Set` `entrySet()`
`Object` `get(Object key)` Get value for key.
`boolean` `isEmpty()`
`Set` `keySet()`
`Object` `put(Object key, Object value)` Put an object into the cache, possibly wrapping it with a weak reference.
`void` `putAll(Map map)`
`Object` `remove(Object key)`
`int` `size()`
`String` `toString()`
`protected boolean` `useWeakValue(Object key, Object value)` Decide whether use a weak reference for the value of the given key-value pair.
`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 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(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(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 `Map`

### isEmpty

    public boolean isEmpty()

  Specified by:
    `isEmpty` in interface `Map`

### containsKey

    public boolean containsKey(Object key)

  Specified by:
    `containsKey` in interface `Map`

### containsValue

    public boolean containsValue(Object value)

  Specified by:
    `containsValue` in interface `Map`

### remove

    public Object remove(Object key)

  Specified by:
    `remove` in interface `Map`

### putAll

    public void putAll(Map map)

  Specified by:
    `putAll` in interface `Map`

### clear

    public void clear()

  Specified by:
    `clear` in interface `Map`

### keySet

    public Set keySet()

  Specified by:
    `keySet` in interface `Map`

### values

    public Collection values()

  Specified by:
    `values` in interface `Map`

### entrySet

    public Set entrySet()

  Specified by:
    `entrySet` in interface `Map`

### put

    public Object put(Object key,
                      Object value)

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

  Specified by:
    `put` in interface `Map`
  See Also:
    `useWeakValue(Object, Object)`

### useWeakValue

    protected boolean useWeakValue(Object key,
                                   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 Object get(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 `Map`
  See Also:
    `create(Object)`

### create

    protected Object create(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 String toString()

  Overrides:
    `toString` in class `Object`

The Spring Framework

Copyright © 2002-2008 The Spring Framework.