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 {
36 /** Name of the “client token” property. */
37 public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
39 /** Name of the “total blocks” property. */
40 public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
42 /** Name of the “required blocks” property. */
43 public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
45 /** Name of the “successful blocks” property. */
46 public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
48 /** Name of the “failed blocks” property. */
49 public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
51 /** Name of the “fatally failed blocks” property. */
52 public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
54 /** Name of the “total finalized” property. */
55 public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
57 /** Property change listeners. */
58 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
60 /** The node the request belongs to. */
61 private final Node node;
63 /** The identifier of the request. */
64 private final String identifier;
66 /** The client token of the request. */
67 private String clientToken;
69 /** The total number of blocks. */
70 private int totalBlocks;
72 /** The required number of blocks. */
73 private int requiredBlocks;
75 /** The number of successful blocks. */
76 private int successfulBlocks;
78 /** The number of failedBlocks. */
79 private int failedBlocks;
81 /** The number of fatally failed blocks. */
82 private int fatallyFailedBlocks;
84 /** Whether the total number has been finalized. */
85 private boolean totalFinalized;
92 * Adds a property change listener.
94 * @param propertyChangeListener
95 * The property change listener to add
97 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
98 propertyChangeListeners.add(propertyChangeListener);
102 * Removes a property change listener.
104 * @param propertyChangeListener
105 * The property change listener to remove
107 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
108 propertyChangeListeners.remove(propertyChangeListener);
112 * Notifies all listeners that a property has changed.
115 * The name of the property
117 * The old value of the property
119 * The new value of the property
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);
130 * Creates a new request with the given identifier.
133 * The node the request belongs to
135 * The identifier of the request
137 Request(Node node, String identifier) {
139 this.identifier = identifier;
143 * Returns the node the request belongs to.
145 * @return The node the request belongs to
147 public Node getNode() {
152 * Returns the identifier of the request. It is unique per node.
154 * @return The identifier of the request
156 public String getIdentifier() {
161 * Returns the client token of the request.
163 * @return The client token of the request
165 public String getClientToken() {
170 * Sets the client token of the request.
173 * The client token of the request
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);
184 * Returns the total number of blocks of a request. Until
185 * {@link #isTotalFinalized()} returns <code>true</code> this value may
188 * @return The total number of blocks of a request
190 public int getTotalBlocks() {
195 * Sets the total number of blocks of a request.
198 * The total number of blocks
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);
209 * @return the requiredBlocks
211 public int getRequiredBlocks() {
212 return requiredBlocks;
216 * @param requiredBlocks
217 * the requiredBlocks to set
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);
228 * @return the successfulBlocks
230 public int getSuccessfulBlocks() {
231 return successfulBlocks;
235 * @param successfulBlocks
236 * the successfulBlocks to set
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);
247 * @return the failedBlocks
249 public int getFailedBlocks() {
254 * @param failedBlocks
255 * the failedBlocks to set
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);
266 * @return the fatallyFailedBlocks
268 public int getFatallyFailedBlocks() {
269 return fatallyFailedBlocks;
273 * @param fatallyFailedBlocks
274 * the fatallyFailedBlocks to set
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);
285 * @return the totalFinalized
287 public boolean isTotalFinalized() {
288 return totalFinalized;
292 * @param totalFinalized
293 * the totalFinalized to set
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);