// $Id: Promise.java,v 1.2 2003/12/15 22:30:20 belaban Exp $

package org.jgroups.util;


/**
 Allows a thread to submit an asynchronous request and to wait for the result. The caller may choose to check
 for the result at a later time, or immediately and it may block or not. Both the caller and responder have to
 know the promise.
 */
public class Promise {
    Object result=null;


    /** Gets result. If result was already submitted, returns it immediately (resetting it), else blocks until
     results get available. <em>Note that the result has to be non-null</em>
     @param timeout Max time to wait for result. If it is 0, we wait indefinitely
     */
    public Object getResult(long timeout) {
        Object ret=null;

        synchronized(this) {
            if(result != null) {
                ret=result;
                result=null;
                return ret;
            }
            if(timeout <= 0) {
                try {
                    wait();
                }
                catch(Exception ex) {
                }
            }
            else {
                try {
                    wait(timeout);
                }
                catch(Exception ex) {
                }
            }
            if(result != null) {
                ret=result;
                result=null;
                return ret;
            }
            return null;
        }
    }


    /** Checks whether result is available. Does not block. */
    public Object checkForResult() {
        synchronized(this) {
            return result;
        }
    }

    /** Sets the result and notifies any threads waiting for it */
    public void setResult(Object obj) {
        synchronized(this) {
            result=obj;
            notifyAll();
        }
    }


    /** Causes all waiting threads to return */
    public void reset() {
        synchronized(this) {
            result=null;
            notifyAll();
        }
    }


    public String toString() {
        return "result=" + result;
    }


}

@@ -18,17 +18,17 @@
 ava,v 1.
-2
+3
  2003/12
@@ -32,19 +32,19 @@
 /12/
-15 22:30:20
+22 17:33:05
  bel
@@ -87,16 +87,18 @@
 ;%0A%0A%0A/**%0A
+ *
  Allows 
@@ -199,16 +199,18 @@
 o check%0A
+ *
  for the
@@ -312,16 +312,18 @@
 have to%0A
+ *
  know th
@@ -384,16 +384,45 @@
 t=null;%0A
+    boolean hasResult=false;%0A
 %0A%0A    /*
@@ -422,16 +422,23 @@
 %0A    /**
+%0A     *
  Gets re
@@ -475,16 +475,23 @@
 bmitted,
+%0A     *
  returns
@@ -533,16 +533,23 @@
 e blocks
+%0A     *
  until%0A 
@@ -547,24 +547,26 @@
 * until%0A    
+ *
  results get
@@ -580,62 +580,23 @@
 ble.
- %3Cem%3ENote that the result has to be non-null%3C/em%3E%0A
+%0A     * %0A 
     
+*
  @pa
@@ -641,16 +641,38 @@
 t. If it
+%0A     *               
  is 0, w
@@ -809,38 +809,33 @@
          if(
-r
+hasR
 esult
- != null
 ) %7B%0A        
@@ -875,32 +875,65 @@
    result=null;%0A
+                hasResult=false;%0A
                 
@@ -974,203 +974,115 @@
     
-if(timeout %3C= 0) %7B%0A                try %7B%0A                    wait();%0A                %7D%0A                catch(Exception ex) %7B%0A                %7D%0A            %7D%0A            else %7B%0A                try
+try %7B%0A                if(timeout %3C= 0) %7B%0A                    wait();%0A                %7D%0A                else
  %7B%0A 
@@ -1129,32 +1129,42 @@
           %7D%0A    
+        %7D%0A
             catc
@@ -1181,33 +1181,16 @@
 n ex) %7B%0A
-                %7D
 %0A       
@@ -1215,22 +1215,17 @@
  if(
-r
+hasR
 esult
- != null
 ) %7B%0A
@@ -1273,32 +1273,65 @@
    result=null;%0A
+                hasResult=false;%0A
                 
@@ -1406,16 +1406,23 @@
 %0A    /**
+%0A     *
  Checks 
@@ -1462,15 +1462,27 @@
  not
+%0A     *
  block.
+%0A    
  */%0A
@@ -1598,16 +1598,23 @@
 %0A    /**
+%0A     *
  Sets th
@@ -1646,16 +1646,23 @@
  threads
+%0A     *
  waiting
@@ -1668,16 +1668,21 @@
 g for it
+%0A    
  */%0A    
@@ -1770,16 +1770,44 @@
 lt=obj;%0A
+            hasResult=true;%0A
         
@@ -1848,16 +1848,23 @@
 %0A    /**
+%0A     *
  Causes 
@@ -1892,16 +1892,21 @@
 o return
+%0A    
  */%0A    
@@ -1973,32 +1973,61 @@
    result=null;%0A
+            hasResult=false;%0A
             noti
@@ -2128,9 +2128,8 @@
    %7D%0A%0A%0A%7D
-%0A

change as suggested by Thomas Sorgie