1 package nmp.util;
2
3 /***
4 * Used to store the result of an operation not yet known
5 */
6 public class DelayedPromise
7 implements Promise
8 {
9 private Object result;
10 private boolean completed;
11
12 public DelayedPromise() {
13 completed = false;
14 }
15
16 /***
17 * Returns true if the result of invocation is already known
18 */
19 public boolean isResultAvailable() {
20 return completed;
21 }
22
23 /***
24 * Gets the result of the preformed operation. This method blocks until the result
25 * is received.
26 */
27 public Object getResult() {
28 waitForResult( 0);
29 return result;
30 }
31
32 /***
33 * Gets the result of the preformed operation. This method blocks until the result
34 * is received.
35 */
36 public synchronized void setResult( Object result) {
37 this.result = result;
38 completed = true;
39 this.notifyAll();
40 }
41
42 /***
43 * Waits (a given time) until the result of the operation is available.
44 *
45 * @param mills The maximum time to wait in miliseconds. 0 - means waiting forever.
46 * @return Returns true if result is available
47 */
48 public synchronized boolean waitForResult( long mills) {
49 if( completed)
50 return true;
51 try {
52 this.wait( mills);
53 }catch( Exception ex) {
54 Log.errorDump( ex);
55 }
56 return completed;
57 }
58 }
This page was automatically generated by Maven