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/04/19 09:10:40 UTC

svn commit: r530294 - in /mina/trunk/core/src/main/java/org/apache/mina/common: IoSession.java support/BaseIoSession.java

Author: trustin
Date: Thu Apr 19 00:10:40 2007
New Revision: 530294

URL: http://svn.apache.org/viewvc?view=rev&rev=530294
Log:
Related issue: DIRMINA-370 (More atomic operations for user defined session attributes.)
* Added IoSession.getAttribute(String key, Object defaultValue) as Niklas suggested

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=530294&r1=530293&r2=530294
==============================================================================
--- 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 Thu Apr 19 00:10:40 2007
@@ -109,6 +109,23 @@
     Object getAttribute( String key );
     
     /**
+     * Returns the value of user defined attribute associated with the
+     * specified key.  If there's no such attribute, the specified defalut
+     * value is associated with the specified key, and the default value is
+     * returned.  This method is same with the following code except that the
+     * operation is performed atomically.
+     * <pre>
+     * if (containsAttribute(key)) {
+     *     return getAttribute(key);
+     * } else {
+     *     setAttribute(key, defaultValue);
+     *     return defaultValue;
+     * }
+     * </pre>
+     */
+    Object getAttribute(String key, Object defaultValue);
+
+    /**
      * Sets a user-defined attribute.
      * 
      * @param key the key of the attribute

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=530294&r1=530293&r2=530294
==============================================================================
--- 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 Thu Apr 19 00:10:40 2007
@@ -192,6 +192,22 @@
         
         return attributes.get( key );
     }
+    
+    public Object getAttribute(String key, Object defaultValue) {
+        if (key == null) {
+            throw new NullPointerException("key");
+        }
+        if (defaultValue == null) {
+            return attributes.get(key);
+        }
+        
+        Object answer = attributes.putIfAbsent(key, defaultValue);
+        if (answer == null) {
+            return defaultValue;
+        } else {
+            return answer;
+        }
+    }
 
     public Object setAttribute( String key, Object value )
     {