Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TypedListMap |
|
| 0.0;0 |
1 | /* | |
2 | * Licensed under the MIT License | |
3 | * | |
4 | * Copyright 2010 (c) Flávio Silva, http://flsilva.com | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person | |
7 | * obtaining a copy of this software and associated documentation | |
8 | * files (the "Software"), to deal in the Software without | |
9 | * restriction, including without limitation the rights to use, | |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the | |
12 | * Software is furnished to do so, subject to the following | |
13 | * conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be | |
16 | * included in all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
25 | * OTHER DEALINGS IN THE SOFTWARE. | |
26 | * | |
27 | * http://www.opensource.org/licenses/mit-license.php | |
28 | */ | |
29 | ||
30 | 1 | package org.as3collections.maps |
31 | { | |
32 | import org.as3collections.IListMap; | |
33 | import org.as3collections.IListMapIterator; | |
34 | import org.as3collections.IMap; | |
35 | import org.as3collections.IMapEntry; | |
36 | import org.as3collections.iterators.ListMapIterator; | |
37 | ||
38 | /** | |
39 | * <code>TypedSortedMap</code> works as a wrapper for a map. | |
40 | * It stores the <code>wrapMap</code> constructor's argument in the <code>wrappedMap</code> variable. | |
41 | * So every method call to this class is forwarded to the <code>wrappedMap</code> object. | |
42 | * The methods that need to be checked for the type of the keys and values are previously validated with the <code>validateKeyType</code>, <code>validateValueType</code> or <code>validateMap</code> method before forward the call. | |
43 | * If the type of a key or value requested to be inserted to this map is incompatible with the type of the map a <code>org.as3coreaddendum.errors.ClassCastError</code> is thrown. | |
44 | * The calls that are forwarded to the <code>wrappedMap</code> returns the return of the <code>wrappedMap</code> call. | |
45 | * <p><code>TypedSortedMap</code> does not allow <code>null</code> keys or values.</p> | |
46 | * | |
47 | * @example | |
48 | * | |
49 | * <listing version="3.0"> | |
50 | * import org.as3collections.ISortedMap; | |
51 | * import org.as3collections.maps.SortedArrayListMap; | |
52 | * import org.as3collections.maps.TypedSortedMap; | |
53 | * | |
54 | * var map1:ISortedMap = new SortedArrayListMap(); | |
55 | * | |
56 | * map1.put("e", 1) // null | |
57 | * map1.put("d", 2) // null | |
58 | * map1.put("c", 3) // null | |
59 | * map1.put("b", 4) // null | |
60 | * map1.put("a", 5) // null | |
61 | * | |
62 | * map1 // {a=5,b=4,c=3,d=2,e=1} | |
63 | * map1.size() // 5 | |
64 | * | |
65 | * var map2:ISortedMap = new TypedSortedMap(map1, String, Number); // you can use this way | |
66 | * | |
67 | * //var map2:ISortedMap = MapUtil.getTypedSortedMap(map1, String, Number); // or you can use this way | |
68 | * | |
69 | * map2 // {a=5,b=4,c=3,d=2,e=1} | |
70 | * map2.size() // 5 | |
71 | * | |
72 | * map2.equals(map1) // false | |
73 | * | |
74 | * map2.put("f", 6) // null | |
75 | * map2 // {a=5,b=4,c=3,d=2,e=1,f=6} | |
76 | * map2.size() // 6 | |
77 | * | |
78 | * map2.put("g", "h") // ClassCastError: Invalid value type. value: h | type: String | expected value type: Number | |
79 | * map2.put(7, 8) // ClassCastError: Invalid key type. key: 7 | type: int | expected key type: String | |
80 | * </listing> | |
81 | * | |
82 | * @see org.as3collections.utils.MapUtil#getTypedSortedMap() MapUtil.getTypedSortedMap() | |
83 | * @author Flávio Silva | |
84 | */ | |
85 | public class TypedListMap extends TypedMap implements IListMap | |
86 | { | |
87 | /** | |
88 | * Returns the return of the call <code>wrapMap.modCount</code>. | |
89 | */ | |
90 | public function get modCount(): int { return wrappedListMap.modCount; } | |
91 | ||
92 | /** | |
93 | * @private | |
94 | */ | |
95 | protected function get wrappedListMap(): IListMap { return wrappedMap as IListMap; } | |
96 | ||
97 | /** | |
98 | * Constructor, creates a new <code>TypedSortedMap</code> object. | |
99 | * | |
100 | * @param wrapMap the target map to wrap. | |
101 | * @param typeKeys the type of the keys allowed by this map. | |
102 | * @param typeValues the type of the values allowed by this map. | |
103 | * @throws ArgumentError if the <code>wrapMap</code> argument is <code>null</code>. | |
104 | * @throws ArgumentError if the <code>typeKeys</code> argument is <code>null</code>. | |
105 | * @throws ArgumentError if the <code>typeValues</code> argument is <code>null</code>. | |
106 | * @throws org.as3coreaddendum.errors.ClassCastError if the types of one or more keys or values in the <code>wrapMap</code> argument are incompatible with the <code>typeKeys</code> or <code>typeValues</code> argument. | |
107 | */ | |
108 | public function TypedListMap(wrapMap:IListMap, typeKeys:*, typeValues:*): void | |
109 | { | |
110 | 1 | super(wrapMap, typeKeys, typeValues); |
111 | 1 | } |
112 | ||
113 | /** | |
114 | * Creates and return a new <code>TypedListMap</code> object with the clone of the <code>wrappedMap</code> object. | |
115 | * | |
116 | * @return a new <code>TypedListMap</code> object with the clone of the <code>wrappedMap</code> object. | |
117 | */ | |
118 | override public function clone(): * | |
119 | { | |
120 | 1 | return new TypedListMap(wrappedListMap.clone(), typeKeys, typeValues); |
121 | } | |
122 | ||
123 | /** | |
124 | * Forwards the call to <code>wrappedMap.getKeyAt</code>. | |
125 | * | |
126 | * @param index | |
127 | * @return | |
128 | */ | |
129 | public function getKeyAt(index:int): * | |
130 | { | |
131 | 1 | return wrappedListMap.getKeyAt(index); |
132 | } | |
133 | ||
134 | /** | |
135 | * Forwards the call to <code>wrappedMap.getValueAt</code>. | |
136 | * | |
137 | * @param index | |
138 | * @return | |
139 | */ | |
140 | public function getValueAt(index:int): * | |
141 | { | |
142 | 1 | return wrappedListMap.getValueAt(index); |
143 | } | |
144 | ||
145 | /** | |
146 | * Forwards the call to <code>wrappedMap.headMap</code>. | |
147 | * | |
148 | * @param toKey | |
149 | * @return | |
150 | */ | |
151 | public function headMap(toKey:*): IListMap | |
152 | { | |
153 | 1 | return new TypedListMap(wrappedListMap.headMap(toKey), typeKeys, typeValues); |
154 | } | |
155 | ||
156 | /** | |
157 | * Forwards the call to <code>wrappedMap.indexOfKey</code>. | |
158 | * | |
159 | * @param key | |
160 | * @return | |
161 | */ | |
162 | public function indexOfKey(key:*): int | |
163 | { | |
164 | 1 | return wrappedListMap.indexOfKey(key); |
165 | } | |
166 | ||
167 | /** | |
168 | * Forwards the call to <code>wrappedMap.indexOfValue</code>. | |
169 | * | |
170 | * @param value | |
171 | * @return | |
172 | */ | |
173 | public function indexOfValue(value:*): int | |
174 | { | |
175 | 1 | return wrappedListMap.indexOfValue(value); |
176 | } | |
177 | ||
178 | /** | |
179 | * Returns a <code>IListMapIterator</code> object to iterate over the mappings in this map (in proper sequence), starting at the specified position in this map. | |
180 | * The specified index indicates the first value that would be returned by an initial call to <code>next</code>. | |
181 | * An initial call to <code>previous</code> would return the value with the specified index minus one. | |
182 | * <p>This implementation always throws an <code>UnsupportedOperationError</code>.</p> | |
183 | * | |
184 | * @param index index of first value to be returned from the iterator (by a call to the <code>next</code> method) | |
185 | * @return a <code>IListMapIterator</code> object to iterate over the mappings in this map (in proper sequence), starting at the specified position in this map. | |
186 | */ | |
187 | public function listMapIterator(index:int = 0): IListMapIterator | |
188 | { | |
189 | 1 | return new ListMapIterator(this, index); |
190 | } | |
191 | ||
192 | /** | |
193 | * The map is validated to be forwarded to <code>wrappedMap.putAllAt</code>. | |
194 | * | |
195 | * @param index | |
196 | * @param map | |
197 | */ | |
198 | public function putAllAt(index:int, map:IMap): void | |
199 | { | |
200 | 1 | validateMap(map); |
201 | 1 | wrappedListMap.putAllAt(index, map); |
202 | 1 | } |
203 | ||
204 | /** | |
205 | * The key and value are validated to be forwarded to <code>wrappedMap.putAt</code>. | |
206 | * | |
207 | * @param index index at which the specified mapping is to be inserted. | |
208 | * @param key key with which the specified value is to be associated. | |
209 | * @param value value to be associated with the specified key. | |
210 | * @throws org.as3coreaddendum.errors.ClassCastError if the type of the specified key or value is incompatible with this map. | |
211 | * @return the return of the call <code>wrappedMap.put</code>. | |
212 | */ | |
213 | public function putAt(index:int, key:*, value:*): void | |
214 | { | |
215 | 1 | validateKey(key); |
216 | 1 | validateValue(value); |
217 | 1 | wrappedListMap.putAt(index, key, value); |
218 | 1 | } |
219 | ||
220 | /** | |
221 | * Forwards the call to <code>wrappedMap.removeAt</code>. | |
222 | * | |
223 | * @param index | |
224 | */ | |
225 | public function removeAt(index:int): IMapEntry | |
226 | { | |
227 | 1 | return wrappedListMap.removeAt(index); |
228 | } | |
229 | ||
230 | /** | |
231 | * Forwards the call to <code>wrappedMap.reverse</code>. | |
232 | * | |
233 | * @param index | |
234 | */ | |
235 | public function reverse(): void | |
236 | { | |
237 | 1 | wrappedListMap.reverse(); |
238 | 1 | } |
239 | ||
240 | /** | |
241 | * Forwards the call to <code>wrappedMap.removeRange</code>. | |
242 | * | |
243 | * @param fromIndex | |
244 | * @param toIndex | |
245 | * @return the return of the call <code>wrappedMap.removeRange</code>. | |
246 | */ | |
247 | public function removeRange(fromIndex:int, toIndex:int): IListMap | |
248 | { | |
249 | 1 | return wrappedListMap.removeRange(fromIndex, toIndex); |
250 | } | |
251 | ||
252 | /** | |
253 | * The key is validated to be forwarded to <code>wrappedMap.setKeyAt</code>. | |
254 | * | |
255 | * @param index | |
256 | * @param key the key to forward to <code>wrappedMap.setKeyAt</code>. | |
257 | * @throws org.as3coreaddendum.errors.ClassCastError if the type of the specified key or value is incompatible with this map. | |
258 | * @return the return of the call <code>wrappedMap.setKeyAt</code>. | |
259 | */ | |
260 | public function setKeyAt(index:int, key:*): * | |
261 | { | |
262 | 1 | validateKey(key); |
263 | 1 | return wrappedListMap.setKeyAt(index, key); |
264 | } | |
265 | ||
266 | /** | |
267 | * The value is validated to be forwarded to <code>wrappedMap.setValueAt</code>. | |
268 | * | |
269 | * @param index | |
270 | * @param key the key to forward to <code>wrappedMap.setValueAt</code>. | |
271 | * @throws org.as3coreaddendum.errors.ClassCastError if the type of the specified key or value is incompatible with this map. | |
272 | * @return the return of the call <code>wrappedMap.setValueAt</code>. | |
273 | */ | |
274 | public function setValueAt(index:int, value:*): * | |
275 | { | |
276 | 1 | validateValue(value); |
277 | 1 | return wrappedListMap.setValueAt(index, value); |
278 | } | |
279 | ||
280 | /** | |
281 | * Forwards the call to <code>wrappedMap.subMap</code>. | |
282 | * | |
283 | * @param fromIndex | |
284 | * @param toIndex | |
285 | * @return | |
286 | */ | |
287 | public function subMap(fromIndex:int, toIndex:int): IListMap | |
288 | { | |
289 | 1 | return new TypedListMap(wrappedListMap.subMap(fromIndex, toIndex), typeKeys, typeValues); |
290 | } | |
291 | ||
292 | /** | |
293 | * Forwards the call to <code>wrappedMap.tailMap</code>. | |
294 | * | |
295 | * @param fromKey | |
296 | * @return | |
297 | */ | |
298 | public function tailMap(fromKey:*): IListMap | |
299 | { | |
300 | 1 | return new TypedListMap(wrappedListMap.tailMap(fromKey), typeKeys, typeValues); |
301 | } | |
302 | ||
303 | } | |
304 | ||
305 | } |