Author: trustin
Date: Wed Apr 18 00:30:08 2007
New Revision: 529898

URL: http://svn.apache.org/viewvc?view=rev&rev=529898
Log:
Resolved issue: DIRMINA-370 (More atomic operations for user defined session 
attributes.)
* Added IoSession.removeAttribute(String, Object)
* Added IoSession.setAttributeIfAbsent(String, Object)
* Added IoSession.replaceAttribute(String, Object, Object)

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/common/IoSession.java
    
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/IoSession.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/IoSession.java?view=diff&rev=529898&r1=529897&r2=529898
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/IoSession.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/IoSession.java Wed Apr 
18 00:30:08 2007
@@ -128,11 +128,57 @@
     Object setAttribute( String key );
     
     /**
+     * Sets a user defined attribute if the attribute with the specified key
+     * is not set yet.  This method is same with the following code except
+     * that the operation is performed atomically.
+     * <pre>
+     * if (containsAttribute(key)) {
+     *     return getAttribute(key);
+     * } else {
+     *     return setAttribute(key, value);
+     * }
+     * </pre>
+     */
+    Object setAttributeIfAbsent( String key, Object value );
+
+    /**
      * Removes a user-defined attribute with the specified key.
      * 
      * @return The old value of the attribute.  <tt>null</tt> if not found.
      */
     Object removeAttribute( String key );
+    
+    /**
+     * Removes a user defined attribute with the specified key if the current
+     * attribute value is equal to the specified value.  This method is same
+     * with the following code except that the operation is performed
+     * atomically.
+     * <pre>
+     * if (containsAttribute(key) && getAttribute(key).equals(value)) {
+     *     removeAttribute(key);
+     *     return true;
+     * } else {
+     *     return false;
+     * }
+     * </pre>
+     */
+    boolean removeAttribute(String key, Object value);
+    
+    /**
+     * Replaces a user defined attribute with the specified key if the
+     * value of the attribute is equals to the specified old value.
+     * This method is same with the following code except that the operation
+     * is performed atomically.
+     * <pre>
+     * if (containsAttribute(key) && getAttribute(key).equals(oldValue)) {
+     *     setAttribute(key, newValue);
+     *     return true;
+     * } else {
+     *     return false;
+     * }
+     * </pre>
+     */
+    boolean replaceAttribute(String key, Object oldValue, Object newValue);
     
     /**
      * Returns <tt>true</tt> if this session contains the attribute with

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java?view=diff&rev=529898&r1=529897&r2=529898
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java 
(original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseIoSession.java 
Wed Apr 18 00:30:08 2007
@@ -21,9 +21,9 @@
 
 import java.net.SocketAddress;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.apache.mina.common.CloseFuture;
 import org.apache.mina.common.DefaultWriteRequest;
@@ -44,7 +44,7 @@
 public abstract class BaseIoSession implements IoSession
 {
     private final Object lock = new Object();
-    private final Map<String,Object> attributes = new 
ConcurrentHashMap<String, Object>( );
+    private final ConcurrentMap<String,Object> attributes = new 
ConcurrentHashMap<String, Object>( );
     private final long creationTime;
 
     /** 
@@ -177,14 +177,22 @@
 
     public Object getAttribute( String key )
     {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        
         return attributes.get( key );
     }
 
     public Object setAttribute( String key, Object value )
     {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        
         if( value == null )
         {
-            return removeAttribute( key );
+            return attributes.remove( key );
         }
         else
         {
@@ -197,9 +205,42 @@
         return setAttribute( key, Boolean.TRUE );
     }
     
+    public Object setAttributeIfAbsent( String key, Object value )
+    {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        
+        if (value == null) {
+            return null;
+        }
+
+        return attributes.putIfAbsent( key, value );
+    }
+    
     public Object removeAttribute( String key )
     {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        
         return attributes.remove( key );
+    }
+    
+    public boolean removeAttribute(String key, Object value) {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        
+        if (value == null) {
+            return false;
+        }
+
+        return attributes.remove(key, value);
+    }
+    
+    public boolean replaceAttribute(String key, Object oldValue, Object 
newValue) {
+        return attributes.replace(key, oldValue, newValue);
     }
     
     public boolean containsAttribute( String key )


Reply via email to