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 | |
|
30 | 1 | package org.vostokframework.domain.loading.states.queueloader.policies |
31 | |
{ |
32 | |
import org.as3collections.ICollection; |
33 | |
import org.as3collections.IIterator; |
34 | |
import org.vostokframework.domain.loading.GlobalLoadingSettings; |
35 | |
import org.vostokframework.domain.loading.ILoader; |
36 | |
import org.vostokframework.domain.loading.LoadPriority; |
37 | |
import org.vostokframework.domain.loading.LoaderRepository; |
38 | |
import org.vostokframework.domain.loading.states.queueloader.QueueLoadingStatus; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class SpecialHighestLowestQueueLoadingPolicy extends AbstractQueueLoadingPolicy |
46 | |
{ |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public function SpecialHighestLowestQueueLoadingPolicy(loaderRepository:LoaderRepository, globalLoadingSettings:GlobalLoadingSettings) |
54 | |
{ |
55 | 1 | super(loaderRepository, globalLoadingSettings); |
56 | 1 | } |
57 | |
|
58 | |
override protected function isNextLoaderEligible(loadingStatus:QueueLoadingStatus):Boolean |
59 | |
{ |
60 | 1 | var nextLoader:ILoader = loadingStatus.queuedLoaders.peek(); |
61 | 1 | var priorityNextLoader:LoadPriority = LoadPriority.getByOrdinal(nextLoader.priority); |
62 | |
|
63 | 1 | if (priorityNextLoader.equals(LoadPriority.HIGHEST)) |
64 | |
{ |
65 | 1 | stopAnyNotHighest(loadingStatus); |
66 | 1 | return true; |
67 | |
} |
68 | |
|
69 | 1 | if (priorityNextLoader.equals(LoadPriority.LOWEST)) |
70 | |
{ |
71 | 1 | if (containsOnlyLowest(loadingStatus.loadingLoaders)) return true; |
72 | 1 | return false; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | 1 | if (containsSomeHighest(loadingStatus.loadingLoaders)) return false; |
78 | |
|
79 | |
|
80 | |
|
81 | 1 | stopAnyLowest(loadingStatus); |
82 | 1 | return true; |
83 | |
} |
84 | |
|
85 | |
private function containsSomeHighest(loadingLoaders:ICollection):Boolean |
86 | |
{ |
87 | 1 | if (loadingLoaders.isEmpty()) return false; |
88 | |
|
89 | 1 | var it:IIterator = loadingLoaders.iterator(); |
90 | |
var loader:ILoader; |
91 | |
var loaderPriority:LoadPriority; |
92 | |
|
93 | 1 | while (it.hasNext()) |
94 | |
{ |
95 | 1 | loader = it.next(); |
96 | 1 | loaderPriority = LoadPriority.getByOrdinal(loader.priority); |
97 | 1 | if (loaderPriority.equals(LoadPriority.HIGHEST)) return true; |
98 | |
} |
99 | |
|
100 | 1 | return false; |
101 | |
} |
102 | |
|
103 | |
private function containsOnlyLowest(loadingLoaders:ICollection):Boolean |
104 | |
{ |
105 | 1 | if (loadingLoaders.isEmpty()) return true; |
106 | |
|
107 | 1 | var it:IIterator = loadingLoaders.iterator(); |
108 | |
var loader:ILoader; |
109 | |
var loaderPriority:LoadPriority; |
110 | |
|
111 | 1 | while (it.hasNext()) |
112 | |
{ |
113 | 1 | loader = it.next(); |
114 | 1 | loaderPriority = LoadPriority.getByOrdinal(loader.priority); |
115 | 1 | if (!loaderPriority.equals(LoadPriority.LOWEST)) return false; |
116 | |
} |
117 | |
|
118 | 1 | return true; |
119 | |
} |
120 | |
|
121 | |
private function stopAnyNotHighest(loadingStatus:QueueLoadingStatus):void |
122 | |
{ |
123 | 1 | if (loadingStatus.loadingLoaders.isEmpty()) return; |
124 | |
|
125 | 1 | var it:IIterator = loadingStatus.loadingLoaders.iterator(); |
126 | |
var loader:ILoader; |
127 | |
var loaderPriority:LoadPriority; |
128 | |
|
129 | 1 | while (it.hasNext()) |
130 | |
{ |
131 | 1 | loader = it.next(); |
132 | 1 | loaderPriority = LoadPriority.getByOrdinal(loader.priority); |
133 | |
|
134 | 1 | if (!loaderPriority.equals(LoadPriority.HIGHEST)) |
135 | |
{ |
136 | 1 | loadingStatus.queuedLoaders.add(loader); |
137 | 1 | it.remove(); |
138 | |
|
139 | 1 | loader.stop(); |
140 | |
} |
141 | |
} |
142 | 1 | } |
143 | |
|
144 | |
private function stopAnyLowest(loadingStatus:QueueLoadingStatus):void |
145 | |
{ |
146 | 1 | if (loadingStatus.loadingLoaders.isEmpty()) return; |
147 | |
|
148 | 1 | var it:IIterator = loadingStatus.loadingLoaders.iterator(); |
149 | |
var loader:ILoader; |
150 | |
var loaderPriority:LoadPriority; |
151 | |
|
152 | 1 | while (it.hasNext()) |
153 | |
{ |
154 | 1 | loader = it.next(); |
155 | 1 | loaderPriority = LoadPriority.getByOrdinal(loader.priority); |
156 | |
|
157 | 1 | if (loaderPriority.equals(LoadPriority.LOWEST)) |
158 | |
{ |
159 | 1 | loadingStatus.queuedLoaders.add(loader); |
160 | 1 | it.remove(); |
161 | |
|
162 | 1 | loader.stop(); |
163 | |
} |
164 | |
} |
165 | 1 | } |
166 | |
|
167 | |
} |
168 | |
|
169 | |
} |