Author: mwebb
Date: Wed Jul 25 04:19:57 2007
New Revision: 559400

URL: http://svn.apache.org/viewvc?view=rev&rev=559400
Log:
updated to follow the FastHashMap from commons-collections

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=559400&r1=559399&r2=559400
==============================================================================
--- 
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 
Wed Jul 25 04:19:57 2007
@@ -21,10 +21,9 @@
 
 
 import java.util.Collection;
-import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.ReentrantLock;
 
 
@@ -42,8 +41,7 @@
  */
 public class CopyOnWriteMap<K, V> implements Map<K, V>
 {
-    private volatile Map<K, V> modifiableMap;
-    private volatile Map<K, V> constantMap;
+    private HashMap<K, V> internalMap;
 
     transient final ReentrantLock lock = new ReentrantLock();
 
@@ -54,7 +52,7 @@
      */
     public CopyOnWriteMap()
     {
-        update( Collections.<K, V> emptyMap() );
+        internalMap = new HashMap<K,V>();
     }
 
 
@@ -63,49 +61,15 @@
      * initial data being held by this map is contained in
      * the supplied map.
      *
-     * @param modifiableMap
+     * @param internalMap
      *  A Map containing the initial contents to be placed into
      *  this class.
      */
-    public CopyOnWriteMap( Map<K, V> modifiableMap )
+    public CopyOnWriteMap( Map<K, V> data )
     {
-        update( modifiableMap );
+        internalMap = new HashMap<K,V>( data );
     }
 
-
-    /**
-     * This method is the heart of this class.  Any time the 
-     * internals Maps for this class are changed, this method
-     * is called in order to update the internal Maps.
-     *
-     * @param newMap
-     *  A Map that represents the new information.
-     */
-    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
-        {
-            /* 
-             * 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
-        {
-            lock.unlock();
-        }
-    }
-
-
     /**
      * Adds the provided key and value to this map.
      * 
@@ -113,10 +77,11 @@
      */
     public V put( K key, V value )
     {
-        V r = modifiableMap.put( key, value );
-        update( modifiableMap );
-
-        return r;
+        HashMap<K,V> newMap = new HashMap<K,V>( internalMap );
+        V val = newMap.put( key, value );
+        internalMap = newMap;
+        
+        return val;
     }
 
 
@@ -128,10 +93,11 @@
      */
     public V remove( Object key )
     {
-        V r = modifiableMap.remove( key );
-        update( modifiableMap );
-
-        return r;
+        HashMap<K,V> newMap = new HashMap<K,V>( internalMap );
+        V val = newMap.remove( key );
+        internalMap = newMap;
+        
+        return val;
     }
 
 
@@ -143,8 +109,9 @@
      */
     public void putAll( Map<? extends K, ? extends V> newData )
     {
-        modifiableMap.putAll( newData );
-        update( modifiableMap );
+        HashMap<K,V> newMap = new HashMap<K,V>( internalMap );
+        newMap.putAll( newData );
+        internalMap = newMap;
     }
 
 
@@ -155,7 +122,9 @@
      */
     public void clear()
     {
-        update( Collections.<K, V> emptyMap() );
+        HashMap<K,V> newMap = new HashMap<K,V>( internalMap );
+        newMap.clear();
+        internalMap = newMap;
     }
 
 
@@ -170,7 +139,7 @@
      */
     public int size()
     {
-        return constantMap.size();
+        return internalMap.size();
     }
 
 
@@ -181,7 +150,7 @@
      */
     public boolean isEmpty()
     {
-        return constantMap.isEmpty();
+        return internalMap.isEmpty();
     }
 
 
@@ -193,7 +162,7 @@
      */
     public boolean containsKey( Object key )
     {
-        return constantMap.containsKey( key );
+        return internalMap.containsKey( key );
     }
 
 
@@ -205,7 +174,7 @@
      */
     public boolean containsValue( Object value )
     {
-        return constantMap.containsValue( value );
+        return internalMap.containsValue( value );
     }
 
 
@@ -217,7 +186,7 @@
      */
     public V get( Object key )
     {
-        return constantMap.get( key );
+        return internalMap.get( key );
     }
 
 
@@ -226,7 +195,7 @@
      */
     public Set<K> keySet()
     {
-        return constantMap.keySet();
+        return internalMap.keySet();
     }
 
 
@@ -235,7 +204,7 @@
      */
     public Collection<V> values()
     {
-        return constantMap.values();
+        return internalMap.values();
     }
 
 
@@ -244,6 +213,6 @@
      */
     public Set<Entry<K, V>> entrySet()
     {
-        return constantMap.entrySet();
+        return internalMap.entrySet();
     }
 }


Reply via email to