org.sourceforge.jemm.util.managed
Class EnhancedConcurrentHashMap<K,V>

java.lang.Object
  extended by org.sourceforge.jemm.util.managed.EnhancedConcurrentHashMap<K,V>
Type Parameters:
K - The key type.
V - The value type
All Implemented Interfaces:
java.util.concurrent.ConcurrentMap<K,V>, java.util.Map<K,V>, EnhancedConcurrentMap<K,V>

public class EnhancedConcurrentHashMap<K,V>
extends java.lang.Object
implements EnhancedConcurrentMap<K,V>

EnhancedConcurrentHashMap is similar to ConcurrentHashMap, but adds some extra features. The main extra method is 'perform' that allows an arbitrary action to be performed whilst the lock on the given key is held (even if there is no entry in the map beforehand. N.B This map does not support null values, if put(key,null) is used, contains(key) will return false.

Author:
Rory Graves

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
static int DEFAULT_NO_BUCKETS
          The default number of locking buckets.
 
Constructor Summary
  EnhancedConcurrentHashMap()
          Creates an EnhancedConcurrentHashMap with a default number of buckets.
protected EnhancedConcurrentHashMap(ECMapBucketFactory<K,V> bucketFactory, int noBuckets)
           
  EnhancedConcurrentHashMap(int noBuckets)
          Creates an EnhancedConcurrentHashMap with the given number of internal buckets.
 
Method Summary
 void clear()
          Clear all the entries in this map.
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
           
 V get(java.lang.Object key)
           
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
 void perform(K key, ECMAction<K,V> action)
          Perform the given action whilst holding the lock on the given key.
 V put(K key, V value)
           
 void putAll(java.util.Map<? extends K,? extends V> m)
           
 V putIfAbsent(K key, V value)
          Atomically associate a value with a key, if a value is not already assocated with the given key.
 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)
           
 int size()
           
 java.util.Collection<V> values()
           
 
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
 

Field Detail

DEFAULT_NO_BUCKETS

public static final int DEFAULT_NO_BUCKETS
The default number of locking buckets.

See Also:
Constant Field Values
Constructor Detail

EnhancedConcurrentHashMap

public EnhancedConcurrentHashMap()
Creates an EnhancedConcurrentHashMap with a default number of buckets.


EnhancedConcurrentHashMap

public EnhancedConcurrentHashMap(int noBuckets)
Creates an EnhancedConcurrentHashMap with the given number of internal buckets.

Parameters:
noBuckets -

EnhancedConcurrentHashMap

protected EnhancedConcurrentHashMap(ECMapBucketFactory<K,V> bucketFactory,
                                    int noBuckets)
Method Detail

perform

public void perform(K key,
                    ECMAction<K,V> action)
Perform the given action whilst holding the lock on the given key. The action is able to access, update or clear the value associated with the given key as part of the action.

Specified by:
perform in interface EnhancedConcurrentMap<K,V>
Parameters:
key - The key to lock during the action.
action - The action to perform whilst the lock is held.

putIfAbsent

public V putIfAbsent(K key,
                     V value)
Atomically associate a value with a key, if a value is not already assocated with the given key.

Specified by:
putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,V>
Parameters:
key - The key
value - The new value to associate.
Returns:
The old value associated with 'key'
See Also:
ConcurrentHashMap.putIfAbsent(Object,Object)

remove

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

replace

public V replace(K key,
                 V value)
Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V>

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)
Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V>

clear

public void clear()
Clear all the entries in this map. N.B. This is not a thread safe operation, inserts that occur simultaneously may be lost or survive at random.

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

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>

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface java.util.Map<K,V>

get

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

isEmpty

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

keySet

public java.util.Set<K> keySet()
Specified by:
keySet 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>

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)
Specified by:
putAll 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>

values

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


Copyright © 2009. All Rights Reserved.