2 * jFCPlib-high-level-client - HighLevelContinousCallback.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.fcp.highlevel;
22 import java.util.ArrayList;
23 import java.util.List;
26 * Callback for an operation that sends progress messages before completion.
30 * The type of the high-level progress
31 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class HighLevelProgressCallback<P extends HighLevelProgress> extends HighLevelCallback<P> {
36 /** Callback listeners. */
37 private final List<HighLevelProgressCallbackListener<P>> highLevelContinuousCallbackListeners = new ArrayList<HighLevelProgressCallbackListener<P>>();
40 * Creates a new continuous callback with the given result.
42 * @see HighLevelCallback#HighLevelCallback(HighLevelResult)
44 * The result of the operation
46 HighLevelProgressCallback(P progress) {
55 * Adds a callback listener to this callback.
57 * @param highLevelContinuousCallbackListener
58 * The callback listener to add
60 public void addHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
61 highLevelContinuousCallbackListeners.add(highLevelContinuousCallbackListener);
66 * Removes a callback listener from this callback.
68 * @param highLevelContinuousCallbackListener
69 * The callback listener to remove
71 public void removeHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
72 highLevelContinuousCallbackListeners.remove(highLevelContinuousCallbackListener);
76 * Notifies all listeners that progress results have been received.
78 private void fireGotProgress() {
79 for (HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener: highLevelContinuousCallbackListeners) {
80 highLevelContinuousCallbackListener.gotProgress(this);
89 * Waits for the next progress on this callback. Completion of the result
90 * also counts as progress.
92 * @throws InterruptedException
93 * if {@link Object#wait()} is interrupted
95 public void waitForProgress() throws InterruptedException {
96 synchronized (syncObject) {
102 * Waits for the given amount of time (in milliseconds) for the next
103 * progress on this callback. Completion of the result also counts as
107 * The maximum time to wait for progress
108 * @throws InterruptedException
109 * if {@link Object#wait()} is interrupted
111 public void waitForProgress(long waitTime) throws InterruptedException {
112 synchronized (syncObject) {
113 syncObject.wait(waitTime);
118 * Notifies all listeners that the progress was updated.
120 void progressUpdated() {
121 synchronized (syncObject) {