first version of request table
[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         /** Name of the “client token” property. */
37         public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
38
39         /** Name of the “total blocks” property. */
40         public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
41
42         /** Name of the “required blocks” property. */
43         public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
44
45         /** Name of the “successful blocks” property. */
46         public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
47
48         /** Name of the “failed blocks” property. */
49         public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
50
51         /** Name of the “fatally failed blocks” property. */
52         public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
53
54         /** Name of the “total finalized” property. */
55         public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
56
57         /** Property change listeners. */
58         private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
59
60         /** The node the request belongs to. */
61         private final Node node;
62
63         /** The identifier of the request. */
64         private final String identifier;
65
66         /** The client token of the request. */
67         private String clientToken;
68
69         /** The total number of blocks. */
70         private int totalBlocks;
71
72         /** The required number of blocks. */
73         private int requiredBlocks;
74
75         /** The number of successful blocks. */
76         private int successfulBlocks;
77
78         /** The number of failedBlocks. */
79         private int failedBlocks;
80
81         /** The number of fatally failed blocks. */
82         private int fatallyFailedBlocks;
83
84         /** Whether the total number has been finalized. */
85         private boolean totalFinalized;
86
87         //
88         // EVENT MANAGEMENT
89         //
90
91         /**
92          * Adds a property change listener.
93          * 
94          * @param propertyChangeListener
95          *            The property change listener to add
96          */
97         public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
98                 propertyChangeListeners.add(propertyChangeListener);
99         }
100
101         /**
102          * Removes a property change listener.
103          * 
104          * @param propertyChangeListener
105          *            The property change listener to remove
106          */
107         public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
108                 propertyChangeListeners.remove(propertyChangeListener);
109         }
110
111         /**
112          * Notifies all listeners that a property has changed.
113          * 
114          * @param property
115          *            The name of the property
116          * @param oldValue
117          *            The old value of the property
118          * @param newValue
119          *            The new value of the property
120          */
121         private void firePropertyChange(String property, Object oldValue, Object newValue) {
122                 PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this, property, oldValue, newValue);
123                 for (PropertyChangeListener propertyChangeListener: propertyChangeListeners) {
124                         propertyChangeListener.propertyChange(propertyChangeEvent);
125                 }
126
127         }
128
129         /**
130          * Creates a new request with the given identifier.
131          * 
132          * @param node
133          *            The node the request belongs to
134          * @param identifier
135          *            The identifier of the request
136          */
137         Request(Node node, String identifier) {
138                 this.node = node;
139                 this.identifier = identifier;
140         }
141
142         /**
143          * Returns the node the request belongs to.
144          * 
145          * @return The node the request belongs to
146          */
147         public Node getNode() {
148                 return node;
149         }
150
151         /**
152          * Returns the identifier of the request. It is unique per node.
153          * 
154          * @return The identifier of the request
155          */
156         public String getIdentifier() {
157                 return identifier;
158         }
159
160         /**
161          * Returns the client token of the request.
162          * 
163          * @return The client token of the request
164          */
165         public String getClientToken() {
166                 return clientToken;
167         }
168
169         /**
170          * Sets the client token of the request.
171          * 
172          * @param clientToken
173          *            The client token of the request
174          */
175         public void setClientToken(String clientToken) {
176                 String oldClientToken = this.clientToken;
177                 this.clientToken = clientToken;
178                 if (((oldClientToken == null) && (clientToken != null)) || ((oldClientToken != null) && (clientToken == null)) || ((clientToken != null) && !clientToken.equals(oldClientToken))) {
179                         firePropertyChange(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
180                 }
181         }
182
183         /**
184          * Returns the total number of blocks of a request. Until
185          * {@link #isTotalFinalized()} returns <code>true</code> this value may
186          * change!
187          * 
188          * @return The total number of blocks of a request
189          */
190         public int getTotalBlocks() {
191                 return totalBlocks;
192         }
193
194         /**
195          * Sets the total number of blocks of a request.
196          * 
197          * @param totalBlocks
198          *            The total number of blocks
199          */
200         public void setTotalBlocks(int totalBlocks) {
201                 int oldTotalBlocks = this.totalBlocks;
202                 this.totalBlocks = totalBlocks;
203                 if (oldTotalBlocks != totalBlocks) {
204                         firePropertyChange(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
205                 }
206         }
207
208         /**
209          * @return the requiredBlocks
210          */
211         public int getRequiredBlocks() {
212                 return requiredBlocks;
213         }
214
215         /**
216          * @param requiredBlocks
217          *            the requiredBlocks to set
218          */
219         public void setRequiredBlocks(int requiredBlocks) {
220                 int oldRequiredBlocks = this.requiredBlocks;
221                 this.requiredBlocks = requiredBlocks;
222                 if (oldRequiredBlocks != requiredBlocks) {
223                         firePropertyChange(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
224                 }
225         }
226
227         /**
228          * @return the successfulBlocks
229          */
230         public int getSuccessfulBlocks() {
231                 return successfulBlocks;
232         }
233
234         /**
235          * @param successfulBlocks
236          *            the successfulBlocks to set
237          */
238         public void setSuccessfulBlocks(int successfulBlocks) {
239                 int oldSuccessfulBlocks = this.successfulBlocks;
240                 this.successfulBlocks = successfulBlocks;
241                 if (oldSuccessfulBlocks != successfulBlocks) {
242                         firePropertyChange(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
243                 }
244         }
245
246         /**
247          * @return the failedBlocks
248          */
249         public int getFailedBlocks() {
250                 return failedBlocks;
251         }
252
253         /**
254          * @param failedBlocks
255          *            the failedBlocks to set
256          */
257         public void setFailedBlocks(int failedBlocks) {
258                 int oldFailedBlocks = this.failedBlocks;
259                 this.failedBlocks = failedBlocks;
260                 if (oldFailedBlocks != failedBlocks) {
261                         firePropertyChange(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
262                 }
263         }
264
265         /**
266          * @return the fatallyFailedBlocks
267          */
268         public int getFatallyFailedBlocks() {
269                 return fatallyFailedBlocks;
270         }
271
272         /**
273          * @param fatallyFailedBlocks
274          *            the fatallyFailedBlocks to set
275          */
276         public void setFatallyFailedBlocks(int fatallyFailedBlocks) {
277                 int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
278                 this.fatallyFailedBlocks = fatallyFailedBlocks;
279                 if (oldFatallyFailedBlocks != fatallyFailedBlocks) {
280                         firePropertyChange(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
281                 }
282         }
283
284         /**
285          * @return the totalFinalized
286          */
287         public boolean isTotalFinalized() {
288                 return totalFinalized;
289         }
290
291         /**
292          * @param totalFinalized
293          *            the totalFinalized to set
294          */
295         public void setTotalFinalized(boolean totalFinalized) {
296                 boolean oldTotalFinalized = this.totalFinalized;
297                 this.totalFinalized = totalFinalized;
298                 if (oldTotalFinalized != totalFinalized) {
299                         firePropertyChange(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
300                 }
301         }
302
303 }