Class AbstractCachingMapDecorator<K,V>
java.lang.Object
org.springframework.binding.collection.AbstractCachingMapDecorator<K,V>
- All Implemented Interfaces:
Serializable
,Map<K,
V>
public abstract class AbstractCachingMapDecorator<K,V>
extends Object
implements Map<K,V>, 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:
- 2.4
- Author:
- Keith Donald, Juergen Hoeller
- See Also:
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a CachingMapDecorator with strong keys, using an underlying synchronized Map.AbstractCachingMapDecorator
(boolean weak) Create a CachingMapDecorator, using an underlying synchronized Map.AbstractCachingMapDecorator
(boolean weak, int size) Create a CachingMapDecorator with initial size, using an underlying synchronized Map.AbstractCachingMapDecorator
(Map<K, V> targetMap) Create a CachingMapDecorator for the given Map.AbstractCachingMapDecorator
(Map<K, V> targetMap, boolean synchronize, boolean weak) Create a CachingMapDecorator for the given Map. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) protected abstract V
Create a value to cache for the given key.entrySet()
Get value for key.boolean
isEmpty()
keySet()
Put an object into the cache, possibly wrapping it with a weak reference.void
int
size()
toString()
protected boolean
useWeakValue
(K key, V value) Decide whether to use a weak reference for the value of the given key-value pair.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
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
AbstractCachingMapDecorator
public AbstractCachingMapDecorator()Create a CachingMapDecorator with strong keys, using an underlying synchronized Map. -
AbstractCachingMapDecorator
public AbstractCachingMapDecorator(boolean weak) Create a CachingMapDecorator, using an underlying synchronized Map.- Parameters:
weak
- whether to use weak references for keys and values
-
AbstractCachingMapDecorator
public AbstractCachingMapDecorator(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 valuessize
- the initial cache size
-
AbstractCachingMapDecorator
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
-
AbstractCachingMapDecorator
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 decoratesynchronize
- whether to synchronize on the given Mapweak
- whether to use weak references for values
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
put
Put an object into the cache, possibly wrapping it with a weak reference. -
useWeakValue
Decide whether to use a weak reference for the value of the given key-value pair.- Parameters:
key
- the candidate keyvalue
- the candidate value- Returns:
true
in order to use a weak reference;false
otherwise.
-
get
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.
-
create
Create a value to cache for the given key. Called byget
if there is no value cached already.- Parameters:
key
- the cache key- See Also:
-
toString
-