Author: mwebb
Date: Tue Jul 24 18:46:50 2007
New Revision: 559276
URL: http://svn.apache.org/viewvc?view=rev&rev=559276
Log:
performming a little housekeeping as I get this all integrated into Eclipse
Added:
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/
Removed:
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/App.java
mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/AppTest.java
Added:
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=auto&rev=559276
==============================================================================
---
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
(added)
+++
mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/CopyOnWriteMap.java
Tue Jul 24 18:46:50 2007
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.util;
+
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.locks.ReentrantLock;
+
+
+/**
+ * A thread-safe version of [EMAIL PROTECTED] Map} in which all operations
that change the
+ * Map are implemented by making a new copy of the underlying Map.
+ *
+ * While the creation of a new Map can be expensive, this class is designed for
+ * cases in which the primary function is to read data from the Map, not to
+ * modify the Map. Therefore the operations that do not cause a change to
this
+ * class happen quickly and concurrently.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class CopyOnWriteMap<K, V> implements Map<K, V>
+{
+ private volatile Map<K, V> modifiableReference;
+ private volatile Map<K, V> constantReference;
+
+ transient final ReentrantLock lock = new ReentrantLock();
+
+
+ /**
+ * Creates a new instance of CopyOnWriteMap.
+ *
+ */
+ public CopyOnWriteMap()
+ {
+ update( Collections.<K, V> emptyMap() );
+ }
+
+
+ /**
+ * Creates a new instance of CopyOnWriteMap in which the
+ * initial data being held by this class is contained in
+ * the supplied Map.
+ *
+ * @param modifiableReference
+ * A Map containing the initial contents to be placed into
+ * this class.
+ */
+ public CopyOnWriteMap( Map<K, V> data )
+ {
+ update( 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();
+ try
+ {
+ modifiableReference = newMap;
+ constantReference = Collections.unmodifiableMap(
modifiableReference );
+ }
+ finally
+ {
+ lock.unlock();
+ }
+ }
+
+
+ public V put( K key, V value )
+ {
+ V r = modifiableReference.put( key, value );
+ update( modifiableReference );
+
+ return r;
+ }
+
+
+ public V remove( Object key )
+ {
+ V r = modifiableReference.remove( key );
+ update( modifiableReference );
+
+ return r;
+ }
+
+
+ public void putAll( Map<? extends K, ? extends V> newData )
+ {
+ modifiableReference.putAll( newData );
+ update( modifiableReference );
+ }
+
+
+ /**
+ * @see java.util.Map#clear()
+ */
+ public void clear()
+ {
+ update( Collections.<K,V>emptyMap() );
+ }
+
+
+ // ==============================================
+ // ==== Below are methods that do not modify ====
+ // ==== the internal Maps ====
+ // ==============================================
+ /**
+ * @see java.util.Map#size()
+ */
+ public int size()
+ {
+ return modifiableReference.size();
+ }
+
+
+ /**
+ * @see java.util.Map#isEmpty()
+ */
+ public boolean isEmpty()
+ {
+ return modifiableReference.isEmpty();
+ }
+
+
+ /**
+ * @see java.util.Map#containsKey(java.lang.Object)
+ */
+ public boolean containsKey( Object key )
+ {
+ return modifiableReference.containsKey( key );
+ }
+
+
+ /**
+ * @see java.util.Map#containsValue(java.lang.Object)
+ */
+ public boolean containsValue( Object value )
+ {
+ return modifiableReference.containsValue( value );
+ }
+
+
+ /**
+ * @see java.util.Map#get(java.lang.Object)
+ */
+ public V get( Object key )
+ {
+ return modifiableReference.get( key );
+ }
+
+
+ /**
+ * This method will return a read-only [EMAIL PROTECTED] Set}.
+ */
+ public Set<K> keySet()
+ {
+ return constantReference.keySet();
+ }
+
+
+ /**
+ * This method will return a read-only [EMAIL PROTECTED] Collection}.
+ */
+ public Collection<V> values()
+ {
+ return constantReference.values();
+ }
+
+
+ /**
+ * This method will return a read-only [EMAIL PROTECTED] Set}.
+ */
+ public Set<Entry<K, V>> entrySet()
+ {
+ return constantReference.entrySet();
+ }
+}
\ No newline at end of file