add some logging
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelContinuousCallback.java
1 /*
2  * jFCPlib-high-level-client - HighLevelContinousCallback.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.fcp.highlevel;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26  * Callback for an operation that sends progress messages before completion.
27  * 
28  * @param <R>
29  *            The type of the continuous result
30  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
31  * @version $Id$
32  */
33 public class HighLevelContinuousCallback<R extends HighLevelContinuousResult> extends HighLevelCallback<R> {
34
35         /** Callback listeners. */
36         private final List<HighLevelContinuousCallbackListener<R>> highLevelContinuousCallbackListeners = new ArrayList<HighLevelContinuousCallbackListener<R>>();
37
38         /** List of progress results. */
39         private final List<R> progressQueue = new ArrayList<R>();
40
41         /**
42          * Creates a new continuous callback with the given result.
43          * 
44          * @see HighLevelCallback#HighLevelCallback(HighLevelResult)
45          * @param progress
46          *            The result of the operation
47          */
48         HighLevelContinuousCallback(R progress) {
49                 super(progress);
50         }
51
52         //
53         // EVENT MANAGEMENT
54         //
55
56         /**
57          * Adds a callback listener to this callback. If this callback already has
58          * some progress results, the listener is notified immediately.
59          * 
60          * @param highLevelContinuousCallbackListener
61          *            The callback listener to add
62          */
63         public void addHighLevelContinuousCallbackListener(HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener) {
64                 highLevelContinuousCallbackListeners.add(highLevelContinuousCallbackListener);
65                 synchronized (progressQueue) {
66                         if (!progressQueue.isEmpty()) {
67                                 fireGotProgress();
68                         }
69                 }
70         }
71
72         /**
73          * Removes a callback listener from this callback.
74          * 
75          * @param highLevelContinuousCallbackListener
76          *            The callback listener to remove
77          */
78         public void removeHighLevelContinuousCallbackListener(HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener) {
79                 highLevelContinuousCallbackListeners.remove(highLevelContinuousCallbackListener);
80         }
81
82         /**
83          * Notifies all listeners that progress results have been received.
84          */
85         private void fireGotProgress() {
86                 for (HighLevelContinuousCallbackListener<R> highLevelContinuousCallbackListener: highLevelContinuousCallbackListeners) {
87                         highLevelContinuousCallbackListener.gotProgress(this);
88                 }
89         }
90
91         //
92         // ACCESSORS
93         //
94
95         /**
96          * Returns the next progress result and removes it from the queue. If no
97          * progress result is yet available, this method will block.
98          * 
99          * @return The next progress result
100          * @throws InterruptedException
101          *             if {@link Object#wait()} is interrupted
102          */
103         public R getProgress() throws InterruptedException {
104                 return getProgress(0);
105         }
106
107         /**
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
111          * passed.
112          * 
113          * @param waitTime
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
118          */
119         public R getProgress(long waitTime) throws InterruptedException {
120                 synchronized (progressQueue) {
121                         if (progressQueue.isEmpty()) {
122                                 progressQueue.wait(waitTime);
123                         }
124                         return progressQueue.remove(0);
125                 }
126         }
127
128         /**
129          * Returns the latest progress result and clears the queue.
130          * 
131          * @return The latest progress result, or <code>null</code> if the queue
132          *         is empty
133          */
134         public R getLatestProgress() {
135                 synchronized (progressQueue) {
136                         if (progressQueue.isEmpty()) {
137                                 return null;
138                         }
139                         R latestProgress = progressQueue.get(progressQueue.size() - 1);
140                         progressQueue.clear();
141                         return latestProgress;
142                 }
143         }
144
145         /**
146          * Adds a progress result and notifies all listeners.
147          * 
148          * @param progress
149          *            The progress result to add
150          */
151         void addProgress(R progress) {
152                 synchronized (progressQueue) {
153                         progressQueue.add(progress);
154                         progressQueue.notify();
155                 }
156                 fireGotProgress();
157         }
158
159 }