You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by mw...@apache.org on 2007/07/25 04:34:28 UTC
svn commit: r559288 -
/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
Author: mwebb
Date: Tue Jul 24 19:34:27 2007
New Revision: 559288
URL: http://svn.apache.org/viewvc?view=rev&rev=559288
Log:
added comments and cleaned up the code a bit
Modified:
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
Modified: mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java?view=diff&rev=559288&r1=559287&r2=559288
==============================================================================
--- mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java (original)
+++ mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java Tue Jul 24 19:34:27 2007
@@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
@@ -41,8 +42,8 @@
*/
public class CopyOnWriteMap<K, V> implements Map<K, V>
{
- private volatile Map<K, V> modifiableReference;
- private volatile Map<K, V> constantReference;
+ private volatile Map<K, V> modifiableMap;
+ private volatile Map<K, V> constantMap;
transient final ReentrantLock lock = new ReentrantLock();
@@ -59,16 +60,16 @@
/**
* Creates a new instance of CopyOnWriteMap in which the
- * initial data being held by this class is contained in
- * the supplied Map.
+ * initial data being held by this map is contained in
+ * the supplied map.
*
- * @param modifiableReference
+ * @param modifiableMap
* A Map containing the initial contents to be placed into
* this class.
*/
- public CopyOnWriteMap( Map<K, V> data )
+ public CopyOnWriteMap( Map<K, V> modifiableMap )
{
- update( data );
+ update( modifiableMap );
}
@@ -80,14 +81,23 @@
* @param newMap
* A Map that represents the new information.
*/
- private void update( Map<K,V> newMap )
+ private void update( Map<K, V> newMap )
{
final ReentrantLock lock = this.lock;
lock.lock();
+
+ /*
+ * by locking this operation, we ensure that the modifiableMap
+ * and the constantMap contain the identical information.
+ */
try
{
- modifiableReference = newMap;
- constantReference = Collections.unmodifiableMap( modifiableReference );
+ /*
+ * a ConcurrentHashMap was selected in hopes of making this
+ * class as thread-safe as possible.
+ */
+ modifiableMap = new ConcurrentHashMap<K, V>( newMap );
+ constantMap = Collections.unmodifiableMap( modifiableMap );
}
finally
{
@@ -96,37 +106,56 @@
}
+ /**
+ * Adds the provided key and value to this map.
+ *
+ * @see java.util.Map#put(java.lang.Object, java.lang.Object)
+ */
public V put( K key, V value )
{
- V r = modifiableReference.put( key, value );
- update( modifiableReference );
+ V r = modifiableMap.put( key, value );
+ update( modifiableMap );
return r;
}
+ /**
+ * Removed the value and key from this map based on the
+ * provided key.
+ *
+ * @see java.util.Map#remove(java.lang.Object)
+ */
public V remove( Object key )
{
- V r = modifiableReference.remove( key );
- update( modifiableReference );
+ V r = modifiableMap.remove( key );
+ update( modifiableMap );
return r;
}
+ /**
+ * Inserts all the keys and values contained in the
+ * provided map to this map.
+ *
+ * @see java.util.Map#putAll(java.util.Map)
+ */
public void putAll( Map<? extends K, ? extends V> newData )
{
- modifiableReference.putAll( newData );
- update( modifiableReference );
+ modifiableMap.putAll( newData );
+ update( modifiableMap );
}
/**
+ * Removes all entries in this map.
+ *
* @see java.util.Map#clear()
*/
public void clear()
{
- update( Collections.<K,V>emptyMap() );
+ update( Collections.<K, V> emptyMap() );
}
@@ -135,47 +164,60 @@
// ==== the internal Maps ====
// ==============================================
/**
+ * Returns the number of key/value pairs in this map.
+ *
* @see java.util.Map#size()
*/
public int size()
{
- return modifiableReference.size();
+ return constantMap.size();
}
/**
+ * Returns true if this map is empty, otherwise false.
+ *
* @see java.util.Map#isEmpty()
*/
public boolean isEmpty()
{
- return modifiableReference.isEmpty();
+ return constantMap.isEmpty();
}
/**
+ * Returns true if this map contains the provided key, otherwise
+ * this method return false.
+ *
* @see java.util.Map#containsKey(java.lang.Object)
*/
public boolean containsKey( Object key )
{
- return modifiableReference.containsKey( key );
+ return constantMap.containsKey( key );
}
/**
+ * Returns true if this map contains the provided value, otherwise
+ * this method returns false.
+ *
* @see java.util.Map#containsValue(java.lang.Object)
*/
public boolean containsValue( Object value )
{
- return modifiableReference.containsValue( value );
+ return constantMap.containsValue( value );
}
/**
+ * Returns the value associated with the provided key from this
+ * map.
+ *
* @see java.util.Map#get(java.lang.Object)
*/
public V get( Object key )
{
- return modifiableReference.get( key );
+ return constantMap.get( key );
}
@@ -184,7 +226,7 @@
*/
public Set<K> keySet()
{
- return constantReference.keySet();
+ return constantMap.keySet();
}
@@ -193,7 +235,7 @@
*/
public Collection<V> values()
{
- return constantReference.values();
+ return constantMap.values();
}
@@ -202,6 +244,6 @@
*/
public Set<Entry<K, V>> entrySet()
{
- return constantReference.entrySet();
+ return constantMap.entrySet();
}
}