org.springframework.cache.concurrent
Class ConcurrentCache<K,V>

java.lang.Object
  extended by org.springframework.cache.support.AbstractDelegatingCache<K,V>
      extended by org.springframework.cache.concurrent.ConcurrentCache<K,V>
All Implemented Interfaces:
Cache<K,V>

public class ConcurrentCache<K,V>
extends AbstractDelegatingCache<K,V>

Simple Cache implementation based on the JDK 1.5+ java.util.concurrent package. Useful for testing or simple caching scenarios.

Author:
Costin Leau

Field Summary
 
Fields inherited from class org.springframework.cache.support.AbstractDelegatingCache
NULL_HOLDER
 
Constructor Summary
ConcurrentCache()
           
ConcurrentCache(ConcurrentMap<K,V> delegate, String name)
           
ConcurrentCache(String name)
           
 
Method Summary
 String getName()
          Returns the cache name.
 ConcurrentMap<K,V> getNativeCache()
          Returns the the native, underlying cache provider.
 V putIfAbsent(K key, V value)
          If the specified key is not already associated with a value, associate it with the given value.
 boolean remove(Object key, Object value)
          Remove entry for key only if currently mapped to given value.
 V replace(K key, V value)
          Replace entry for key only if currently mapped to some value.
 boolean replace(K key, V oldValue, V newValue)
          Replace entry for key only if currently mapped to given value.
 
Methods inherited from class org.springframework.cache.support.AbstractDelegatingCache
clear, containsKey, filterNull, get, getAllowNullValues, put, remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConcurrentCache

public ConcurrentCache()

ConcurrentCache

public ConcurrentCache(String name)

ConcurrentCache

public ConcurrentCache(ConcurrentMap<K,V> delegate,
                       String name)
Method Detail

getName

public String getName()
Description copied from interface: Cache
Returns the cache name.

Returns:
the cache name.

getNativeCache

public ConcurrentMap<K,V> getNativeCache()
Description copied from interface: Cache
Returns the the native, underlying cache provider.

Returns:

putIfAbsent

public V putIfAbsent(K key,
                     V value)
Description copied from interface: Cache
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to:
  if (!cache.containsKey(key)) 
     return cache.put(key, value);
  else
     return cache.get(key);
        

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.

remove

public boolean remove(Object key,
                      Object value)
Description copied from interface: Cache
Remove entry for key only if currently mapped to given value. Similar to:
   if ((cache.containsKey(key) && cache.get(key).equals(value)) {
      cache.remove(key);
      return true;
   } 
   else 
      return false;
 

Parameters:
key - key with which the specified value is associated.
value - value associated with the specified key.
Returns:
true if the value was removed, false otherwise

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)
Description copied from interface: Cache
Replace entry for key only if currently mapped to given value. Similar to:
 
  if ((cache.containsKey(key) && cache.get(key).equals(oldValue)) {
     cache.put(key, newValue);
     return true;
 } else return false;
 

Parameters:
key - key with which the specified value is associated.
oldValue - value expected to be associated with the specified key.
newValue - value to be associated with the specified key.
Returns:
true if the value was replaced

replace

public V replace(K key,
                 V value)
Description copied from interface: Cache
Replace entry for key only if currently mapped to some value. Acts as
 
  if ((cache.containsKey(key)) {
     return cache.put(key, value);
 } else return null;
 
except that the action is performed atomically.

Parameters:
key - key with which the specified value is associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.