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>
33 public class HighLevelProgressCallback<P extends HighLevelProgress> extends HighLevelCallback<P> {
35 /** Callback listeners. */
36 private final List<HighLevelProgressCallbackListener<P>> highLevelContinuousCallbackListeners = new ArrayList<HighLevelProgressCallbackListener<P>>();
39 * Creates a new continuous callback with the given result.
41 * @see HighLevelCallback#HighLevelCallback(HighLevelResult)
43 * The result of the operation
45 HighLevelProgressCallback(P progress) {
54 * Adds a callback listener to this callback.
56 * @param highLevelContinuousCallbackListener
57 * The callback listener to add
59 public void addHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
60 highLevelContinuousCallbackListeners.add(highLevelContinuousCallbackListener);
65 * Removes a callback listener from this callback.
67 * @param highLevelContinuousCallbackListener
68 * The callback listener to remove
70 public void removeHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
71 highLevelContinuousCallbackListeners.remove(highLevelContinuousCallbackListener);
75 * Notifies all listeners that progress results have been received.
77 private void fireGotProgress() {
78 for (HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener: highLevelContinuousCallbackListeners) {
79 highLevelContinuousCallbackListener.gotProgress(this);
88 * Waits for the next progress on this callback. Completion of the result
89 * also counts as progress.
91 * @throws InterruptedException
92 * if {@link Object#wait()} is interrupted
94 public void waitForProgress() throws InterruptedException {
95 synchronized (syncObject) {
101 * Waits for the given amount of time (in milliseconds) for the next
102 * progress on this callback. Completion of the result also counts as
106 * The maximum time to wait for progress
107 * @throws InterruptedException
108 * if {@link Object#wait()} is interrupted
110 public void waitForProgress(long waitTime) throws InterruptedException {
111 synchronized (syncObject) {
112 syncObject.wait(waitTime);
117 * Notifies all listeners that the progress was updated.
119 void progressUpdated() {
120 synchronized (syncObject) {