add identifier to all requests
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelProgressCallback.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
29  * <P>
30  * The type of the high-level progress
31  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
32  * @version $Id$
33  */
34 public class HighLevelProgressCallback<P extends HighLevelProgress> extends HighLevelCallback<P> {
35
36         /** Callback listeners. */
37         private final List<HighLevelProgressCallbackListener<P>> highLevelContinuousCallbackListeners = new ArrayList<HighLevelProgressCallbackListener<P>>();
38
39         /**
40          * Creates a new continuous callback with the given result.
41          * 
42          * @see HighLevelCallback#HighLevelCallback(HighLevelResult)
43          * @param progress
44          *            The result of the operation
45          */
46         HighLevelProgressCallback(P progress) {
47                 super(progress);
48         }
49
50         //
51         // EVENT MANAGEMENT
52         //
53
54         /**
55          * Adds a callback listener to this callback.
56          * 
57          * @param highLevelContinuousCallbackListener
58          *            The callback listener to add
59          */
60         public void addHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
61                 highLevelContinuousCallbackListeners.add(highLevelContinuousCallbackListener);
62                 fireGotProgress();
63         }
64
65         /**
66          * Removes a callback listener from this callback.
67          * 
68          * @param highLevelContinuousCallbackListener
69          *            The callback listener to remove
70          */
71         public void removeHighLevelContinuousCallbackListener(HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener) {
72                 highLevelContinuousCallbackListeners.remove(highLevelContinuousCallbackListener);
73         }
74
75         /**
76          * Notifies all listeners that progress results have been received.
77          */
78         private void fireGotProgress() {
79                 for (HighLevelProgressCallbackListener<P> highLevelContinuousCallbackListener: highLevelContinuousCallbackListeners) {
80                         highLevelContinuousCallbackListener.gotProgress(this);
81                 }
82         }
83
84         //
85         // ACCESSORS
86         //
87
88         /**
89          * Waits for the next progress on this callback. Completion of the result
90          * also counts as progress.
91          * 
92          * @throws InterruptedException
93          *             if {@link Object#wait()} is interrupted
94          */
95         public void waitForProgress() throws InterruptedException {
96                 synchronized (syncObject) {
97                         syncObject.wait();
98                 }
99         }
100
101         /**
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
104          * progress.
105          * 
106          * @param waitTime
107          *            The maximum time to wait for progress
108          * @throws InterruptedException
109          *             if {@link Object#wait()} is interrupted
110          */
111         public void waitForProgress(long waitTime) throws InterruptedException {
112                 synchronized (syncObject) {
113                         syncObject.wait(waitTime);
114                 }
115         }
116
117         /**
118          * Notifies all listeners that the progress was updated.
119          */
120         void progressUpdated() {
121                 synchronized (syncObject) {
122                         syncObject.notify();
123                 }
124                 fireGotProgress();
125         }
126
127 }