create callback with fixed result
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelCallback.java
index 3f0cd39..27fc6bb 100644 (file)
@@ -40,10 +40,23 @@ public class HighLevelCallback<R extends HighLevelResult> {
        /** The list of callback listeners. */
        private final List<HighLevelCallbackListener<R>> highLevelCallbackListeners = Collections.synchronizedList(new ArrayList<HighLevelCallbackListener<R>>());
 
+       /** Whether the result is complete. */
+       private boolean resultComplete = false;
+
        /** The result of the operation. */
        private R result = null;
 
        /**
+        * Package-private construtor.
+        * 
+        * @param result
+        *            The result of the operation
+        */
+       HighLevelCallback(R result) {
+               this.result = result;
+       }
+
+       /**
         * Adds a callback listener to this callback. The callback listener will be
         * called as soon as the result of the operation is known. If the result is
         * already known, the listener will be called immediately. Also, as soon as
@@ -91,7 +104,7 @@ public class HighLevelCallback<R extends HighLevelResult> {
         */
        public boolean isDone() {
                synchronized (syncObject) {
-                       return result != null;
+                       return resultComplete;
                }
        }
 
@@ -120,7 +133,7 @@ public class HighLevelCallback<R extends HighLevelResult> {
         */
        public R getResult(long waitTime) throws InterruptedException {
                synchronized (syncObject) {
-                       if (result == null) {
+                       if (!resultComplete) {
                                syncObject.wait(waitTime);
                        }
                        return result;
@@ -128,17 +141,27 @@ public class HighLevelCallback<R extends HighLevelResult> {
        }
 
        /**
-        * Sets the result of the operation. Calling this method will result in all
-        * listeners being notified.
+        * Returns the result even if it is not yet complete.
         * 
-        * @see #fireGotResult()
-        * @param result
-        *            The result of the operation
+        * @return The result of the operation
         */
-       void setResult(R result) {
+       R getIntermediaryResult() {
                synchronized (syncObject) {
-                       this.result = result;
-                       syncObject.notifyAll();
+                       return result;
+               }
+       }
+
+       /**
+        * Marks the result as complete and notify the listeners. If the result was
+        * already complete, nothing will be done.
+        */
+       void setDone() {
+               synchronized (syncObject) {
+                       if (resultComplete) {
+                               return;
+                       }
+                       resultComplete = true;
+                       syncObject.notify();
                }
                fireGotResult();
        }