com.opensymphony.xwork2.inject.util
Class ReferenceMap<K,V>

java.lang.Object
  extended by com.opensymphony.xwork2.inject.util.ReferenceMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.util.Map<K,V>
Direct Known Subclasses:
ReferenceCache

public class ReferenceMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>, java.io.Serializable

Concurrent hash map that wraps keys and/or values in soft or weak references. Does not support null keys or values. Uses identity equality for weak and soft keys.

The concurrent semantics of ConcurrentHashMap combined with the fact that the garbage collector can asynchronously reclaim and clean up after keys and values at any time can lead to some racy semantics. For example, size() returns an upper bound on the size, i.e. the actual size may be smaller in cases where the key or value has been reclaimed but the map entry has not been cleaned up yet.

Another example: If get(Object) cannot find an existing entry for a key, it will try to create one. This operation is not atomic. One thread could put(Object, Object) a value between the time another thread running get() checks for an entry and decides to create one. In this case, the newly created value will replace the put value in the map. Also, two threads running get() concurrently can potentially create duplicate values for a given key.

In other words, this class is great for caching but not atomicity.

Author:
crazybob@google.com (Bob Lee)
See Also:
Serialized Form

Nested Class Summary
protected static interface ReferenceMap.Strategy
           
 
Constructor Summary
ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType)
          Concurrent hash map that wraps keys and/or values based on specified reference types.
 
Method Summary
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns an unmodifiable set view of the entries in this map.
 V get(java.lang.Object key)
           
protected  com.opensymphony.xwork2.inject.util.ReferenceMap.PutStrategy getPutStrategy()
           
 boolean isEmpty()
           
 java.util.Set<K> keySet()
          Returns an unmodifiable set view of the keys in this map.
 V put(K key, V value)
           
 void putAll(java.util.Map<? extends K,? extends V> t)
           
 V putIfAbsent(K key, V value)
           
protected  ReferenceMap.Strategy putIfAbsentStrategy()
           
protected  ReferenceMap.Strategy putStrategy()
           
 V remove(java.lang.Object key)
           
 boolean remove(java.lang.Object key, java.lang.Object value)
           
 V replace(K key, V value)
           
 boolean replace(K key, V oldValue, V newValue)
           
protected  ReferenceMap.Strategy replaceStrategy()
           
 int size()
           
 java.util.Collection<V> values()
          Returns an unmodifiable set view of the values in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

ReferenceMap

public ReferenceMap(ReferenceType keyReferenceType,
                    ReferenceType valueReferenceType)
Concurrent hash map that wraps keys and/or values based on specified reference types.

Parameters:
keyReferenceType - key reference type
valueReferenceType - value reference type
Method Detail

get

public V get(java.lang.Object key)
Specified by:
get in interface java.util.Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface java.util.Map<K,V>

remove

public V remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<K,V>

size

public int size()
Specified by:
size in interface java.util.Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<K,V>

putAll

public void putAll(java.util.Map<? extends K,? extends V> t)
Specified by:
putAll in interface java.util.Map<K,V>

clear

public void clear()
Specified by:
clear in interface java.util.Map<K,V>

keySet

public java.util.Set<K> keySet()
Returns an unmodifiable set view of the keys in this map. As this method creates a defensive copy, the performance is O(n).

Specified by:
keySet in interface java.util.Map<K,V>

values

public java.util.Collection<V> values()
Returns an unmodifiable set view of the values in this map. As this method creates a defensive copy, the performance is O(n).

Specified by:
values in interface java.util.Map<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V value)

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object value)

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)

replace

public V replace(K key,
                 V value)

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns an unmodifiable set view of the entries in this map. As this method creates a defensive copy, the performance is O(n).

Specified by:
entrySet in interface java.util.Map<K,V>

putStrategy

protected ReferenceMap.Strategy putStrategy()

putIfAbsentStrategy

protected ReferenceMap.Strategy putIfAbsentStrategy()

replaceStrategy

protected ReferenceMap.Strategy replaceStrategy()

getPutStrategy

protected com.opensymphony.xwork2.inject.util.ReferenceMap.PutStrategy getPutStrategy()


Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.