X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelCallback.java;h=27fc6bb37a60263b4a3216a9deb08877fd57ce11;hb=60144e6607f54352fdd28869e4af8b567c4e69da;hp=304d24e263105a375f0aa6ff96ac0d44134f0ec1;hpb=7020af8093e423c6a990a6db20da3514ac56236a;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java index 304d24e..27fc6bb 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java @@ -35,13 +35,26 @@ import java.util.List; public class HighLevelCallback { /** Object used for synchronization. */ - public final Object syncObject = new Object(); + private final Object syncObject = new Object(); /** The list of callback listeners. */ - public final List> highLevelCallbackListeners = Collections.synchronizedList(new ArrayList>()); + private final List> highLevelCallbackListeners = Collections.synchronizedList(new ArrayList>()); + + /** Whether the result is complete. */ + private boolean resultComplete = false; /** The result of the operation. */ - public R result = null; + 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 @@ -91,7 +104,7 @@ public class HighLevelCallback { */ public boolean isDone() { synchronized (syncObject) { - return result != null; + return resultComplete; } } @@ -120,7 +133,7 @@ public class HighLevelCallback { */ 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 { } /** - * 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(); }