Reformat source code, new line length for comments (79), some trailing whitespace...
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / highlevel / Request.java
1 /*
2  * jFCPlib - Request.java - Copyright © 2009 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.fcp.highlevel;
20
21 import net.pterodactylus.fcp.PersistentGet;
22 import net.pterodactylus.fcp.PersistentPut;
23
24 /**
25  * Wrapper class around request responses from the node, such as
26  * {@link PersistentGet} or {@link PersistentPut}.
27  *
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  */
30 public abstract class Request {
31
32         /** The identifier of the request. */
33         private final String identifier;
34
35         /** The client token of the request. */
36         private final String clientToken;
37
38         /** Whether the request is on the global queue. */
39         private final boolean global;
40
41         /** Whether the get request is complete. */
42         private boolean complete;
43
44         /** Whether the get request has failed. */
45         private boolean failed;
46
47         /** The data length. */
48         private long length;
49
50         /** The mime type. */
51         private String contentType;
52
53         /** The error code in case of failure. */
54         private int errorCode;
55
56         /** Whether the failure is fatal. */
57         private boolean fatal;
58
59         /** The total number of blocks. */
60         private int totalBlocks;
61
62         /** The required number of blocks. */
63         private int requiredBlocks;
64
65         /** The successfully processed number of blocks. */
66         private int succeededBlocks;
67
68         /** The number of failed blocks. */
69         private int failedBlocks;
70
71         /** The number of fatally failed blocks. */
72         private int fatallyFailedBlocks;
73
74         /** Whether the total number of blocks is finalized. */
75         private boolean finalizedTotal;
76
77         /**
78          * Creates a new request with the given identifier and client token.
79          *
80          * @param identifier
81          *            The identifier of the request
82          * @param clientToken
83          *            The client token of the request
84          * @param global
85          *            <code>true</code> if the request is on the global queue,
86          *            <code>false</code> otherwise
87          */
88         protected Request(String identifier, String clientToken, boolean global) {
89                 this.identifier = identifier;
90                 this.clientToken = clientToken;
91                 this.global = global;
92         }
93
94         /**
95          * Returns the identifier of the request.
96          *
97          * @return The request’s identifier
98          */
99         public String getIdentifier() {
100                 return identifier;
101         }
102
103         /**
104          * Returns the client token of the request.
105          *
106          * @return The request’s client token
107          */
108         public String getClientToken() {
109                 return clientToken;
110         }
111
112         /**
113          * Returns whether this request is on the global queue.
114          *
115          * @return <code>true</code> if the request is on the global queue,
116          *         <code>false</code> otherwise
117          */
118         public boolean isGlobal() {
119                 return global;
120         }
121
122         /**
123          * Returns whether this request is complete.
124          *
125          * @return <code>true</code> if this request is complete, false otherwise
126          */
127         public boolean isComplete() {
128                 return complete;
129         }
130
131         /**
132          * Sets whether this request is complete.
133          *
134          * @param complete
135          *            <code>true</code> if this request is complete, false
136          *            otherwise
137          */
138         void setComplete(boolean complete) {
139                 this.complete = complete;
140         }
141
142         /**
143          * Returns whether this request has failed. This method should only be
144          * called if {@link #isComplete()} returns <code>true</code>.
145          *
146          * @return <code>true</code> if this request failed, <code>false</code>
147          *         otherwise
148          */
149         public boolean hasFailed() {
150                 return failed;
151         }
152
153         /**
154          * Sets whether this request has failed.
155          *
156          * @param failed
157          *            <code>true</code> if this request failed, <code>false</code>
158          *            otherwise
159          */
160         void setFailed(boolean failed) {
161                 this.failed = failed;
162         }
163
164         /**
165          * Returns the length of the data.
166          *
167          * @return The length of the data
168          */
169         public long getLength() {
170                 return length;
171         }
172
173         /**
174          * Sets the length of the data.
175          *
176          * @param length
177          *            The length of the data
178          */
179         void setLength(long length) {
180                 this.length = length;
181         }
182
183         /**
184          * Returns the content type of the data.
185          *
186          * @return The content type of the data
187          */
188         public String getContentType() {
189                 return contentType;
190         }
191
192         /**
193          * Sets the content type of the data.
194          *
195          * @param contentType
196          *            The content type of the data
197          */
198         void setContentType(String contentType) {
199                 this.contentType = contentType;
200         }
201
202         /**
203          * Returns the error code. This method should only be called if
204          * {@link #hasFailed()} returns <code>true</code>.
205          *
206          * @return The error code
207          */
208         public int getErrorCode() {
209                 return errorCode;
210         }
211
212         /**
213          * Sets the error code.
214          *
215          * @param errorCode
216          *            The error code
217          */
218         void setErrorCode(int errorCode) {
219                 this.errorCode = errorCode;
220         }
221
222         /**
223          * Returns whether this request has fatally failed, i.e. repitition will
224          * not cause the request to succeed.
225          *
226          * @return <code>true</code> if this request can not be made succeed by
227          *         repeating, <code>false</code> otherwise
228          */
229         public boolean isFatal() {
230                 return fatal;
231         }
232
233         /**
234          * Sets whether this request has fatally failed.
235          *
236          * @param fatal
237          *            <code>true</code> if this request failed fatally,
238          *            <code>false</code> otherwise
239          */
240         void setFatal(boolean fatal) {
241                 this.fatal = fatal;
242         }
243
244         /**
245          * Returns the total number of blocks of this request.
246          *
247          * @return This request’s total number of blocks
248          */
249         public int getTotalBlocks() {
250                 return totalBlocks;
251         }
252
253         /**
254          * Sets the total number of blocks of this request.
255          *
256          * @param totalBlocks
257          *            This request’s total number of blocks
258          */
259         void setTotalBlocks(int totalBlocks) {
260                 this.totalBlocks = totalBlocks;
261         }
262
263         /**
264          * Returns the number of required blocks. Any progress percentages should
265          * be calculated against this value as 100%. Also, as long as
266          * {@link #isFinalizedTotal()} returns {@code false} this value might
267          * change.
268          *
269          * @return The number of required blocks
270          */
271         public int getRequiredBlocks() {
272                 return requiredBlocks;
273         }
274
275         /**
276          * Sets the number of required blocks.
277          *
278          * @param requiredBlocks
279          *            The number of required blocks
280          */
281         void setRequiredBlocks(int requiredBlocks) {
282                 this.requiredBlocks = requiredBlocks;
283         }
284
285         /**
286          * Returns the number of succeeded blocks.
287          *
288          * @return The number of succeeded blocks
289          */
290         public int getSucceededBlocks() {
291                 return succeededBlocks;
292         }
293
294         /**
295          * Sets the number of succeeded blocks.
296          *
297          * @param succeededBlocks
298          *            The number of succeeded blocks
299          */
300         void setSucceededBlocks(int succeededBlocks) {
301                 this.succeededBlocks = succeededBlocks;
302         }
303
304         /**
305          * Returns the number of failed blocks. These blocks may be retried untill
306          * the maximum number of retries has been reached.
307          *
308          * @return The number of failed blocks
309          */
310         public int getFailedBlocks() {
311                 return failedBlocks;
312         }
313
314         /**
315          * Sets the number of failed blocks.
316          *
317          * @param failedBlocks
318          *            The number of failed blocks
319          */
320         void setFailedBlocks(int failedBlocks) {
321                 this.failedBlocks = failedBlocks;
322         }
323
324         /**
325          * Returns the number of fatally failed blocks.
326          *
327          * @return The number of fatally failed blocks
328          */
329         public int getFatallyFailedBlocks() {
330                 return fatallyFailedBlocks;
331         }
332
333         /**
334          * Sets the number of fatally failed blocks.
335          *
336          * @param fatallyFailedBlocks
337          *            The number of fatally failed blocks
338          */
339         void setFatallyFailedBlocks(int fatallyFailedBlocks) {
340                 this.fatallyFailedBlocks = fatallyFailedBlocks;
341         }
342
343         /**
344          * Returns whether the number of blocks has been finalized.
345          *
346          * @return {@code true} if the number of blocks is finalized, {@code false}
347          *         otherwise
348          */
349         public boolean isFinalizedTotal() {
350                 return finalizedTotal;
351         }
352
353         /**
354          * Sets whether the number of blocks has been finalized.
355          *
356          * @param finalizedTotal
357          *            {@code true} if the number of blocks has been finalized,
358          *            {@code false} otherwise
359          */
360         void setFinalizedTotal(boolean finalizedTotal) {
361                 this.finalizedTotal = finalizedTotal;
362         }
363
364 }