You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2007/08/17 09:08:44 UTC
svn commit: r566948 -
/mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
Author: trustin
Date: Fri Aug 17 00:08:43 2007
New Revision: 566948
URL: http://svn.apache.org/viewvc?view=rev&rev=566948
Log:
* Reformatted code
* Removed all SuppressWarning annotations
* Added clone()
* Optimized clear()
Modified:
mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java?view=diff&rev=566948&r1=566947&r2=566948
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/CopyOnWriteMap.java Fri Aug 17 00:08:43 2007
@@ -19,13 +19,11 @@
*/
package org.apache.mina.util;
-
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
-
/**
* A thread-safe version of {@link Map} in which all operations that change the
* Map are implemented by making a new copy of the underlying Map.
@@ -35,23 +33,20 @@
* modify the Map. Therefore the operations that do not cause a change to this
* class happen quickly and concurrently.
*
- * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*/
-public class CopyOnWriteMap<K, V> implements Map<K, V>, Cloneable
-{
- private HashMap<K, V> internalMap;
+public class CopyOnWriteMap<K, V> implements Map<K, V>, Cloneable {
+ private volatile Map<K, V> internalMap;
/**
* Creates a new instance of CopyOnWriteMap.
*
*/
- public CopyOnWriteMap()
- {
- internalMap = new HashMap<K,V>();
+ public CopyOnWriteMap() {
+ internalMap = new HashMap<K, V>();
}
-
/**
* Creates a new instance of CopyOnWriteMap in which the
* initial data being held by this map is contained in
@@ -61,9 +56,8 @@
* A Map containing the initial contents to be placed into
* this class.
*/
- public CopyOnWriteMap( Map<K, V> data )
- {
- internalMap = new HashMap<K,V>( data );
+ public CopyOnWriteMap(Map<K, V> data) {
+ internalMap = new HashMap<K, V>(data);
}
/**
@@ -71,69 +65,55 @@
*
* @see java.util.Map#put(java.lang.Object, java.lang.Object)
*/
- @SuppressWarnings("unchecked")
- public V put( K key, V value )
- {
- synchronized( this ){
- HashMap<K,V> newMap = (HashMap<K, V>)internalMap.clone();
- V val = newMap.put( key, value );
+ public V put(K key, V value) {
+ synchronized (this) {
+ Map<K, V> newMap = new HashMap<K, V>(internalMap);
+ V val = newMap.put(key, value);
internalMap = newMap;
return val;
}
}
-
/**
* Removed the value and key from this map based on the
* provided key.
*
* @see java.util.Map#remove(java.lang.Object)
*/
- @SuppressWarnings("unchecked")
- public V remove( Object key )
- {
- synchronized( this ){
- HashMap<K,V> newMap = (HashMap<K,V>)internalMap.clone();
- V val = newMap.remove( key );
+ public V remove(Object key) {
+ synchronized (this) {
+ Map<K, V> newMap = new HashMap<K, V>(internalMap);
+ V val = newMap.remove(key);
internalMap = newMap;
return val;
}
}
-
/**
* Inserts all the keys and values contained in the
* provided map to this map.
*
* @see java.util.Map#putAll(java.util.Map)
*/
- @SuppressWarnings("unchecked")
- public void putAll( Map<? extends K, ? extends V> newData )
- {
- synchronized( this ){
- HashMap<K,V> newMap = (HashMap<K,V>)internalMap.clone();
- newMap.putAll( newData );
+ public void putAll(Map<? extends K, ? extends V> newData) {
+ synchronized (this) {
+ Map<K, V> newMap = new HashMap<K, V>(internalMap);
+ newMap.putAll(newData);
internalMap = newMap;
}
}
-
/**
* Removes all entries in this map.
*
* @see java.util.Map#clear()
*/
- @SuppressWarnings("unchecked")
- public void clear()
- {
- synchronized( this ){
- HashMap<K,V> newMap = (HashMap<K,V>)internalMap.clone();
- newMap.clear();
- internalMap = newMap;
+ public void clear() {
+ synchronized (this) {
+ internalMap = new HashMap<K, V>();
}
}
-
// ==============================================
// ==== Below are methods that do not modify ====
// ==== the internal Maps ====
@@ -143,82 +123,76 @@
*
* @see java.util.Map#size()
*/
- public int size()
- {
+ public int size() {
return internalMap.size();
}
-
/**
* Returns true if this map is empty, otherwise false.
*
* @see java.util.Map#isEmpty()
*/
- public boolean isEmpty()
- {
+ public boolean isEmpty() {
return internalMap.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 internalMap.containsKey( key );
+ public boolean containsKey(Object key) {
+ return internalMap.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 internalMap.containsValue( value );
+ public boolean containsValue(Object value) {
+ return internalMap.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 internalMap.get( key );
+ public V get(Object key) {
+ return internalMap.get(key);
}
-
/**
* This method will return a read-only {@link Set}.
*/
- public Set<K> keySet()
- {
+ public Set<K> keySet() {
return internalMap.keySet();
}
-
/**
* This method will return a read-only {@link Collection}.
*/
- public Collection<V> values()
- {
+ public Collection<V> values() {
return internalMap.values();
}
-
/**
* This method will return a read-only {@link Set}.
*/
- public Set<Entry<K, V>> entrySet()
- {
+ public Set<Entry<K, V>> entrySet() {
return internalMap.entrySet();
+ }
+
+ @Override
+ public Object clone() {
+ try {
+ return super.clone();
+ } catch (CloneNotSupportedException e) {
+ throw new InternalError();
+ }
}
}