2 * jFCPlib - Request.java - Copyright © 2009–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.fcp.highlevel;
20 import net.pterodactylus.fcp.PersistentGet;
21 import net.pterodactylus.fcp.PersistentPut;
24 * Wrapper class around request responses from the node, such as
25 * {@link PersistentGet} or {@link PersistentPut}.
27 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29 public abstract class Request {
31 /** The identifier of the request. */
32 private final String identifier;
34 /** The client token of the request. */
35 private final String clientToken;
37 /** Whether the request is on the global queue. */
38 private final boolean global;
40 /** Whether the get request is complete. */
41 private boolean complete;
43 /** Whether the get request has failed. */
44 private boolean failed;
46 /** The data length. */
50 private String contentType;
52 /** The error code in case of failure. */
53 private int errorCode;
55 /** Whether the failure is fatal. */
56 private boolean fatal;
58 /** The total number of blocks. */
59 private int totalBlocks;
61 /** The required number of blocks. */
62 private int requiredBlocks;
64 /** The successfully processed number of blocks. */
65 private int succeededBlocks;
67 /** The number of failed blocks. */
68 private int failedBlocks;
70 /** The number of fatally failed blocks. */
71 private int fatallyFailedBlocks;
73 /** Whether the total number of blocks is finalized. */
74 private boolean finalizedTotal;
77 * Creates a new request with the given identifier and client token.
80 * The identifier of the request
82 * The client token of the request
84 * <code>true</code> if the request is on the global queue,
85 * <code>false</code> otherwise
87 protected Request(String identifier, String clientToken, boolean global) {
88 this.identifier = identifier;
89 this.clientToken = clientToken;
94 * Returns the identifier of the request.
96 * @return The request’s identifier
98 public String getIdentifier() {
103 * Returns the client token of the request.
105 * @return The request’s client token
107 public String getClientToken() {
112 * Returns whether this request is on the global queue.
114 * @return <code>true</code> if the request is on the global queue,
115 * <code>false</code> otherwise
117 public boolean isGlobal() {
122 * Returns whether this request is complete.
124 * @return <code>true</code> if this request is complete, false otherwise
126 public boolean isComplete() {
131 * Sets whether this request is complete.
134 * <code>true</code> if this request is complete, false
137 void setComplete(boolean complete) {
138 this.complete = complete;
142 * Returns whether this request has failed. This method should only be
143 * called if {@link #isComplete()} returns <code>true</code>.
145 * @return <code>true</code> if this request failed, <code>false</code>
148 public boolean hasFailed() {
153 * Sets whether this request has failed.
156 * <code>true</code> if this request failed, <code>false</code>
159 void setFailed(boolean failed) {
160 this.failed = failed;
164 * Returns the length of the data.
166 * @return The length of the data
168 public long getLength() {
173 * Sets the length of the data.
176 * The length of the data
178 void setLength(long length) {
179 this.length = length;
183 * Returns the content type of the data.
185 * @return The content type of the data
187 public String getContentType() {
192 * Sets the content type of the data.
195 * The content type of the data
197 void setContentType(String contentType) {
198 this.contentType = contentType;
202 * Returns the error code. This method should only be called if
203 * {@link #hasFailed()} returns <code>true</code>.
205 * @return The error code
207 public int getErrorCode() {
212 * Sets the error code.
217 void setErrorCode(int errorCode) {
218 this.errorCode = errorCode;
222 * Returns whether this request has fatally failed, i.e. repitition will
223 * not cause the request to succeed.
225 * @return <code>true</code> if this request can not be made succeed by
226 * repeating, <code>false</code> otherwise
228 public boolean isFatal() {
233 * Sets whether this request has fatally failed.
236 * <code>true</code> if this request failed fatally,
237 * <code>false</code> otherwise
239 void setFatal(boolean fatal) {
244 * Returns the total number of blocks of this request.
246 * @return This request’s total number of blocks
248 public int getTotalBlocks() {
253 * Sets the total number of blocks of this request.
256 * This request’s total number of blocks
258 void setTotalBlocks(int totalBlocks) {
259 this.totalBlocks = totalBlocks;
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
268 * @return The number of required blocks
270 public int getRequiredBlocks() {
271 return requiredBlocks;
275 * Sets the number of required blocks.
277 * @param requiredBlocks
278 * The number of required blocks
280 void setRequiredBlocks(int requiredBlocks) {
281 this.requiredBlocks = requiredBlocks;
285 * Returns the number of succeeded blocks.
287 * @return The number of succeeded blocks
289 public int getSucceededBlocks() {
290 return succeededBlocks;
294 * Sets the number of succeeded blocks.
296 * @param succeededBlocks
297 * The number of succeeded blocks
299 void setSucceededBlocks(int succeededBlocks) {
300 this.succeededBlocks = succeededBlocks;
304 * Returns the number of failed blocks. These blocks may be retried untill
305 * the maximum number of retries has been reached.
307 * @return The number of failed blocks
309 public int getFailedBlocks() {
314 * Sets the number of failed blocks.
316 * @param failedBlocks
317 * The number of failed blocks
319 void setFailedBlocks(int failedBlocks) {
320 this.failedBlocks = failedBlocks;
324 * Returns the number of fatally failed blocks.
326 * @return The number of fatally failed blocks
328 public int getFatallyFailedBlocks() {
329 return fatallyFailedBlocks;
333 * Sets the number of fatally failed blocks.
335 * @param fatallyFailedBlocks
336 * The number of fatally failed blocks
338 void setFatallyFailedBlocks(int fatallyFailedBlocks) {
339 this.fatallyFailedBlocks = fatallyFailedBlocks;
343 * Returns whether the number of blocks has been finalized.
345 * @return {@code true} if the number of blocks is finalized, {@code false}
348 public boolean isFinalizedTotal() {
349 return finalizedTotal;
353 * Sets whether the number of blocks has been finalized.
355 * @param finalizedTotal
356 * {@code true} if the number of blocks has been finalized,
357 * {@code false} otherwise
359 void setFinalizedTotal(boolean finalizedTotal) {
360 this.finalizedTotal = finalizedTotal;