add some properties
[jSite2.git] / src / net / pterodactylus / jsite / core / Request.java
1 /*
2  * jSite2 - Request.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.jsite.core;
21
22 import net.pterodactylus.util.beans.AbstractBean;
23
24 /**
25  * A request is an ongoing download or upload reported by the freenet node.
26  * 
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  * @version $Id$
29  */
30 public class Request extends AbstractBean {
31
32         /**
33          * The type of a request.
34          * 
35          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36          * @version $Id$
37          */
38         public enum Type {
39
40                 /** Type of request is unknown. */
41                 unknown,
42
43                 /** The request is a Get request. */
44                 get,
45
46                 /** The request is a Put request. */
47                 put,
48
49                 /** The request is a PutDir request. */
50                 putDir
51
52         }
53
54         /** Name of the “type” property. */
55         public static final String PROPERTY_TYPE = "type";
56
57         /** Name of the “client token” property. */
58         public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
59
60         /** Name of the “finished” property. */
61         public static final String PROPERTY_FINISHED = "finished";
62
63         /** Name of the “successful” property. */
64         public static final String PROPERTY_SUCCESSFUL = "successful";
65
66         /** Name of the “fetchable” property. */
67         public static final String PROPERTY_FETCHABLE = "fetchable";
68
69         /** Name of the “total blocks” property. */
70         public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
71
72         /** Name of the “required blocks” property. */
73         public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
74
75         /** Name of the “successful blocks” property. */
76         public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
77
78         /** Name of the “failed blocks” property. */
79         public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
80
81         /** Name of the “fatally failed blocks” property. */
82         public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
83
84         /** Name of the “total finalized” property. */
85         public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
86
87         /** The node the request belongs to. */
88         private final Node node;
89
90         /** The identifier of the request. */
91         private final String identifier;
92
93         /** The type of the request. */
94         private Type type;
95
96         /** The client token of the request. */
97         private String clientToken;
98
99         /** Whether the request is finished. */
100         private boolean finished;
101
102         /** Whether the request was finished successfully. */
103         private boolean successful;
104
105         /** Whether the data is already fetchable (in case of put requests). */
106         private boolean fetchable;
107
108         /** The total number of blocks. */
109         private int totalBlocks;
110
111         /** The required number of blocks. */
112         private int requiredBlocks;
113
114         /** The number of successful blocks. */
115         private int successfulBlocks;
116
117         /** The number of failedBlocks. */
118         private int failedBlocks;
119
120         /** The number of fatally failed blocks. */
121         private int fatallyFailedBlocks;
122
123         /** Whether the total number has been finalized. */
124         private boolean totalFinalized;
125
126         /**
127          * Creates a new request with the given identifier.
128          * 
129          * @param node
130          *            The node the request belongs to
131          * @param identifier
132          *            The identifier of the request
133          */
134         Request(Node node, String identifier) {
135                 this.node = node;
136                 this.identifier = identifier;
137         }
138
139         //
140         // EVENT MANAGEMENT
141         //
142
143         /**
144          * Returns the node the request belongs to.
145          * 
146          * @return The node the request belongs to
147          */
148         public Node getNode() {
149                 return node;
150         }
151
152         /**
153          * Returns the identifier of the request. It is unique per node.
154          * 
155          * @return The identifier of the request
156          */
157         public String getIdentifier() {
158                 return identifier;
159         }
160
161         /**
162          * Returns the type of the request.
163          * 
164          * @return The type of the request
165          */
166
167         public Type getType() {
168                 return type;
169         }
170
171         /**
172          * Sets the type of the request.
173          * 
174          * @param type
175          *            The type of the request
176          */
177         void setType(Type type) {
178                 Type oldType = this.type;
179                 this.type = type;
180                 fireIfPropertyChanged(PROPERTY_TYPE, oldType, type);
181         }
182
183         /**
184          * Returns the client token of the request.
185          * 
186          * @return The client token of the request
187          */
188         public String getClientToken() {
189                 return clientToken;
190         }
191
192         /**
193          * Sets the client token of the request.
194          * 
195          * @param clientToken
196          *            The client token of the request
197          */
198         void setClientToken(String clientToken) {
199                 String oldClientToken = this.clientToken;
200                 this.clientToken = clientToken;
201                 fireIfPropertyChanged(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
202         }
203
204         /**
205          * Returns whether the request has finished.
206          * 
207          * @see #isSuccessful()
208          * @return <code>true</code> if the request is finished,
209          *         <code>false</code> otherwise
210          */
211         public boolean isFinished() {
212                 return finished;
213         }
214
215         /**
216          * Sets whether the request has finished.
217          * 
218          * @param finished
219          *            <code>true</code> if the request has finished,
220          *            <code>false</code> otherwise
221          */
222         void setFinished(boolean finished) {
223                 boolean oldFinished = this.finished;
224                 this.finished = finished;
225                 firePropertyChange(PROPERTY_FINISHED, oldFinished, finished);
226         }
227
228         /**
229          * Returns whether the request finished successfully. This value will only
230          * have meaning if {@link #isFinished()} returns <code>true</code>.
231          * 
232          * @return <code>true</code> if the request finished successfully,
233          *         <code>false</code> otherwise
234          */
235         public boolean isSuccessful() {
236                 return successful;
237         }
238
239         /**
240          * Sets whether this request finished successfully.
241          * 
242          * @param successful
243          *            <code>true</code> if the request finished successfully,
244          *            <code>false</code> otherwise
245          */
246         void setSuccessful(boolean successful) {
247                 boolean oldSuccessful = this.successful;
248                 this.successful = successful;
249                 firePropertyChange(PROPERTY_SUCCESSFUL, oldSuccessful, successful);
250         }
251
252         /**
253          * Returns whether the data inserted by this {@link Type#put} or
254          * {@link Type#putDir} request is already fetchable by other clients.
255          * 
256          * @return <code>true</code> if the data is already fetchable,
257          *         <code>false</code> otherwise
258          */
259         public boolean isFetchable() {
260                 return fetchable;
261         }
262
263         /**
264          * Sets whether the data inserted by this {@link Type#put} or
265          * {@link Type#putDir} request is already fetchable by other clients.
266          * 
267          * @param fetchable
268          *            <code>true</code> if the data is already fetchable,
269          *            <code>false</code> otherwise
270          */
271         void setFetchable(boolean fetchable) {
272                 boolean oldFetchable = this.fetchable;
273                 this.fetchable = fetchable;
274                 firePropertyChange(PROPERTY_FETCHABLE, oldFetchable, fetchable);
275         }
276
277         /**
278          * Returns the total number of blocks of a request. Until
279          * {@link #isTotalFinalized()} returns <code>true</code> this value may
280          * change!
281          * 
282          * @return The total number of blocks of a request
283          */
284         public int getTotalBlocks() {
285                 return totalBlocks;
286         }
287
288         /**
289          * Sets the total number of blocks of a request.
290          * 
291          * @param totalBlocks
292          *            The total number of blocks
293          */
294         void setTotalBlocks(int totalBlocks) {
295                 int oldTotalBlocks = this.totalBlocks;
296                 this.totalBlocks = totalBlocks;
297                 fireIfPropertyChanged(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
298         }
299
300         /**
301          * @return the requiredBlocks
302          */
303         public int getRequiredBlocks() {
304                 return requiredBlocks;
305         }
306
307         /**
308          * @param requiredBlocks
309          *            the requiredBlocks to set
310          */
311         void setRequiredBlocks(int requiredBlocks) {
312                 int oldRequiredBlocks = this.requiredBlocks;
313                 this.requiredBlocks = requiredBlocks;
314                 fireIfPropertyChanged(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
315         }
316
317         /**
318          * @return the successfulBlocks
319          */
320         public int getSuccessfulBlocks() {
321                 return successfulBlocks;
322         }
323
324         /**
325          * @param successfulBlocks
326          *            the successfulBlocks to set
327          */
328         void setSuccessfulBlocks(int successfulBlocks) {
329                 int oldSuccessfulBlocks = this.successfulBlocks;
330                 this.successfulBlocks = successfulBlocks;
331                 fireIfPropertyChanged(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
332         }
333
334         /**
335          * @return the failedBlocks
336          */
337         public int getFailedBlocks() {
338                 return failedBlocks;
339         }
340
341         /**
342          * @param failedBlocks
343          *            the failedBlocks to set
344          */
345         void setFailedBlocks(int failedBlocks) {
346                 int oldFailedBlocks = this.failedBlocks;
347                 this.failedBlocks = failedBlocks;
348                 fireIfPropertyChanged(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
349         }
350
351         /**
352          * @return the fatallyFailedBlocks
353          */
354         public int getFatallyFailedBlocks() {
355                 return fatallyFailedBlocks;
356         }
357
358         /**
359          * @param fatallyFailedBlocks
360          *            the fatallyFailedBlocks to set
361          */
362         void setFatallyFailedBlocks(int fatallyFailedBlocks) {
363                 int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
364                 this.fatallyFailedBlocks = fatallyFailedBlocks;
365                 fireIfPropertyChanged(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
366         }
367
368         /**
369          * @return the totalFinalized
370          */
371         public boolean isTotalFinalized() {
372                 return totalFinalized;
373         }
374
375         /**
376          * @param totalFinalized
377          *            the totalFinalized to set
378          */
379         void setTotalFinalized(boolean totalFinalized) {
380                 boolean oldTotalFinalized = this.totalFinalized;
381                 this.totalFinalized = totalFinalized;
382                 fireIfPropertyChanged(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
383         }
384
385 }