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