6a71de6fb970405b58d5a3bff5bfd45e98e622ee
[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  */
42 public class HighLevelProgress extends HighLevelResult {
43
44         /** Whether the request is finished. */
45         private boolean finished;
46
47         /** Whether a Put request should be fetchable now. */
48         private boolean fetchable;
49
50         /** The generated URI, in case of a Put request. */
51         private String uri;
52
53         /** The number of total blocks. */
54         private int totalBlocks;
55
56         /** The number of required blocks. */
57         private int requiredBlocks;
58
59         /** The number of successfully transferred blocks. */
60         private int successfulBlocks;
61
62         /** The number of failed blocks. */
63         private int failedBlocks;
64
65         /** The number of fatally failed blocks. */
66         private int fatallyFailedBlocks;
67
68         /** Whether the total number is finalized. */
69         private boolean totalFinalized;
70
71         /**
72          * Package-private constructor.
73          * 
74          * @param identifier
75          *            The identifier of the request
76          */
77         public HighLevelProgress(String identifier) {
78                 super(identifier);
79         }
80
81         /**
82          * Creates a new high-level progress for a request that is finished.
83          * 
84          * @param identifier
85          *            The identifier of the request
86          * @param successful
87          *            <code>true</code> if the request finished successfully,
88          *            <code>false</code> otherwise
89          */
90         public HighLevelProgress(String identifier, boolean successful) {
91                 this(identifier);
92                 finished = true;
93                 setFailed(!successful);
94         }
95
96         /**
97          * Creates a new high-level progress for a Put or PutDir request that
98          * generated a URI.
99          * 
100          * @param identifier
101          *            The identifier of the request
102          * @param uri
103          *            The URI of the request
104          */
105         public HighLevelProgress(String identifier, String uri) {
106                 this(identifier);
107                 this.uri = uri;
108         }
109
110         /**
111          * Creates a new high-level progress with the given values.
112          * 
113          * @param identifier
114          *            The identifier of the request
115          * @param totalBlocks
116          *            The total number of blocks
117          * @param requiredBlocks
118          *            The number of required blocks
119          * @param successfulBlocks
120          *            The number of successful blocks
121          * @param failedBlocks
122          *            The number of failed blocks
123          * @param fatallyFailedBlocks
124          *            The number of fatally failed blocks
125          * @param totalFinalized
126          *            <code>true</code> if the total number of blocks is
127          *            finalized, <code>false</code> otherwise
128          */
129         public HighLevelProgress(String identifier, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean totalFinalized) {
130                 this(identifier);
131                 this.totalBlocks = totalBlocks;
132                 this.requiredBlocks = requiredBlocks;
133                 this.successfulBlocks = successfulBlocks;
134                 this.failedBlocks = failedBlocks;
135                 this.fatallyFailedBlocks = fatallyFailedBlocks;
136                 this.totalFinalized = totalFinalized;
137         }
138
139         /**
140          * Returns whether this progress means that a request has finished. Use
141          * {@link #isFailed()} to check if the request failed.
142          * 
143          * @see #isFailed()
144          * @return <code>true</code> if the request has finished
145          */
146         public boolean isFinished() {
147                 return finished;
148         }
149
150         /**
151          * Sets whether the request described by this progress has finished.
152          * 
153          * @param finished
154          *            <code>true</code> if the request has finished,
155          *            <code>false</code> otherwise
156          */
157         void setFinished(boolean finished) {
158                 this.finished = finished;
159         }
160
161         /**
162          * Returns whether the request should be fetchable now, in case it was a Put
163          * request.
164          * 
165          * @return <code>true</code> if the request should be fetchable now,
166          *         <code>false</code> otherwise
167          */
168         public boolean isFetchable() {
169                 return fetchable;
170         }
171
172         /**
173          * Sets whether the request should be fetchable now, in case it was a Put
174          * request.
175          * 
176          * @param fetchable
177          *            <code>true</code> if the request should be fetchable now,
178          *            <code>false</code> otherwise
179          */
180         void setFetchable(boolean fetchable) {
181                 this.fetchable = fetchable;
182         }
183
184         /**
185          * Returns the URI that was generated by the request. Of course only Put and
186          * PutDir requests will generated URIs.
187          * 
188          * @return The generated URI
189          */
190         public String getURI() {
191                 return uri;
192         }
193
194         /**
195          * Sets the URI generated by the request.
196          * 
197          * @param uri
198          *            The generated URI
199          */
200         void setURI(String uri) {
201                 this.uri = uri;
202         }
203
204         /**
205          * Returns the number of total blocks.
206          * 
207          * @return The number of total blocks
208          */
209         public int getTotalBlocks() {
210                 return totalBlocks;
211         }
212
213         /**
214          * Sets the number of total blocks.
215          * 
216          * @param totalBlocks
217          *            The number of total blocks
218          */
219         void setTotalBlocks(int totalBlocks) {
220                 this.totalBlocks = totalBlocks;
221         }
222
223         /**
224          * Returns the number of required blocks. For downloads, this number is
225          * smaller than {@link #getTotalBlocks()}.
226          * 
227          * @return The number of required blocks
228          */
229         public int getRequiredBlocks() {
230                 return requiredBlocks;
231         }
232
233         /**
234          * Sets the number of required blocks.
235          * 
236          * @param requiredBlocks
237          *            The number of required blocks
238          */
239         void setRequiredBlocks(int requiredBlocks) {
240                 this.requiredBlocks = requiredBlocks;
241         }
242
243         /**
244          * Returns the number of successfully transferred blocks.
245          * 
246          * @return The number of successfully transferred blocks
247          */
248         public int getSuccessfulBlocks() {
249                 return successfulBlocks;
250         }
251
252         /**
253          * Sets the number of successfully transferred blocks.
254          * 
255          * @param successfulBlocks
256          *            The number of successfully transferred blocks
257          */
258         void setSuccessfulBlocks(int successfulBlocks) {
259                 this.successfulBlocks = successfulBlocks;
260         }
261
262         /**
263          * Returns the number of failed blocks. Blocks that have failed can be
264          * retried.
265          * 
266          * @return The number of failed blocks
267          */
268         public int getFailedBlocks() {
269                 return failedBlocks;
270         }
271
272         /**
273          * Sets the number of failed blocks.
274          * 
275          * @param failedBlocks
276          *            The number of failed blocks
277          */
278         void setFailedBlocks(int failedBlocks) {
279                 this.failedBlocks = failedBlocks;
280         }
281
282         /**
283          * Returns the number of fatally failed blocks. Fatally failed blocks will
284          * never complete, even with endless retries.
285          * 
286          * @return The number of fatally failed blocks
287          */
288         public int getFatallyFailedBlocks() {
289                 return fatallyFailedBlocks;
290         }
291
292         /**
293          * Sets the number of fatally failed blocks.
294          * 
295          * @param fatallyFailedBlocks
296          *            The number fatally failed blocks
297          */
298         void setFatallyFailedBlocks(int fatallyFailedBlocks) {
299                 this.fatallyFailedBlocks = fatallyFailedBlocks;
300         }
301
302         /**
303          * Returns whether the result of {@link #getTotalBlocks()} is final, i.e. it
304          * won’t change anymore.
305          * 
306          * @return <code>true</code> if the result of {@link #getTotalBlocks()} is
307          *         final, <code>false</code> otherwise
308          */
309         public boolean isTotalFinalized() {
310                 return totalFinalized;
311         }
312
313         /**
314          * Sets whether the result of {@link #getTotalBlocks()} is final, i.e. it
315          * won’t change anymore.
316          * 
317          * @param totalFinalized
318          *            <code>true</code> if the result of {@link #getTotalBlocks()}
319          *            is final, <code>false</code> otherwise
320          */
321         void setTotalFinalized(boolean totalFinalized) {
322                 this.totalFinalized = totalFinalized;
323         }
324
325 }