simplify abstract bean a bit
[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 net.pterodactylus.util.beans.AbstractBean;
23
24 /**
25  * A request is an ongoing download or upload reported by the freenet node.
26  * 
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  * @version $Id$
29  */
30 public class Request extends AbstractBean {
31
32         /**
33          * The type of a request.
34          * 
35          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36          * @version $Id$
37          */
38         public enum Type {
39
40                 /** Type of request is unknown. */
41                 unknown,
42
43                 /** The request is a Get request. */
44                 get,
45
46                 /** The request is a Put request. */
47                 put,
48
49                 /** The request is a PutDir request. */
50                 putDir
51
52         }
53
54         /** Name of the “type” property. */
55         public static final String PROPERTY_TYPE = "type";
56
57         /** Name of the “client token” property. */
58         public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
59
60         /** Name of the “total blocks” property. */
61         public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
62
63         /** Name of the “required blocks” property. */
64         public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
65
66         /** Name of the “successful blocks” property. */
67         public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
68
69         /** Name of the “failed blocks” property. */
70         public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
71
72         /** Name of the “fatally failed blocks” property. */
73         public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
74
75         /** Name of the “total finalized” property. */
76         public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
77
78         /** The node the request belongs to. */
79         private final Node node;
80
81         /** The identifier of the request. */
82         private final String identifier;
83
84         /** The type of the request. */
85         private Type type;
86
87         /** The client token of the request. */
88         private String clientToken;
89
90         /** The total number of blocks. */
91         private int totalBlocks;
92
93         /** The required number of blocks. */
94         private int requiredBlocks;
95
96         /** The number of successful blocks. */
97         private int successfulBlocks;
98
99         /** The number of failedBlocks. */
100         private int failedBlocks;
101
102         /** The number of fatally failed blocks. */
103         private int fatallyFailedBlocks;
104
105         /** Whether the total number has been finalized. */
106         private boolean totalFinalized;
107
108         /**
109          * Creates a new request with the given identifier.
110          * 
111          * @param node
112          *            The node the request belongs to
113          * @param identifier
114          *            The identifier of the request
115          */
116         Request(Node node, String identifier) {
117                 this.node = node;
118                 this.identifier = identifier;
119         }
120
121         //
122         // EVENT MANAGEMENT
123         //
124
125         /**
126          * Returns the node the request belongs to.
127          * 
128          * @return The node the request belongs to
129          */
130         public Node getNode() {
131                 return node;
132         }
133
134         /**
135          * Returns the identifier of the request. It is unique per node.
136          * 
137          * @return The identifier of the request
138          */
139         public String getIdentifier() {
140                 return identifier;
141         }
142
143         /**
144          * Returns the type of the request.
145          * 
146          * @return The type of the request
147          */
148
149         public Type getType() {
150                 return type;
151         }
152
153         /**
154          * Sets the type of the request.
155          * 
156          * @param type
157          *            The type of the request
158          */
159
160         public void setType(Type type) {
161                 Type oldType = this.type;
162                 this.type = type;
163                 fireIfPropertyChanged(PROPERTY_TYPE, oldType, type);
164         }
165
166         /**
167          * Returns the client token of the request.
168          * 
169          * @return The client token of the request
170          */
171         public String getClientToken() {
172                 return clientToken;
173         }
174
175         /**
176          * Sets the client token of the request.
177          * 
178          * @param clientToken
179          *            The client token of the request
180          */
181         public void setClientToken(String clientToken) {
182                 String oldClientToken = this.clientToken;
183                 this.clientToken = clientToken;
184                 fireIfPropertyChanged(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
185         }
186
187         /**
188          * Returns the total number of blocks of a request. Until
189          * {@link #isTotalFinalized()} returns <code>true</code> this value may
190          * change!
191          * 
192          * @return The total number of blocks of a request
193          */
194         public int getTotalBlocks() {
195                 return totalBlocks;
196         }
197
198         /**
199          * Sets the total number of blocks of a request.
200          * 
201          * @param totalBlocks
202          *            The total number of blocks
203          */
204         public void setTotalBlocks(int totalBlocks) {
205                 int oldTotalBlocks = this.totalBlocks;
206                 this.totalBlocks = totalBlocks;
207                 fireIfPropertyChanged(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
208         }
209
210         /**
211          * @return the requiredBlocks
212          */
213         public int getRequiredBlocks() {
214                 return requiredBlocks;
215         }
216
217         /**
218          * @param requiredBlocks
219          *            the requiredBlocks to set
220          */
221         public void setRequiredBlocks(int requiredBlocks) {
222                 int oldRequiredBlocks = this.requiredBlocks;
223                 this.requiredBlocks = requiredBlocks;
224                 fireIfPropertyChanged(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
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                 fireIfPropertyChanged(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
242         }
243
244         /**
245          * @return the failedBlocks
246          */
247         public int getFailedBlocks() {
248                 return failedBlocks;
249         }
250
251         /**
252          * @param failedBlocks
253          *            the failedBlocks to set
254          */
255         public void setFailedBlocks(int failedBlocks) {
256                 int oldFailedBlocks = this.failedBlocks;
257                 this.failedBlocks = failedBlocks;
258                 fireIfPropertyChanged(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
259         }
260
261         /**
262          * @return the fatallyFailedBlocks
263          */
264         public int getFatallyFailedBlocks() {
265                 return fatallyFailedBlocks;
266         }
267
268         /**
269          * @param fatallyFailedBlocks
270          *            the fatallyFailedBlocks to set
271          */
272         public void setFatallyFailedBlocks(int fatallyFailedBlocks) {
273                 int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
274                 this.fatallyFailedBlocks = fatallyFailedBlocks;
275                 fireIfPropertyChanged(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
276         }
277
278         /**
279          * @return the totalFinalized
280          */
281         public boolean isTotalFinalized() {
282                 return totalFinalized;
283         }
284
285         /**
286          * @param totalFinalized
287          *            the totalFinalized to set
288          */
289         public void setTotalFinalized(boolean totalFinalized) {
290                 boolean oldTotalFinalized = this.totalFinalized;
291                 this.totalFinalized = totalFinalized;
292                 fireIfPropertyChanged(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
293         }
294
295 }