X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelCallback.java;h=7dc55dfc90aa753f80f5567d176fd3c82ac39227;hb=15f2240c29062e572f512e703a06e414bb3d5d37;hp=3e3e70f539118db01d5db7e59ea3981f899d9644;hpb=fa94fcde43d1c87745bcdbf25b4fa6242d5b1834;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java index 3e3e70f..7dc55df 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java @@ -30,26 +30,29 @@ import java.util.List; * @param * The type of the high-level operation result * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - * @version $Id$ */ public class HighLevelCallback { /** Object used for synchronization. */ - private final Object syncObject = new Object(); + protected final Object syncObject = new Object(); /** The list of callback listeners. */ private final List> highLevelCallbackListeners = Collections.synchronizedList(new ArrayList>()); /** Whether the result is complete. */ - private boolean resultComplete = false; + protected boolean resultComplete = false; /** The result of the operation. */ private R result = null; /** * Package-private construtor. + * + * @param result + * The result of the operation */ - HighLevelCallback() { + HighLevelCallback(R result) { + this.result = result; } /** @@ -80,15 +83,17 @@ public class HighLevelCallback { } /** - * Notifies all listeners that the result of the operation is now known. + * Notifies all listeners that the result of the operation is now known. As + * soon as a listener was notified it will be removed from the list of + * listeners! * * @see HighLevelCallbackListener#gotResult(HighLevelCallback) */ private synchronized void fireGotResult() { for (HighLevelCallbackListener highLevelCallbackListener: highLevelCallbackListeners) { - highLevelCallbackListeners.remove(highLevelCallbackListener); highLevelCallbackListener.gotResult(this); } + highLevelCallbackListeners.clear(); } /** @@ -129,7 +134,7 @@ public class HighLevelCallback { */ public R getResult(long waitTime) throws InterruptedException { synchronized (syncObject) { - if (!resultComplete) { + while (!resultComplete) { syncObject.wait(waitTime); } return result; @@ -137,44 +142,6 @@ public class HighLevelCallback { } /** - * 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; - 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 @@ -186,9 +153,8 @@ public class HighLevelCallback { } /** - * 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. + * Marks the result as complete and notify the listeners. If the result was + * already complete, nothing will be done. */ void setDone() { synchronized (syncObject) { @@ -196,7 +162,7 @@ public class HighLevelCallback { return; } resultComplete = true; - syncObject.notify(); + syncObject.notifyAll(); } fireGotResult(); }