From: David ‘Bombe’ Roden Date: Tue, 15 Apr 2008 17:13:19 +0000 (+0000) Subject: allow intermediary results X-Git-Tag: v0.1.1~162 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=348c8edc79eb5e46c737d4881edffa3a720d2d33;p=jFCPlib.git allow intermediary results git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@822 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java index b43a8cc..aed413d 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java @@ -40,6 +40,9 @@ public class HighLevelCallback { /** The list of callback listeners. */ private final List> highLevelCallbackListeners = Collections.synchronizedList(new ArrayList>()); + /** Whether the result is complete. */ + private boolean resultComplete = false; + /** The result of the operation. */ private R result = null; @@ -97,7 +100,7 @@ public class HighLevelCallback { */ public boolean isDone() { synchronized (syncObject) { - return result != null; + return resultComplete; } } @@ -126,7 +129,7 @@ public class HighLevelCallback { */ public R getResult(long waitTime) throws InterruptedException { synchronized (syncObject) { - if (result == null) { + if (!resultComplete) { syncObject.wait(waitTime); } return result; @@ -134,19 +137,52 @@ public class HighLevelCallback { } /** - * Sets the result of the operation. Calling this method will result in all - * listeners being notified. + * Sets the complete result of the operation. Calling this method will + * result in all listeners being notified. * * @see #fireGotResult() * @param result * The result of the operation */ void setResult(R result) { + setResult(result, true); + } + + /** + * Sets the result of the operation. Depending on the notify + * parameter the listeners are notified. You have to call this method with + * notify = true after your result is completed, otherwise + * clients will block endlessly! + * + * @param result + * The result of the operation + * @param notify + * true to finalize the result and notify all + * listeners, false if something in the result + * might still change + */ + void setResult(R result, boolean notify) { synchronized (syncObject) { this.result = result; - syncObject.notifyAll(); + if (notify) { + resultComplete = true; + syncObject.notifyAll(); + } + } + if (notify) { + fireGotResult(); + } + } + + /** + * Returns the result even if it is not yet complete. + * + * @return The result of the operation + */ + R getIntermediaryResult() { + synchronized (syncObject) { + return result; } - fireGotResult(); } }