org.norther.tammi.acorn.util
Class ConcurrentReadMap

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

public class ConcurrentReadMap
extends ConcurrentAbstractMap
implements Cloneable

An implementation of the Map interface that supports thread-safe concurrent reading and exclusive writing. The concurrent reader policy provides a choice for reader oriented tasks that typically initialize a map in one thread after construction and have several readers in multiple threads using the map from there on. The ConcurrentWriteMap class is another alternative for tasks needing concurrent writing as well.

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 implementation is based on the extended semantices of volatile specified by JMM/JSR 133 that is available from jdk 1.5. A synchronized barrier based implementation is provided by the ConcurrentLockMap class.

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

Based on Doug Lea's ConcurrentReaderHashMap .

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

Nested Class Summary
 
Nested classes/interfaces inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap
ConcurrentAbstractMap.Entry
 
Field Summary
protected static int DEFAULT_CAPACITY
          The default capacity.
protected static int MAXIMUM_CAPACITY
          The maximum capacity.
protected static int MINIMUM_CAPACITY
          The minimum capacity.
 
Fields inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap
DEFAULT_LOAD_FACTOR
 
Constructor Summary
ConcurrentReadMap()
          Constructs a new, empty map with a default capacity and load factor.
ConcurrentReadMap(int capacity)
          Constructs a new, empty map with the specified initial capacity and default load factor.
ConcurrentReadMap(int capacity, float factor)
          Constructs a new, empty map with the specified initial capacity and the specified load factor.
ConcurrentReadMap(Map map)
          Constructs a new map with the same mappings as the given map.
 
Method Summary
protected  int capacity(int capacity)
          Returns the appropriate capacity for the expected maximum capacity.
 void clear()
           
 Object clone()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected  ConcurrentAbstractMap.Entry getEntry(Object key)
          Gets the internal entry mapped to the specified key.
protected  Object getInternal(Object key)
          Gets the internal value mapped to the specified key.
protected  ConcurrentAbstractMap.Entry[] getReadTable()
          Gets the internal read table.
protected  ConcurrentAbstractMap.Entry[] getWriteTable()
          Gets the internal write table.
 boolean isEmpty()
           
 void putAll(Map map)
           
protected  Object putInternal(Object key, Object value, boolean r)
          Puts the internal value mapped to the specified key.
protected  void recordWrite(Object x)
          Forces a memory synchronization that will cause all readers to see the table.
protected  void rehash()
          Rehashes the contents of this map into a new table with a larger capacity.
protected  Object removeInternal(Object key, Object value)
          Removes the internal mapping with the optional value.
protected  Object replaceInternal(Object key, Object oldValue, Object newValue)
          Replaces the internal value mapped to the specified key with the optional value.
protected  void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
          Sets the internal write table.
 int size()
           
 
Methods inherited from class org.norther.tammi.acorn.util.ConcurrentAbstractMap
add, capacity, contains, elements, entrySet, get, hash, keys, keySet, put, putIfAbsent, remove, remove, replace, replace, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

MAXIMUM_CAPACITY

protected static final int MAXIMUM_CAPACITY
The maximum capacity.

See Also:
Constant Field Values

MINIMUM_CAPACITY

protected static final int MINIMUM_CAPACITY
The minimum capacity.

See Also:
Constant Field Values

DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY
The default capacity.

See Also:
Constant Field Values
Constructor Detail

ConcurrentReadMap

public ConcurrentReadMap()
Constructs a new, empty map with a default capacity and load factor.


ConcurrentReadMap

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

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

Parameters:
capacity - the initial capacity.
Throws:
IllegalArgumentException - for negative values.

ConcurrentReadMap

public ConcurrentReadMap(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.

ConcurrentReadMap

public ConcurrentReadMap(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

clone

public Object clone()
Overrides:
clone in class ConcurrentAbstractMap

size

public int size()
Specified by:
size in interface Map
Overrides:
size in class AbstractMap

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
Overrides:
isEmpty in class AbstractMap

containsKey

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

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
Overrides:
containsValue in class AbstractMap

putAll

public void putAll(Map map)
Specified by:
putAll in interface Map
Overrides:
putAll in class AbstractMap

clear

public void clear()
Specified by:
clear in interface Map
Overrides:
clear in class AbstractMap

capacity

protected int capacity(int capacity)
Description copied from class: ConcurrentAbstractMap
Returns the appropriate capacity for the expected maximum capacity.

Specified by:
capacity in class ConcurrentAbstractMap
Parameters:
capacity - the desired capacity.
Returns:
the appropriate capacity.

getReadTable

protected ConcurrentAbstractMap.Entry[] getReadTable()
Description copied from class: ConcurrentAbstractMap
Gets the internal read table.

Specified by:
getReadTable in class ConcurrentAbstractMap
Returns:
the read table.

getWriteTable

protected ConcurrentAbstractMap.Entry[] getWriteTable()
Description copied from class: ConcurrentAbstractMap
Gets the internal write table.

Specified by:
getWriteTable in class ConcurrentAbstractMap
Returns:
the write table.

setWriteTable

protected void setWriteTable(ConcurrentAbstractMap.Entry[] tab)
Description copied from class: ConcurrentAbstractMap
Sets the internal write table.

Specified by:
setWriteTable in class ConcurrentAbstractMap
Parameters:
tab - the write table.

recordWrite

protected void recordWrite(Object x)
Forces a memory synchronization that will cause all readers to see the table. Call only when already holding main synch lock. The volatile implemention does nothing.

Parameters:
x - some object to be made visible.

getEntry

protected ConcurrentAbstractMap.Entry getEntry(Object key)
Description copied from class: ConcurrentAbstractMap
Gets the internal entry mapped to the specified key.

Specified by:
getEntry in class ConcurrentAbstractMap
Parameters:
key - the key.
Returns:
the entry or null if not found.

getInternal

protected Object getInternal(Object key)
Description copied from class: ConcurrentAbstractMap
Gets the internal value mapped to the specified key.

Specified by:
getInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
Returns:
the value or null if not found.

putInternal

protected Object putInternal(Object key,
                             Object value,
                             boolean r)
Description copied from class: ConcurrentAbstractMap
Puts the internal value mapped to the specified key.

Specified by:
putInternal in class ConcurrentAbstractMap
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 Object replaceInternal(Object key,
                                 Object oldValue,
                                 Object newValue)
Description copied from class: ConcurrentAbstractMap
Replaces the internal value mapped to the specified key with the optional value.

Specified by:
replaceInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
oldValue - the old value.
newValue - the new value.
Returns:
the previous value or null if not found.

removeInternal

protected Object removeInternal(Object key,
                                Object value)
Description copied from class: ConcurrentAbstractMap
Removes the internal mapping with the optional value.

Specified by:
removeInternal in class ConcurrentAbstractMap
Parameters:
key - the key.
value - the value.
Returns:
the removed value or null if not found.

rehash

protected void rehash()
Rehashes the contents of this map into a new table with a larger capacity. This method is called automatically when the number of keys in this map exceeds its capacity and load factor.



Copyright © 2004 The Norther Organization. All rights reserved.