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