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