2 * jSite2 - Request.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.core;
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;
29 * A request is an ongoing download or upload reported by the freenet node.
31 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class Request {
37 * The type of a request.
39 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44 /** Type of request is unknown. */
47 /** The request is a Get request. */
50 /** The request is a Put request. */
53 /** The request is a PutDir request. */
58 /** Name of the “type” property. */
59 public static final String PROPERTY_TYPE = "type";
61 /** Name of the “client token” property. */
62 public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
64 /** Name of the “total blocks” property. */
65 public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
67 /** Name of the “required blocks” property. */
68 public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
70 /** Name of the “successful blocks” property. */
71 public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
73 /** Name of the “failed blocks” property. */
74 public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
76 /** Name of the “fatally failed blocks” property. */
77 public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
79 /** Name of the “total finalized” property. */
80 public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
82 /** Property change listeners. */
83 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
85 /** The node the request belongs to. */
86 private final Node node;
88 /** The identifier of the request. */
89 private final String identifier;
91 /** The type of the request. */
94 /** The client token of the request. */
95 private String clientToken;
97 /** The total number of blocks. */
98 private int totalBlocks;
100 /** The required number of blocks. */
101 private int requiredBlocks;
103 /** The number of successful blocks. */
104 private int successfulBlocks;
106 /** The number of failedBlocks. */
107 private int failedBlocks;
109 /** The number of fatally failed blocks. */
110 private int fatallyFailedBlocks;
112 /** Whether the total number has been finalized. */
113 private boolean totalFinalized;
116 * Creates a new request with the given identifier.
119 * The node the request belongs to
121 * The identifier of the request
123 Request(Node node, String identifier) {
125 this.identifier = identifier;
133 * Adds a property change listener.
135 * @param propertyChangeListener
136 * The property change listener to add
138 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
139 propertyChangeListeners.add(propertyChangeListener);
143 * Removes a property change listener.
145 * @param propertyChangeListener
146 * The property change listener to remove
148 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
149 propertyChangeListeners.remove(propertyChangeListener);
153 * Notifies all listeners that a property has changed.
156 * The name of the property
158 * The old value of the property
160 * The new value of the property
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);
174 * Returns the node the request belongs to.
176 * @return The node the request belongs to
178 public Node getNode() {
183 * Returns the identifier of the request. It is unique per node.
185 * @return The identifier of the request
187 public String getIdentifier() {
192 * Returns the type of the request.
194 * @return The type of the request
197 public Type getType() {
202 * Sets the type of the request.
205 * The type of the request
208 public void setType(Type type) {
209 Type oldType = this.type;
211 if (((oldType == null) && (type != null)) || ((oldType != null) && (type == null)) || ((type != null) && !type.equals(oldType))) {
212 firePropertyChange(PROPERTY_TYPE, oldType, type);
217 * Returns the client token of the request.
219 * @return The client token of the request
221 public String getClientToken() {
226 * Sets the client token of the request.
229 * The client token of the request
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);
240 * Returns the total number of blocks of a request. Until
241 * {@link #isTotalFinalized()} returns <code>true</code> this value may
244 * @return The total number of blocks of a request
246 public int getTotalBlocks() {
251 * Sets the total number of blocks of a request.
254 * The total number of blocks
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);
265 * @return the requiredBlocks
267 public int getRequiredBlocks() {
268 return requiredBlocks;
272 * @param requiredBlocks
273 * the requiredBlocks to set
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);
284 * @return the successfulBlocks
286 public int getSuccessfulBlocks() {
287 return successfulBlocks;
291 * @param successfulBlocks
292 * the successfulBlocks to set
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);
303 * @return the failedBlocks
305 public int getFailedBlocks() {
310 * @param failedBlocks
311 * the failedBlocks to set
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);
322 * @return the fatallyFailedBlocks
324 public int getFatallyFailedBlocks() {
325 return fatallyFailedBlocks;
329 * @param fatallyFailedBlocks
330 * the fatallyFailedBlocks to set
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);
341 * @return the totalFinalized
343 public boolean isTotalFinalized() {
344 return totalFinalized;
348 * @param totalFinalized
349 * the totalFinalized to set
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);