forward generated URI to listeners
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelProgress.java
1 /*
2  * jFCPlib-high-level-client - HighLevelContinuosResult.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 /**
23  * Result for operations that send progress messages until they have completed.
24  * The fields of the progress message has to be checked in given order because
25  * if you receive this progress asynchronously via a
26  * {@link HighLevelProgressListener} the progress will not have any state, you
27  * will simply get the latest results, with other fields unset. First you should
28  * check whether {@link #isFinished()} returns <code>true</code>. If it does,
29  * the request is finished and {@link #isFailed()} will tell you whether the
30  * request has failed or succeeded. Other fields are not set. If the request is
31  * not yet finished, {@link #isFetchable()} will tell you whether the request
32  * has progressed to a state that allows other clients to fetch the inserted
33  * data. This is of course only valid for Put and PutDir requests. Alternatively
34  * {@link #getURI()} can return a non-<code>null</code> value which signals
35  * that the request generated a URI. If none of those methods return
36  * <code>true</code>, you can use the block count methods to get detailed
37  * progress statistics. When progress you received is a {@link DownloadResult}
38  * you do not need to check
39  * 
40  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
41  * @version $Id$
42  */
43 public class HighLevelProgress extends HighLevelResult {
44
45         /** Whether the request is finished. */
46         private boolean finished;
47
48         /** Whether a Put request should be fetchable now. */
49         private boolean fetchable;
50
51         /** The generated URI, in case of a Put request. */
52         private String uri;
53
54         /** The number of total blocks. */
55         private int totalBlocks;
56
57         /** The number of required blocks. */
58         private int requiredBlocks;
59
60         /** The number of successfully transferred blocks. */
61         private int successfulBlocks;
62
63         /** The number of failed blocks. */
64         private int failedBlocks;
65
66         /** The number of fatally failed blocks. */
67         private int fatallyFailedBlocks;
68
69         /** Whether the total number is finalized. */
70         private boolean totalFinalized;
71
72         /**
73          * Package-private constructor.
74          * 
75          * @param identifier
76          *            The identifier of the request
77          */
78         public HighLevelProgress(String identifier) {
79                 super(identifier);
80         }
81
82         /**
83          * Creates a new high-level progress for a request that is finished.
84          * 
85          * @param identifier
86          *            The identifier of the request
87          * @param successful
88          *            <code>true</code> if the request finished successfully,
89          *            <code>false</code> otherwise
90          */
91         public HighLevelProgress(String identifier, boolean successful) {
92                 this(identifier);
93                 finished = true;
94                 setFailed(!successful);
95         }
96
97         /**
98          * Creates a new high-level progress for a Put or PutDir request that
99          * generated a URI.
100          * 
101          * @param identifier
102          *            The identifier of the request
103          * @param uri
104          *            The URI of the request
105          */
106         public HighLevelProgress(String identifier, String uri) {
107                 this(identifier);
108                 this.uri = uri;
109         }
110
111         /**
112          * Creates a new high-level progress with the given values.
113          * 
114          * @param identifier
115          *            The identifier of the request
116          * @param totalBlocks
117          *            The total number of blocks
118          * @param requiredBlocks
119          *            The number of required blocks
120          * @param successfulBlocks
121          *            The number of successful blocks
122          * @param failedBlocks
123          *            The number of failed blocks
124          * @param fatallyFailedBlocks
125          *            The number of fatally failed blocks
126          * @param totalFinalized
127          *            <code>true</code> if the total number of blocks is
128          *            finalized, <code>false</code> otherwise
129          */
130         public HighLevelProgress(String identifier, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean totalFinalized) {
131                 this(identifier);
132                 this.totalBlocks = totalBlocks;
133                 this.requiredBlocks = requiredBlocks;
134                 this.successfulBlocks = successfulBlocks;
135                 this.failedBlocks = failedBlocks;
136                 this.fatallyFailedBlocks = fatallyFailedBlocks;
137                 this.totalFinalized = totalFinalized;
138         }
139
140         /**
141          * Returns whether this progress means that a request has finished. Use
142          * {@link #isFailed()} to check if the request failed.
143          * 
144          * @see #isFailed()
145          * @return <code>true</code> if the request has finished
146          */
147         public boolean isFinished() {
148                 return finished;
149         }
150
151         /**
152          * Sets whether the request described by this progress has finished.
153          * 
154          * @param finished
155          *            <code>true</code> if the request has finished,
156          *            <code>false</code> otherwise
157          */
158         void setFinished(boolean finished) {
159                 this.finished = finished;
160         }
161
162         /**
163          * Returns whether the request should be fetchable now, in case it was a Put
164          * request.
165          * 
166          * @return <code>true</code> if the request should be fetchable now,
167          *         <code>false</code> otherwise
168          */
169         public boolean isFetchable() {
170                 return fetchable;
171         }
172
173         /**
174          * Sets whether the request should be fetchable now, in case it was a Put
175          * request.
176          * 
177          * @param fetchable
178          *            <code>true</code> if the request should be fetchable now,
179          *            <code>false</code> otherwise
180          */
181         void setFetchable(boolean fetchable) {
182                 this.fetchable = fetchable;
183         }
184
185         /**
186          * Returns the URI that was generated by the request. Of course only Put and
187          * PutDir requests will generated URIs.
188          * 
189          * @return The generated URI
190          */
191         public String getURI() {
192                 return uri;
193         }
194
195         /**
196          * Sets the URI generated by the request.
197          * 
198          * @param uri
199          *            The generated URI
200          */
201         void setURI(String uri) {
202                 this.uri = uri;
203         }
204
205         /**
206          * Returns the number of total blocks.
207          * 
208          * @return The number of total blocks
209          */
210         public int getTotalBlocks() {
211                 return totalBlocks;
212         }
213
214         /**
215          * Sets the number of total blocks.
216          * 
217          * @param totalBlocks
218          *            The number of total blocks
219          */
220         void setTotalBlocks(int totalBlocks) {
221                 this.totalBlocks = totalBlocks;
222         }
223
224         /**
225          * Returns the number of required blocks. For downloads, this number is
226          * smaller than {@link #getTotalBlocks()}.
227          * 
228          * @return The number of required blocks
229          */
230         public int getRequiredBlocks() {
231                 return requiredBlocks;
232         }
233
234         /**
235          * Sets the number of required blocks.
236          * 
237          * @param requiredBlocks
238          *            The number of required blocks
239          */
240         void setRequiredBlocks(int requiredBlocks) {
241                 this.requiredBlocks = requiredBlocks;
242         }
243
244         /**
245          * Returns the number of successfully transferred blocks.
246          * 
247          * @return The number of successfully transferred blocks
248          */
249         public int getSuccessfulBlocks() {
250                 return successfulBlocks;
251         }
252
253         /**
254          * Sets the number of successfully transferred blocks.
255          * 
256          * @param successfulBlocks
257          *            The number of successfully transferred blocks
258          */
259         void setSuccessfulBlocks(int successfulBlocks) {
260                 this.successfulBlocks = successfulBlocks;
261         }
262
263         /**
264          * Returns the number of failed blocks. Blocks that have failed can be
265          * retried.
266          * 
267          * @return The number of failed blocks
268          */
269         public int getFailedBlocks() {
270                 return failedBlocks;
271         }
272
273         /**
274          * Sets the number of failed blocks.
275          * 
276          * @param failedBlocks
277          *            The number of failed blocks
278          */
279         void setFailedBlocks(int failedBlocks) {
280                 this.failedBlocks = failedBlocks;
281         }
282
283         /**
284          * Returns the number of fatally failed blocks. Fatally failed blocks will
285          * never complete, even with endless retries.
286          * 
287          * @return The number of fatally failed blocks
288          */
289         public int getFatallyFailedBlocks() {
290                 return fatallyFailedBlocks;
291         }
292
293         /**
294          * Sets the number of fatally failed blocks.
295          * 
296          * @param fatallyFailedBlocks
297          *            The number fatally failed blocks
298          */
299         void setFatallyFailedBlocks(int fatallyFailedBlocks) {
300                 this.fatallyFailedBlocks = fatallyFailedBlocks;
301         }
302
303         /**
304          * Returns whether the result of {@link #getTotalBlocks()} is final, i.e. it
305          * won’t change anymore.
306          * 
307          * @return <code>true</code> if the result of {@link #getTotalBlocks()} is
308          *         final, <code>false</code> otherwise
309          */
310         public boolean isTotalFinalized() {
311                 return totalFinalized;
312         }
313
314         /**
315          * Sets whether the result of {@link #getTotalBlocks()} is final, i.e. it
316          * won’t change anymore.
317          * 
318          * @param totalFinalized
319          *            <code>true</code> if the result of {@link #getTotalBlocks()}
320          *            is final, <code>false</code> otherwise
321          */
322         void setTotalFinalized(boolean totalFinalized) {
323                 this.totalFinalized = totalFinalized;
324         }
325
326 }