org.norther.tammi.acorn.util
Class ConcurrentAbstractMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by org.norther.tammi.acorn.util.ConcurrentAbstractMap
All Implemented Interfaces:
Serializable, ConcurrentMap, Map
Direct Known Subclasses:
ConcurrentReadMap, ConcurrentWriteMap

public abstract class ConcurrentAbstractMap
extends AbstractMap
implements ConcurrentMap, Serializable

An abstract class implementing the ConcurrentMap interface and supporting thread-safe concurrent access with less synchronization than Hashtable.

Iterators and Enumerations return elements reflecting the state of the map at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException.

This class allows null to be used as a key or value.

Based on Doug Lea's ConcurrentHashMap .

Author:
Doug Lea, Ilkka Priha
See Also:
Serialized Form

Nested Class Summary
protected static class ConcurrentAbstractMap.Entry
          An inner class for chained hash lists.
 
Field Summary
protected static float DEFAULT_LOAD_FACTOR
          The default load factor.
 
Constructor Summary
ConcurrentAbstractMap(int capacity, float factor)
          Constructs a new, empty map with the specified initial capacity and the specified load factor.
ConcurrentAbstractMap(Map map)
          Constructs a new map with the same mappings as the given map.
 
Method Summary
 boolean add(Object key, Object value)
          Associates the specified value with the specified key in this map if not already mapped.
protected abstract  int capacity(int capacity)
          Returns the appropriate capacity for the expected maximum capacity.
protected static int capacity(int capacity, int minimum, int maximum)
          Returns the appropriate capacity within the specified limits.
protected  Object clone()
           
 boolean contains(Object value)
          Returns true if this map maps one or more keys to the specified value.
 Enumeration elements()
          Returns an enumeration of the values in this table.
 Set entrySet()
          Returns a collection view of the mappings contained in this map.
 Object get(Object key)
           
protected abstract  ConcurrentAbstractMap.Entry getEntry(Object key)
          Gets the internal entry mapped to the specified key.
protected abstract  Object getInternal(Object key)
          Gets the internal value mapped to the specified key.
protected abstract  ConcurrentAbstractMap.Entry[] getReadTable()
          Gets the internal read table.
protected abstract  ConcurrentAbstractMap.Entry[] getWriteTable()
          Gets the internal write table.
protected static int hash(Object o)
          Returns an improved hash code for an object.
 Enumeration keys()
          Returns an enumeration of the keys in this table.
 Set keySet()
          Returns a set view of the keys contained in this map.
 Object put(Object key, Object value)
           
 Object putIfAbsent(Object key, Object value)
           
protected abstract  Object putInternal(Object key, Object value, boolean r)
          Puts the internal value mapped to the specified key.
 Object remove(Object key)
           
 boolean remove(Object key, Object value)
           
protected abstract  Object removeInternal(Object key, Object value)
          Removes the internal mapping with the optional value.
 Object replace(Object key, Object value)
           
 boolean replace(Object key, Object oldValue, Object newValue)
           
protected abstract  Object replaceInternal(Object key, Object oldValue, Object newValue)
          Replaces the internal value mapped to the specified key with the optional value.
protected abstract  void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
          Sets the internal write table.
 Collection values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap
clear, containsKey, containsValue, equals, hashCode, isEmpty, putAll, size, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, equals, hashCode, isEmpty, putAll, size
 

Field Detail

DEFAULT_LOAD_FACTOR

protected static final float DEFAULT_LOAD_FACTOR
The default load factor.

See Also:
Constant Field Values
Constructor Detail

ConcurrentAbstractMap

public ConcurrentAbstractMap(int capacity,
                             float factor)
Constructs a new, empty map with the specified initial capacity and the specified load factor.

Note that the initial capacity will be rounded to the nearest power of two.

Parameters:
capacity - the initial capacity.
factor - the load factor of the map.
Throws:
IllegalArgumentException - for negative values.

ConcurrentAbstractMap

public ConcurrentAbstractMap(Map map)
Constructs a new map with the same mappings as the given map. The map is created with the default load factor and the corresponding capacity.

Parameters:
map - the map to copy.
Method Detail

hash

protected static final int hash(Object o)
Returns an improved hash code for an object.

Parameters:
o - the object.
Returns:
the hash code.

capacity

protected static final int capacity(int capacity,
                                    int minimum,
                                    int maximum)
Returns the appropriate capacity within the specified limits.

Parameters:
capacity - the desired capacity.
minimum - the minimum capacity.
maximum - the maximum capacity.
Returns:
the capacity rounded to the nearest power of two.

get

public Object get(Object key)
Specified by:
get in interface Map
Overrides:
get in class AbstractMap

put

public Object put(Object key,
                  Object value)
Specified by:
put in interface Map
Overrides:
put in class AbstractMap

putIfAbsent

public Object putIfAbsent(Object key,
                          Object value)
Specified by:
putIfAbsent in interface ConcurrentMap

replace

public Object replace(Object key,
                      Object value)
Specified by:
replace in interface ConcurrentMap

replace

public boolean replace(Object key,
                       Object oldValue,
                       Object newValue)
Specified by:
replace in interface ConcurrentMap

remove

public Object remove(Object key)
Specified by:
remove in interface Map
Overrides:
remove in class AbstractMap

remove

public boolean remove(Object key,
                      Object value)
Specified by:
remove in interface ConcurrentMap

keySet

public Set keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
keySet in interface Map
Overrides:
keySet in class AbstractMap
Returns:
a set view of the keys contained in this map.

values

public Collection values()
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface Map
Overrides:
values in class AbstractMap
Returns:
a collection view of the values contained in this map.

entrySet

public Set entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface Map
Specified by:
entrySet in class AbstractMap
Returns:
a set view of the mappings contained in this map.

keys

public Enumeration keys()
Returns an enumeration of the keys in this table.

Returns:
an enumeration of the keys in this table.

elements

public Enumeration elements()
Returns an enumeration of the values in this table.

Returns:
an enumeration of the values in this table.

contains

public boolean contains(Object value)
Returns true if this map maps one or more keys to the specified value.

Parameters:
value - value whose presence in this map is to be tested.
Returns:
true if this map contains the specified mapping.

add

public boolean add(Object key,
                   Object value)
Associates the specified value with the specified key in this map if not already mapped. This method may be applied in maps supporting nulls instead of the putIfAbsent method.

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
true if added, false otherwise.

clone

protected Object clone()
                throws CloneNotSupportedException
Overrides:
clone in class AbstractMap
Throws:
CloneNotSupportedException

capacity

protected abstract int capacity(int capacity)
Returns the appropriate capacity for the expected maximum capacity.

Parameters:
capacity - the desired capacity.
Returns:
the appropriate capacity.

getReadTable

protected abstract ConcurrentAbstractMap.Entry[] getReadTable()
Gets the internal read table.

Returns:
the read table.

getWriteTable

protected abstract ConcurrentAbstractMap.Entry[] getWriteTable()
Gets the internal write table.

Returns:
the write table.

setWriteTable

protected abstract void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
Sets the internal write table.

Parameters:
tab - the write table.

getEntry

protected abstract ConcurrentAbstractMap.Entry getEntry(Object key)
Gets the internal entry mapped to the specified key.

Parameters:
key - the key.
Returns:
the entry or null if not found.

getInternal

protected abstract Object getInternal(Object key)
Gets the internal value mapped to the specified key.

Parameters:
key - the key.
Returns:
the value or null if not found.

putInternal

protected abstract Object putInternal(Object key,
                                      Object value,
                                      boolean r)
Puts the internal value mapped to the specified key.

Parameters:
key - the key.
value - the value.
r - if true, replace an existing mapping.
Returns:
the previous value or null if not found.

replaceInternal

protected abstract Object replaceInternal(Object key,
                                          Object oldValue,
                                          Object newValue)
Replaces the internal value mapped to the specified key with the optional value.

Parameters:
key - the key.
oldValue - the old value.
newValue - the new value.
Returns:
the previous value or null if not found.

removeInternal

protected abstract Object removeInternal(Object key,
                                         Object value)
Removes the internal mapping with the optional value.

Parameters:
key - the key.
value - the value.
Returns:
the removed value or null if not found.


Copyright © 2004 The Norther Organization. All rights reserved.