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.
29 * The type of the continuous result
30 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33 public class HighLevelContinuousCallback<R extends HighLevelContinuousResult> extends HighLevelCallback<R> {
35 /** Callback listeners. */
36 private final List<HighLevelContinuousCallbackListener<R>> highLevelContinuousCallbackListeners = new ArrayList<HighLevelContinuousCallbackListener<R>>();
38 /** List of progress results. */
39 private final List<R> progressQueue = new ArrayList<R>();
42 * Creates a new continuous callback with the given result.
44 * @see HighLevelCallback#HighLevelCallback(HighLevelResult)
46 * The result of the operation
48 HighLevelContinuousCallback(R progress) {
57 * Adds a callback listener to this callback. If this callback already has
58 * some progress results, the listener is notified immediately.
60 * @param highLevelContinuousCallbackListener
61 * The callback listener to add
63 public void addHighLevelContinuousCallbackListener(HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener) {
64 highLevelContinuousCallbackListeners.add(highLevelContinuousCallbackListener);
65 synchronized (progressQueue) {
66 if (!progressQueue.isEmpty()) {
73 * Removes a callback listener from this callback.
75 * @param highLevelContinuousCallbackListener
76 * The callback listener to remove
78 public void removeHighLevelContinuousCallbackListener(HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener) {
79 highLevelContinuousCallbackListeners.remove(highLevelContinuousCallbackListener);
83 * Notifies all listeners that progress results have been received.
85 private void fireGotProgress() {
86 for (HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener: highLevelContinuousCallbackListeners) {
87 highLevelContinuousCallbackListener.gotProgress(this);
96 * Returns the next progress result and removes it from the queue. If no
97 * progress result is yet available, this method will block.
99 * @return The next progress result
100 * @throws InterruptedException
101 * if {@link Object#wait()} is interrupted
103 public R getProgress() throws InterruptedException {
104 return getProgress(0);
108 * Returns the next progress result and removes it from the queue. If no
109 * progress result is yet available, this method will block until either a
110 * progress result is available or the given time (in milliseconds) has
114 * The maximum time to wait for a progress result
115 * @return The next progress result
116 * @throws InterruptedException
117 * if {@link Object#wait()} is interrupted
119 public R getProgress(long waitTime) throws InterruptedException {
120 synchronized (progressQueue) {
121 if (progressQueue.isEmpty()) {
122 progressQueue.wait(waitTime);
124 return progressQueue.remove(0);
129 * Returns the latest progress result and clears the queue.
131 * @return The latest progress result, or <code>null</code> if the queue
134 public R getLatestProgress() {
135 synchronized (progressQueue) {
136 if (progressQueue.isEmpty()) {
139 R latestProgress = progressQueue.get(progressQueue.size() - 1);
140 progressQueue.clear();
141 return latestProgress;
146 * Adds a progress result and notifies all listeners.
149 * The progress result to add
151 void addProgress(R progress) {
152 synchronized (progressQueue) {
153 progressQueue.add(progress);
154 progressQueue.notify();