1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 1 | package org.vostokframework.domain.loading.loaders |
30 | |
{ |
31 | |
import org.as3collections.IList; |
32 | |
import org.as3collections.lists.ArrayList; |
33 | |
import org.as3collections.lists.TypedList; |
34 | |
import org.as3coreaddendum.errors.ObjectDisposedError; |
35 | |
import org.as3coreaddendum.events.PriorityEvent; |
36 | |
import org.as3utils.ReflectionUtil; |
37 | |
import org.vostokframework.VostokIdentification; |
38 | |
import org.vostokframework.domain.loading.ILoader; |
39 | |
import org.vostokframework.domain.loading.ILoaderState; |
40 | |
import org.vostokframework.domain.loading.ILoaderStateTransition; |
41 | |
import org.vostokframework.domain.loading.LoadPriority; |
42 | |
|
43 | |
import flash.events.EventDispatcher; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class VostokLoader extends EventDispatcher implements ILoaderStateTransition |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
private var _disposed:Boolean; |
56 | |
private var _identification:VostokIdentification; |
57 | |
private var _index:int; |
58 | |
private var _priority:LoadPriority; |
59 | |
private var _state:ILoaderState; |
60 | |
private var _stateHistory:IList; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public function get identification(): VostokIdentification |
66 | |
{ |
67 | 1 | validateDisposal(); |
68 | 1 | return _identification; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public function get index(): int |
75 | |
{ |
76 | 1 | validateDisposal(); |
77 | 1 | return _index; |
78 | |
} |
79 | |
public function set index(value:int): void |
80 | |
{ |
81 | 1 | validateDisposal(); |
82 | 1 | _index = value; |
83 | 1 | } |
84 | |
|
85 | |
public function get isLoading():Boolean |
86 | |
{ |
87 | 1 | validateDisposal(); |
88 | 1 | return _state.isLoading; |
89 | |
} |
90 | |
|
91 | |
public function get isQueued():Boolean |
92 | |
{ |
93 | 1 | validateDisposal(); |
94 | 1 | return _state.isQueued; |
95 | |
} |
96 | |
|
97 | |
public function get isStopped():Boolean |
98 | |
{ |
99 | 1 | validateDisposal(); |
100 | 1 | return _state.isStopped; |
101 | |
} |
102 | |
|
103 | |
public function get maxConcurrentConnections():int |
104 | |
{ |
105 | 0 | validateDisposal(); |
106 | 0 | return _state.maxConcurrentConnections; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public function get priority(): int |
113 | |
{ |
114 | 1 | validateDisposal(); |
115 | 1 | return _priority.ordinal; |
116 | |
} |
117 | |
public function set priority(value:int): void |
118 | |
{ |
119 | 1 | validateDisposal(); |
120 | |
|
121 | |
try |
122 | |
{ |
123 | 1 | _priority = LoadPriority.getByOrdinal(value); |
124 | |
} |
125 | 1 | catch(error:Error) |
126 | |
{ |
127 | 1 | var errorMessage:String = "Value must be between 0 and 4. Received: <" + value + ">.\n"; |
128 | 1 | errorMessage += "For further information please consult the documentation section about:\n"; |
129 | 1 | errorMessage += ReflectionUtil.getClassPath(LoadPriority); |
130 | 1 | throw new ArgumentError(errorMessage); |
131 | |
} |
132 | |
|
133 | 1 | dispatchEvent(new PriorityEvent(PriorityEvent.CHANGED, _priority.ordinal)); |
134 | 1 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public function get openedConnections():int |
151 | |
{ |
152 | 1 | validateDisposal(); |
153 | 1 | return _state.openedConnections; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public function VostokLoader(identification:VostokIdentification, state:ILoaderState, priority:LoadPriority) |
161 | 1 | { |
162 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
163 | 1 | if (!state) throw new ArgumentError("Argument <state> must not be null."); |
164 | 1 | if (!priority) throw new ArgumentError("Argument <priority> must not be null."); |
165 | |
|
166 | 1 | _identification = identification; |
167 | 1 | _state = state; |
168 | 1 | _priority = priority; |
169 | 1 | _stateHistory = new TypedList(new ArrayList(), ILoaderState); |
170 | |
|
171 | 1 | _state.setLoader(this); |
172 | 1 | } |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public function addChild(child:ILoader): void |
180 | |
{ |
181 | 1 | validateDisposal(); |
182 | 1 | _state.addChild(child); |
183 | 1 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public function addChildren(children:IList): void |
191 | |
{ |
192 | 1 | validateDisposal(); |
193 | 1 | _state.addChildren(children); |
194 | 1 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public function cancel(): void |
201 | |
{ |
202 | 1 | validateDisposal(); |
203 | 1 | _state.cancel(); |
204 | 1 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public function cancelChild(identification:VostokIdentification): void |
212 | |
{ |
213 | 1 | validateDisposal(); |
214 | 1 | _state.cancelChild(identification); |
215 | 1 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public function containsChild(identification:VostokIdentification): Boolean |
223 | |
{ |
224 | 1 | validateDisposal(); |
225 | 1 | return _state.containsChild(identification); |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public function dispose():void |
233 | |
{ |
234 | 1 | if (_disposed) return; |
235 | |
|
236 | 1 | _state.dispose(); |
237 | 1 | _stateHistory.clear(); |
238 | |
|
239 | 1 | _stateHistory = null; |
240 | 1 | _state = null; |
241 | |
|
242 | 1 | _disposed = true; |
243 | 1 | } |
244 | |
|
245 | |
public function equals(other : *): Boolean |
246 | |
{ |
247 | 1 | validateDisposal(); |
248 | |
|
249 | 1 | if (this == other) return true; |
250 | 1 | if (!(other is ILoader)) return false; |
251 | |
|
252 | 1 | var otherLoader:ILoader = other as ILoader; |
253 | 1 | return _identification.equals(otherLoader.identification); |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public function getChild(identification:VostokIdentification): ILoader |
262 | |
{ |
263 | 1 | validateDisposal(); |
264 | 1 | return _state.getChild(identification); |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public function getParent(identification:VostokIdentification): ILoader |
284 | |
{ |
285 | 1 | validateDisposal(); |
286 | 1 | return _state.getParent(identification); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public function load(): void |
294 | |
{ |
295 | 1 | validateDisposal(); |
296 | 1 | _state.load(); |
297 | 1 | } |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
public function removeChild(identification:VostokIdentification): void |
305 | |
{ |
306 | 1 | validateDisposal(); |
307 | 1 | _state.removeChild(identification); |
308 | 1 | } |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public function resumeChild(identification:VostokIdentification): void |
316 | |
{ |
317 | 1 | validateDisposal(); |
318 | 1 | _state.resumeChild(identification); |
319 | 1 | } |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
public function setMaxConcurrentConnections(value:int):void |
325 | |
{ |
326 | 0 | validateDisposal(); |
327 | 0 | _state.setMaxConcurrentConnections(value); |
328 | 0 | } |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public function setState(state:ILoaderState):void |
334 | |
{ |
335 | 1 | validateDisposal(); |
336 | |
|
337 | 1 | _state = state; |
338 | 1 | _stateHistory.add(_state); |
339 | 1 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public function stop(): void |
345 | |
{ |
346 | 1 | validateDisposal(); |
347 | 1 | _state.stop(); |
348 | 1 | } |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public function stopChild(identification:VostokIdentification): void |
356 | |
{ |
357 | 1 | validateDisposal(); |
358 | 1 | _state.stopChild(identification); |
359 | 1 | } |
360 | |
|
361 | |
override public function toString():String |
362 | |
{ |
363 | 0 | return "[ILoader " + identification + "]"; |
364 | |
} |
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
private function validateDisposal():void |
370 | |
{ |
371 | 1 | if (_disposed) throw new ObjectDisposedError("This object was disposed, therefore no more operations can be performed."); |
372 | 1 | } |
373 | |
} |
374 | |
|
375 | |
} |