* @param <R>
* The type of the high-level operation result
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
*/
public class HighLevelCallback<R extends HighLevelResult> {
/** Object used for synchronization. */
- private final Object syncObject = new Object();
+ protected final Object syncObject = new Object();
/** 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;
+ protected boolean resultComplete = false;
/** The result of the operation. */
private R result = null;
}
/**
- * 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<R> highLevelCallbackListener: highLevelCallbackListeners) {
- highLevelCallbackListeners.remove(highLevelCallbackListener);
highLevelCallbackListener.gotResult(this);
}
+ highLevelCallbackListeners.clear();
}
/**
*/
public R getResult(long waitTime) throws InterruptedException {
synchronized (syncObject) {
- if (!resultComplete) {
+ while (!resultComplete) {
syncObject.wait(waitTime);
}
return result;
return;
}
resultComplete = true;
- syncObject.notify();
+ syncObject.notifyAll();
}
fireGotResult();
}