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