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