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;
28 import net.pterodactylus.util.beans.Comparer;
31 * A request is an ongoing download or upload reported by the freenet node.
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class Request {
39 * The type of a request.
41 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46 /** Type of request is unknown. */
49 /** The request is a Get request. */
52 /** The request is a Put request. */
55 /** The request is a PutDir request. */
60 /** Name of the “type” property. */
61 public static final String PROPERTY_TYPE = "type";
63 /** Name of the “client token” property. */
64 public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
66 /** Name of the “total blocks” property. */
67 public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
69 /** Name of the “required blocks” property. */
70 public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
72 /** Name of the “successful blocks” property. */
73 public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
75 /** Name of the “failed blocks” property. */
76 public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
78 /** Name of the “fatally failed blocks” property. */
79 public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
81 /** Name of the “total finalized” property. */
82 public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
84 /** Property change listeners. */
85 private final List<PropertyChangeListener> propertyChangeListeners = Collections.synchronizedList(new ArrayList<PropertyChangeListener>());
87 /** The node the request belongs to. */
88 private final Node node;
90 /** The identifier of the request. */
91 private final String identifier;
93 /** The type of the request. */
96 /** The client token of the request. */
97 private String clientToken;
99 /** The total number of blocks. */
100 private int totalBlocks;
102 /** The required number of blocks. */
103 private int requiredBlocks;
105 /** The number of successful blocks. */
106 private int successfulBlocks;
108 /** The number of failedBlocks. */
109 private int failedBlocks;
111 /** The number of fatally failed blocks. */
112 private int fatallyFailedBlocks;
114 /** Whether the total number has been finalized. */
115 private boolean totalFinalized;
118 * Creates a new request with the given identifier.
121 * The node the request belongs to
123 * The identifier of the request
125 Request(Node node, String identifier) {
127 this.identifier = identifier;
135 * Adds a property change listener.
137 * @param propertyChangeListener
138 * The property change listener to add
140 public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
141 propertyChangeListeners.add(propertyChangeListener);
145 * Removes a property change listener.
147 * @param propertyChangeListener
148 * The property change listener to remove
150 public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
151 propertyChangeListeners.remove(propertyChangeListener);
155 * Notifies all listeners that a property has changed.
158 * The name of the property
160 * The old value of the property
162 * The new value of the property
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);
176 * Returns the node the request belongs to.
178 * @return The node the request belongs to
180 public Node getNode() {
185 * Returns the identifier of the request. It is unique per node.
187 * @return The identifier of the request
189 public String getIdentifier() {
194 * Returns the type of the request.
196 * @return The type of the request
199 public Type getType() {
204 * Sets the type of the request.
207 * The type of the request
210 public void setType(Type type) {
211 Type oldType = this.type;
213 if (!Comparer.equal(oldType, type)) {
214 firePropertyChange(PROPERTY_TYPE, oldType, type);
219 * Returns the client token of the request.
221 * @return The client token of the request
223 public String getClientToken() {
228 * Sets the client token of the request.
231 * The client token of the request
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);
242 * Returns the total number of blocks of a request. Until
243 * {@link #isTotalFinalized()} returns <code>true</code> this value may
246 * @return The total number of blocks of a request
248 public int getTotalBlocks() {
253 * Sets the total number of blocks of a request.
256 * The total number of blocks
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);
267 * @return the requiredBlocks
269 public int getRequiredBlocks() {
270 return requiredBlocks;
274 * @param requiredBlocks
275 * the requiredBlocks to set
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);
286 * @return the successfulBlocks
288 public int getSuccessfulBlocks() {
289 return successfulBlocks;
293 * @param successfulBlocks
294 * the successfulBlocks to set
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);
305 * @return the failedBlocks
307 public int getFailedBlocks() {
312 * @param failedBlocks
313 * the failedBlocks to set
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);
324 * @return the fatallyFailedBlocks
326 public int getFatallyFailedBlocks() {
327 return fatallyFailedBlocks;
331 * @param fatallyFailedBlocks
332 * the fatallyFailedBlocks to set
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);
343 * @return the totalFinalized
345 public boolean isTotalFinalized() {
346 return totalFinalized;
350 * @param totalFinalized
351 * the totalFinalized to set
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);