directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r343990 - /directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java
Date Sun, 13 Nov 2005 22:29:49 GMT
Author: trustin
Date: Sun Nov 13 14:29:44 2005
New Revision: 343990

URL: http://svn.apache.org/viewcvs?rev=343990&view=rev
Log:
Added IoFuture.Callback

Modified:
    directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java

Modified: directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java?rev=343990&r1=343989&r2=343990&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/IoFuture.java Sun Nov 13 14:29:44
2005
@@ -28,15 +28,32 @@
 public class IoFuture
 {
     private Object result;
+    private Callback callback;
     private boolean ready;
 
     /**
+     * Something interested in being notified when the result
+     * of an {@link IoFuture} becomes available.
+     */
+    public interface Callback
+    {
+        /**
+         * Invoked when the operation associated with the {@link IoFuture}
+         * has been completed.
+         * 
+         * @param future  The source {@link IoFuture} which called this
+         *                callback.
+         */
+        public void operationComplete( IoFuture future );
+    }
+
+    /**
      * Creates a new instance.
      */
     protected IoFuture()
     {
     }
-
+    
     /**
      * Wait for the asynchronous operation to end.
      */
@@ -112,6 +129,11 @@
         result = newValue;
         ready = true;
         notifyAll();
+
+        if( callback != null )
+        {
+            invokeCallback();
+        }
     }
 
     /**
@@ -120,5 +142,37 @@
     protected synchronized Object getValue()
     {
         return result;
+    }
+    
+    /**
+     * Returns a {@link Callback} which is associated with this future.
+     */
+    public synchronized Callback getCallback()
+    {
+        return callback;
+    }
+    
+    /**
+     * Sets a {@link Callback} to be notified when a result
+     * becomes available.  If tth result has already become obtained,
+     * the specified callback is notified immediately
+     */
+    public synchronized void setCallback( Callback callback ) 
+    {
+        if( callback == null )
+        {
+            throw new NullPointerException( "callback" );
+        }
+
+        this.callback = callback;
+        if( ready )
+        {
+            invokeCallback();
+        }
+    }
+
+    private void invokeCallback()
+    {
+        callback.operationComplete( this );
     }
 }



Mime
View raw message