extract abstract bean superclass
[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
23 import net.pterodactylus.util.beans.AbstractBean;
24 import net.pterodactylus.util.beans.Comparer;
25
26 /**
27  * A request is an ongoing download or upload reported by the freenet node.
28  * 
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  * @version $Id$
31  */
32 public class Request extends 
33 AbstractBean {
34
35         /**
36          * The type of a request.
37          * 
38          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39          * @version $Id$
40          */
41         public enum Type {
42
43                 /** Type of request is unknown. */
44                 unknown,
45
46                 /** The request is a Get request. */
47                 get,
48
49                 /** The request is a Put request. */
50                 put,
51
52                 /** The request is a PutDir request. */
53                 putDir
54
55         }
56
57         /** Name of the “type” property. */
58         public static final String PROPERTY_TYPE = "type";
59
60         /** Name of the “client token” property. */
61         public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
62
63         /** Name of the “total blocks” property. */
64         public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
65
66         /** Name of the “required blocks” property. */
67         public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
68
69         /** Name of the “successful blocks” property. */
70         public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
71
72         /** Name of the “failed blocks” property. */
73         public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
74
75         /** Name of the “fatally failed blocks” property. */
76         public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
77
78         /** Name of the “total finalized” property. */
79         public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
80
81         /** The node the request belongs to. */
82         private final Node node;
83
84         /** The identifier of the request. */
85         private final String identifier;
86
87         /** The type of the request. */
88         private Type type;
89
90         /** The client token of the request. */
91         private String clientToken;
92
93         /** The total number of blocks. */
94         private int totalBlocks;
95
96         /** The required number of blocks. */
97         private int requiredBlocks;
98
99         /** The number of successful blocks. */
100         private int successfulBlocks;
101
102         /** The number of failedBlocks. */
103         private int failedBlocks;
104
105         /** The number of fatally failed blocks. */
106         private int fatallyFailedBlocks;
107
108         /** Whether the total number has been finalized. */
109         private boolean totalFinalized;
110
111         /**
112          * Creates a new request with the given identifier.
113          * 
114          * @param node
115          *            The node the request belongs to
116          * @param identifier
117          *            The identifier of the request
118          */
119         Request(Node node, String identifier) {
120                 this.node = node;
121                 this.identifier = identifier;
122         }
123
124         //
125         // EVENT MANAGEMENT
126         //
127
128         /**
129          * Returns the node the request belongs to.
130          * 
131          * @return The node the request belongs to
132          */
133         public Node getNode() {
134                 return node;
135         }
136
137         /**
138          * Returns the identifier of the request. It is unique per node.
139          * 
140          * @return The identifier of the request
141          */
142         public String getIdentifier() {
143                 return identifier;
144         }
145
146         /**
147          * Returns the type of the request.
148          * 
149          * @return The type of the request
150          */
151
152         public Type getType() {
153                 return type;
154         }
155
156         /**
157          * Sets the type of the request.
158          * 
159          * @param type
160          *            The type of the request
161          */
162
163         public void setType(Type type) {
164                 Type oldType = this.type;
165                 this.type = type;
166                 if (!Comparer.equal(oldType, type)) {
167                         firePropertyChange(PROPERTY_TYPE, oldType, type);
168                 }
169         }
170
171         /**
172          * Returns the client token of the request.
173          * 
174          * @return The client token of the request
175          */
176         public String getClientToken() {
177                 return clientToken;
178         }
179
180         /**
181          * Sets the client token of the request.
182          * 
183          * @param clientToken
184          *            The client token of the request
185          */
186         public void setClientToken(String clientToken) {
187                 String oldClientToken = this.clientToken;
188                 this.clientToken = clientToken;
189                 if (!Comparer.equal(oldClientToken, clientToken)) {
190                         firePropertyChange(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
191                 }
192         }
193
194         /**
195          * Returns the total number of blocks of a request. Until
196          * {@link #isTotalFinalized()} returns <code>true</code> this value may
197          * change!
198          * 
199          * @return The total number of blocks of a request
200          */
201         public int getTotalBlocks() {
202                 return totalBlocks;
203         }
204
205         /**
206          * Sets the total number of blocks of a request.
207          * 
208          * @param totalBlocks
209          *            The total number of blocks
210          */
211         public void setTotalBlocks(int totalBlocks) {
212                 int oldTotalBlocks = this.totalBlocks;
213                 this.totalBlocks = totalBlocks;
214                 if (oldTotalBlocks != totalBlocks) {
215                         firePropertyChange(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
216                 }
217         }
218
219         /**
220          * @return the requiredBlocks
221          */
222         public int getRequiredBlocks() {
223                 return requiredBlocks;
224         }
225
226         /**
227          * @param requiredBlocks
228          *            the requiredBlocks to set
229          */
230         public void setRequiredBlocks(int requiredBlocks) {
231                 int oldRequiredBlocks = this.requiredBlocks;
232                 this.requiredBlocks = requiredBlocks;
233                 if (oldRequiredBlocks != requiredBlocks) {
234                         firePropertyChange(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
235                 }
236         }
237
238         /**
239          * @return the successfulBlocks
240          */
241         public int getSuccessfulBlocks() {
242                 return successfulBlocks;
243         }
244
245         /**
246          * @param successfulBlocks
247          *            the successfulBlocks to set
248          */
249         public void setSuccessfulBlocks(int successfulBlocks) {
250                 int oldSuccessfulBlocks = this.successfulBlocks;
251                 this.successfulBlocks = successfulBlocks;
252                 if (oldSuccessfulBlocks != successfulBlocks) {
253                         firePropertyChange(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
254                 }
255         }
256
257         /**
258          * @return the failedBlocks
259          */
260         public int getFailedBlocks() {
261                 return failedBlocks;
262         }
263
264         /**
265          * @param failedBlocks
266          *            the failedBlocks to set
267          */
268         public void setFailedBlocks(int failedBlocks) {
269                 int oldFailedBlocks = this.failedBlocks;
270                 this.failedBlocks = failedBlocks;
271                 if (oldFailedBlocks != failedBlocks) {
272                         firePropertyChange(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
273                 }
274         }
275
276         /**
277          * @return the fatallyFailedBlocks
278          */
279         public int getFatallyFailedBlocks() {
280                 return fatallyFailedBlocks;
281         }
282
283         /**
284          * @param fatallyFailedBlocks
285          *            the fatallyFailedBlocks to set
286          */
287         public void setFatallyFailedBlocks(int fatallyFailedBlocks) {
288                 int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
289                 this.fatallyFailedBlocks = fatallyFailedBlocks;
290                 if (oldFatallyFailedBlocks != fatallyFailedBlocks) {
291                         firePropertyChange(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
292                 }
293         }
294
295         /**
296          * @return the totalFinalized
297          */
298         public boolean isTotalFinalized() {
299                 return totalFinalized;
300         }
301
302         /**
303          * @param totalFinalized
304          *            the totalFinalized to set
305          */
306         public void setTotalFinalized(boolean totalFinalized) {
307                 boolean oldTotalFinalized = this.totalFinalized;
308                 this.totalFinalized = totalFinalized;
309                 if (oldTotalFinalized != totalFinalized) {
310                         firePropertyChange(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
311                 }
312         }
313
314 }