X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelCallback.java;h=3e3e70f539118db01d5db7e59ea3981f899d9644;hb=4bdce9b8232e900d6c7fbfea7823505412cb7a32;hp=b43a8ccd9709cfb482b29a24cdadf59a4bf65517;hpb=460b437b16e31ccc70c8b97818dbbae0a94c8f76;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java index b43a8cc..3e3e70f 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,17 +137,66 @@ 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; + } + } + + /** + * Marks the result given in with + * {@link #setResult(HighLevelResult, boolean)} 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(); }