org.apache.clerezza.triaxrs.util
Class SoftConcurrentMap<K,V>

java.lang.Object
  extended by org.apache.clerezza.triaxrs.util.SoftConcurrentMap<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
SimpleMap<K,V>

public class SoftConcurrentMap<K,V>
extends Object
implements SimpleMap<K,V>

Concurrent implementation of the SimpleMap interface. This implementation is intend to be used by soft caches, therefore the keys are kept in WeakHashMap, while the value is kept using the SoftReference.

Pay attention that put value always returns the current value and not the original value. It was done to allow the following code pattern:

 SoftConcurrentMap<K, V> cache = new SoftConcurrentMap<K, V>();
 V cached = cache.get(key);
 return cached = !null ? cached : cache.put(createValue());
 


Constructor Summary
SoftConcurrentMap()
          Provides the map implementation.
 
Method Summary
 void clear()
          Removes all mappings from this map (optional operation).
 V get(K key)
          Returns the value to which this map maps the specified key.
 V put(K key, V val)
          Associates the specified value with the specified key in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SoftConcurrentMap

public SoftConcurrentMap()
Provides the map implementation.

Parameters:
map -
Method Detail

get

public V get(K key)
Description copied from interface: SimpleMap
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null.

Specified by:
get in interface SimpleMap<K,V>

put

public V put(K key,
             V val)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced by the specified value.

Unlike the regular Map.put method, this method returns the current value and not the previous value.

Note that a copy on write pattern is used where the existing cache is treated as a read only cache and then it is copied to a new cache.

Specified by:
put in interface SimpleMap<K,V>
Returns:
val - the current value.

clear

public void clear()
Description copied from interface: SimpleMap
Removes all mappings from this map (optional operation).

Specified by:
clear in interface SimpleMap<K,V>


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.