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