| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayListMap |
|
| 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.AbstractListMap; | |
| 33 | import org.as3collections.ICollection; | |
| 34 | import org.as3collections.IIterator; | |
| 35 | import org.as3collections.IList; | |
| 36 | import org.as3collections.IListMap; | |
| 37 | import org.as3collections.IListMapIterator; | |
| 38 | import org.as3collections.IMap; | |
| 39 | import org.as3collections.IMapEntry; | |
| 40 | import org.as3collections.MapEntry; | |
| 41 | import org.as3collections.errors.IndexOutOfBoundsError; | |
| 42 | import org.as3collections.iterators.ListMapIterator; | |
| 43 | import org.as3collections.iterators.MapIterator; | |
| 44 | import org.as3utils.EquatableUtil; | |
| 45 | ||
| 46 | import flash.errors.IllegalOperationError; | |
| 47 | ||
| 48 | /** | |
| 49 | * Array based implementation of the <code>IMap</code> interface. | |
| 50 | * This implementation provides all of the optional map operations, and permits <code>null</code> values and the <code>null</code> key. | |
| 51 | * <p>This class makes guarantees as to the order of the map. | |
| 52 | * The order in which elements are stored is the order in which they were inserted.</p> | |
| 53 | * <p>This class has great similarity to <code>ArrayList</code> class. | |
| 54 | * In a way this class can be thought of as an <code>ArrayList</code> of mappings.</p> | |
| 55 | * <p>It's possible to create typed list maps. | |
| 56 | * You just sends the <code>ArrayListMap</code> object to the wrapper <code>TypedListMap</code> or uses the <code>MapUtil.getTypedListMap</code>.</p> | |
| 57 | * | |
| 58 | * @example | |
| 59 | * | |
| 60 | * <listing version="3.0"> | |
| 61 | * import org.as3collections.IMap; | |
| 62 | * import org.as3collections.IList; | |
| 63 | * import org.as3collections.maps.ArrayListMap; | |
| 64 | * import org.as3collections.maps.MapEntry; | |
| 65 | * | |
| 66 | * var map1:IMap = new ArrayListMap(); | |
| 67 | * var tf1:TextField = new TextField(); | |
| 68 | * var tf2:TextField = new TextField(); | |
| 69 | * | |
| 70 | * map1 // {} | |
| 71 | * map1.containsKey("a") // false | |
| 72 | * map1.containsKey(tf2) // false | |
| 73 | * map1.containsValue(2) // false | |
| 74 | * map1.containsValue(tf1) // false | |
| 75 | * map1.isEmpty() // true | |
| 76 | * map1.size() // 0 | |
| 77 | * | |
| 78 | * map1.put("a", 1) // null | |
| 79 | * map1 // {a=1} | |
| 80 | * map1.isEmpty() // false | |
| 81 | * map1.size() // 1 | |
| 82 | * map1.containsKey("a") // true | |
| 83 | * map1.containsKey(tf2) // false | |
| 84 | * map1.containsValue(2) // false | |
| 85 | * map1.containsValue(tf1) // false | |
| 86 | * | |
| 87 | * map1.put("b", 2) // null | |
| 88 | * map1 // {a=1,b=2} | |
| 89 | * map1.isEmpty() // false | |
| 90 | * map1.size() // 2 | |
| 91 | * map1.containsKey("a") // true | |
| 92 | * map1.containsKey("b") // true | |
| 93 | * map1.containsKey(tf2) // false | |
| 94 | * map1.containsValue(2) // true | |
| 95 | * | |
| 96 | * map1.put("c", 3) // null | |
| 97 | * map1 // {a=1,b=2,c=3} | |
| 98 | * map1.size() // 3 | |
| 99 | * | |
| 100 | * map1.put("tf1", tf1) // null | |
| 101 | * map1 // {a=1,b=2,c=3,tf1=[object TextField]} | |
| 102 | * map1.size() // 4 | |
| 103 | * map1.containsValue(tf1) // true | |
| 104 | * | |
| 105 | * map1.put(tf2, "tf2") // null | |
| 106 | * map1 // {a=1,b=2,c=3,tf1=[object TextField],[object TextField]=tf2} | |
| 107 | * map1.size() // 5 | |
| 108 | * map1.containsKey(tf2) // true | |
| 109 | * | |
| 110 | * map1.put("a", 1.1) // 1 | |
| 111 | * map1 // {a=1.1,b=2,c=3,tf1=[object TextField],[object TextField]=tf2} | |
| 112 | * map1.size() // 5 | |
| 113 | * | |
| 114 | * map1.put("tf1", String) // [object TextField] | |
| 115 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2} | |
| 116 | * map1.size() // 5 | |
| 117 | * | |
| 118 | * map1.put(tf2, "tf2.1") // tf2 | |
| 119 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2.1} | |
| 120 | * map1.size() // 5 | |
| 121 | * | |
| 122 | * map1.put(Number, 999) // null | |
| 123 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2.1,[class Number]=999} | |
| 124 | * map1.size() // 6 | |
| 125 | * | |
| 126 | * map1.getValue("b") // 2 | |
| 127 | * | |
| 128 | * map1.getValue(tf2) // tf2.1 | |
| 129 | * | |
| 130 | * map1.putAllByObject({fa:"fb",ga:"gb",ha:"hb"}); | |
| 131 | * | |
| 132 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2.1,[class Number]=999,ha=hb,ga=gb,fa=fb} | |
| 133 | * | |
| 134 | * map1.size() // 9 | |
| 135 | * | |
| 136 | * map1.getValue("fa") // fb | |
| 137 | * | |
| 138 | * map1.remove("ga") // gb | |
| 139 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2.1,[class Number]=999,ha=hb,fa=fb} | |
| 140 | * map1.size() // 8 | |
| 141 | * | |
| 142 | * map1.remove("fa") // fb | |
| 143 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[object TextField]=tf2.1,[class Number]=999,ha=hb} | |
| 144 | * map1.size() // 7 | |
| 145 | * | |
| 146 | * map1.remove(tf2) // tf2.1 | |
| 147 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[class Number]=999,ha=hb} | |
| 148 | * map1.size() // 6 | |
| 149 | * | |
| 150 | * map1.getValue("fa") // null | |
| 151 | * map1.getValue(tf2) // null | |
| 152 | * | |
| 153 | * var map2:IMap = map1.clone(); | |
| 154 | * | |
| 155 | * map2 // {a=1.1,b=2,c=3,tf1=[class String],[class Number]=999,ha=hb} | |
| 156 | * map2.size() // 6 | |
| 157 | * map2.isEmpty() // false | |
| 158 | * | |
| 159 | * map1.equals(map2) // true | |
| 160 | * map2.equals(map1) // true | |
| 161 | * map2.equals(map2) // true | |
| 162 | * | |
| 163 | * map2.remove("b") // 2 | |
| 164 | * map2 // {a=1.1,c=3,tf1=[class String],[class Number]=999,ha=hb} | |
| 165 | * map2.equals(map2) // true | |
| 166 | * map2.size() // 5 | |
| 167 | * | |
| 168 | * map1.equals(map2) // false | |
| 169 | * map2.equals(map1) // false | |
| 170 | * | |
| 171 | * map2.getValues() // [1.1,3,[class String],999,hb] | |
| 172 | * | |
| 173 | * var keysMap2:IList = map2.getKeys(); | |
| 174 | * | |
| 175 | * keysMap2 // [a,c,tf1,[class Number],ha] | |
| 176 | * | |
| 177 | * keysMap2.remove("c") // true | |
| 178 | * keysMap2 // [a,tf1,[class Number],ha] | |
| 179 | * map2 // {a=1.1,c=3,tf1=[class String],[class Number]=999,ha=hb} | |
| 180 | * map2.size() // 5 | |
| 181 | * | |
| 182 | * map2.removeAll(keysMap2) // true | |
| 183 | * map2 // {c=3} | |
| 184 | * map2.size() // 1 | |
| 185 | * map2.isEmpty() // false | |
| 186 | * | |
| 187 | * map2.clear(); | |
| 188 | * | |
| 189 | * map2 // {} | |
| 190 | * map2.size() // 0 | |
| 191 | * map2.isEmpty() // true | |
| 192 | * | |
| 193 | * var entry:IMapEntry = new MapEntry("c", 3); | |
| 194 | * | |
| 195 | * entry // c=3 | |
| 196 | * map2.putEntry(entry) // null | |
| 197 | * map2 // {c=3} | |
| 198 | * map2.size() // 1 | |
| 199 | * | |
| 200 | * map1 // {a=1.1,b=2,c=3,tf1=[class String],[class Number]=999,ha=hb} | |
| 201 | * map1.retainAll(map2) // true | |
| 202 | * map1 // {c=3} | |
| 203 | * map1.size() // 1 | |
| 204 | * map1.isEmpty() // false | |
| 205 | * | |
| 206 | * map1.put("d", 4) // null | |
| 207 | * map1.put("e", 5) // null | |
| 208 | * map1.put("f", 6) // null | |
| 209 | * map1 // {c=3,d=4,e=5,f=6} | |
| 210 | * map1.size() // 4 | |
| 211 | * | |
| 212 | * var it:IIterator = map1.iterator(); | |
| 213 | * | |
| 214 | * var e:*; | |
| 215 | * | |
| 216 | * while (it.hasNext()) | |
| 217 | * { | |
| 218 | * | |
| 219 | * e = it.next(); | |
| 220 | * trace(it.pointer() + "=" + e) // c=3 | |
| 221 | * | |
| 222 | * e = it.next(); | |
| 223 | * trace(it.pointer() + "=" + e) // d=4 | |
| 224 | * | |
| 225 | * if (e == 4) | |
| 226 | * { | |
| 227 | * it.remove(); | |
| 228 | * } | |
| 229 | * | |
| 230 | * e = it.next(); | |
| 231 | * trace(it.pointer() + "=" + e) // e=5 | |
| 232 | * | |
| 233 | * e = it.next(); | |
| 234 | * trace(it.pointer() + "=" + e) // f=6 | |
| 235 | * } | |
| 236 | * | |
| 237 | * map1 // {c=3,e=5,f=6} | |
| 238 | * map1.size() // 3 | |
| 239 | * </listing> | |
| 240 | * | |
| 241 | * @see org.as3collections.AbstractListMap AbstractListMap | |
| 242 | * @see org.as3collections.maps.TypedListMap TypedListMap | |
| 243 | * @see org.as3collections.maps.SortedArrayListMap SortedArrayListMap | |
| 244 | * @see org.as3collections.utils.MapUtil#getTypedListMap() MapUtil.getTypedListMap() | |
| 245 | * @author Flávio Silva | |
| 246 | */ | |
| 247 | public class ArrayListMap extends AbstractListMap | |
| 248 | { | |
| 249 | /** | |
| 250 | * Constructor, creates a new <code>ArrayListMap</code> object. | |
| 251 | * | |
| 252 | * @param source a map with wich fill this map. | |
| 253 | */ | |
| 254 | public function ArrayListMap(source:IMap = null) | |
| 255 | { | |
| 256 | 1 | super(source); |
| 257 | 1 | } |
| 258 | ||
| 259 | /** | |
| 260 | * Removes all of the mappings from this map. | |
| 261 | * The map will be empty after this call returns. | |
| 262 | */ | |
| 263 | override public function clear(): void | |
| 264 | { | |
| 265 | 1 | if (isEmpty()) return; |
| 266 | ||
| 267 | 1 | keys.clear(); |
| 268 | 1 | values.clear(); |
| 269 | 1 | _modCount++; |
| 270 | 1 | } |
| 271 | ||
| 272 | /** | |
| 273 | * Creates and return a new <code>ArrayListMap</code> object containing all mappings in this map (in the same order). | |
| 274 | * | |
| 275 | * @return a new <code>ArrayListMap</code> object containing all mappings in this map. | |
| 276 | */ | |
| 277 | override public function clone(): * | |
| 278 | { | |
| 279 | 1 | return new ArrayListMap(this); |
| 280 | } | |
| 281 | ||
| 282 | /** | |
| 283 | * Returns an iterator over a set of mappings. | |
| 284 | * <p>This implementation returns a <code>MapIterator</code> object.</p> | |
| 285 | * | |
| 286 | * @return an iterator over a set of values. | |
| 287 | * @see org.as3collections.iterators.MapIterator MapIterator | |
| 288 | */ | |
| 289 | override public function iterator(): IIterator | |
| 290 | { | |
| 291 | 1 | return new MapIterator(this); |
| 292 | } | |
| 293 | ||
| 294 | /** | |
| 295 | * 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. | |
| 296 | * <p>This implementation returns a <code>ListMapIterator</code> object.</p> | |
| 297 | * | |
| 298 | * @param index index of first value to be returned from the iterator (by a call to the <code>next</code> method) | |
| 299 | * @return a <code>ListMapIterator</code> object to iterate over the mappings in this map (in proper sequence), starting at the specified position in this map. | |
| 300 | */ | |
| 301 | override public function listMapIterator(index:int = 0): IListMapIterator | |
| 302 | { | |
| 303 | 1 | return new ListMapIterator(this, index); |
| 304 | } | |
| 305 | ||
| 306 | /** | |
| 307 | * Associates the specified value with the specified key in this map. | |
| 308 | * If the map previously contained a mapping for the key, the old value is replaced by the specified value, and the order of the key is not affected. | |
| 309 | * (A map <code>m</code> is said to contain a mapping for a key <code>k</code> if and only if <code>m.containsKey(k)</code> would return <code>true</code>.) | |
| 310 | * | |
| 311 | * @param key key with which the specified value is to be associated. | |
| 312 | * @param value value to be associated with the specified key. | |
| 313 | * @return the previous value associated with key, or <code>null</code> if there was no mapping for key. (A <code>null</code> return can also indicate that the map previously associated <code>null</code> with key, because this implementation supports <code>null</code> values.) | |
| 314 | */ | |
| 315 | override public function put(key:*, value:*): * | |
| 316 | { | |
| 317 | 1 | var old:* = null; |
| 318 | ||
| 319 | 1 | if (containsKey(key)) |
| 320 | { | |
| 321 | 1 | old = getValue(key); |
| 322 | 1 | values.setAt(indexOfKey(key), value); |
| 323 | ||
| 324 | 1 | valueRemoved(old); |
| 325 | 1 | valueAdded(value); |
| 326 | ||
| 327 | 1 | return old; |
| 328 | } | |
| 329 | else | |
| 330 | { | |
| 331 | 1 | keys.add(key); |
| 332 | 1 | values.add(value); |
| 333 | ||
| 334 | 1 | keyAdded(key); |
| 335 | 1 | valueAdded(value); |
| 336 | ||
| 337 | 1 | return null; |
| 338 | } | |
| 339 | } | |
| 340 | ||
| 341 | /** | |
| 342 | * Associates the specified value with the specified key at the specified position in this map. | |
| 343 | * Shifts the entry currently at that position (if any) and any subsequent entries to the right (adds one to their indices). | |
| 344 | * | |
| 345 | * @param index index at which the specified entry is to be inserted. | |
| 346 | * @param key key with which the specified value is to be associated. | |
| 347 | * @param value value to be associated with the specified key. | |
| 348 | * @throws ArgumentError if this map already contains the specified key. | |
| 349 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index > size())</code>. | |
| 350 | */ | |
| 351 | override public function putAt(index:int, key:*, value:*): void | |
| 352 | { | |
| 353 | 1 | if (containsKey(key)) |
| 354 | { | |
| 355 | 1 | var message:String = "Argument <key> is already in this map (at index: <" + indexOfKey(key) + ">.\n"; |
| 356 | 1 | message += "Provided <index> argument: " + index + "\n"; |
| 357 | 1 | message += "Provided <key> argument: " + key + "\n"; |
| 358 | ||
| 359 | 1 | throw new ArgumentError(); |
| 360 | } | |
| 361 | ||
| 362 | 1 | checkIndex(index, size()); |
| 363 | 1 | keys.addAt(index, key); |
| 364 | 1 | values.addAt(index, value); |
| 365 | ||
| 366 | 1 | keyAdded(key); |
| 367 | 1 | valueAdded(value); |
| 368 | 1 | } |
| 369 | ||
| 370 | /** | |
| 371 | * Removes the mapping for a key from this map if it is present. | |
| 372 | * <p>Returns the value to which this map previously associated the key, or <code>null</code> if the map contained no mapping for the key. | |
| 373 | * A return value of <code>null</code> does not <em>necessarily</em> indicate that the map contained no mapping for the key. | |
| 374 | * It's possible that the map explicitly mapped the key to <code>null</code>.</p> | |
| 375 | * <p>The map will not contain a mapping for the specified key once the call returns.</p> | |
| 376 | * | |
| 377 | * @param key the key whose mapping is to be removed from the map. | |
| 378 | * @return the previous value associated with key, or <code>null</code> if there was no mapping for <code>key</code>. | |
| 379 | */ | |
| 380 | override public function remove(key:*): * | |
| 381 | { | |
| 382 | 1 | if (!containsKey(key)) return null; |
| 383 | ||
| 384 | 1 | var old:* = getValue(key); |
| 385 | 1 | var index:int = indexOfKey(key); |
| 386 | ||
| 387 | 1 | keys.removeAt(index); |
| 388 | 1 | values.removeAt(index); |
| 389 | ||
| 390 | 1 | keyRemoved(key); |
| 391 | 1 | valueRemoved(old); |
| 392 | ||
| 393 | 1 | return old; |
| 394 | } | |
| 395 | ||
| 396 | /** | |
| 397 | * Removes the mapping at the specified position in this map (optional operation). | |
| 398 | * Shifts any subsequent mappings to the left (subtracts one from their indices). | |
| 399 | * Returns an <code>IMapEntry</code> object containing the mapping (key/value) that was removed from the map. | |
| 400 | * <p>This implementation always throws an <code>UnsupportedOperationError</code>.</p> | |
| 401 | * | |
| 402 | * @param index the index of the mapping to be removed. | |
| 403 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError if the <code>removeAt</code> operation is not supported by this map. | |
| 404 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index >= size())</code>. | |
| 405 | * @return an <code>IMapEntry</code> object containing the mapping (key/value) that was removed from the map. | |
| 406 | */ | |
| 407 | override public function removeAt(index:int): IMapEntry | |
| 408 | { | |
| 409 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This map is empty."); |
| 410 | ||
| 411 | 1 | checkIndex(index, size() - 1); |
| 412 | ||
| 413 | 1 | var key:* = getKeyAt(index); |
| 414 | 1 | var value:* = getValueAt(index); |
| 415 | 1 | var entry:IMapEntry = new MapEntry(key, value); |
| 416 | ||
| 417 | 1 | keys.removeAt(index); |
| 418 | 1 | values.removeAt(index); |
| 419 | ||
| 420 | 1 | keyRemoved(key); |
| 421 | 1 | valueRemoved(value); |
| 422 | ||
| 423 | 1 | return entry; |
| 424 | } | |
| 425 | ||
| 426 | /** | |
| 427 | * Removes all of the mappings whose index is between <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive (optional operation). | |
| 428 | * Shifts any subsequent mappings to the left (subtracts their indices). | |
| 429 | * <p>If <code>toIndex == fromIndex</code>, this operation has no effect.</p> | |
| 430 | * | |
| 431 | * @param fromIndex the index to start removing mappings (inclusive). | |
| 432 | * @param toIndex the index to stop removing mappings (exclusive). | |
| 433 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError if the <code>removeRange</code> operation is not supported by this map. | |
| 434 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
| 435 | * @return a new map containing all the removed mappings. | |
| 436 | */ | |
| 437 | override public function removeRange(fromIndex:int, toIndex:int): IListMap | |
| 438 | { | |
| 439 | 1 | if (isEmpty()) throw new IllegalOperationError("This map is empty."); |
| 440 | ||
| 441 | 1 | checkIndex(fromIndex, size()); |
| 442 | 1 | checkIndex(toIndex, size()); |
| 443 | ||
| 444 | 1 | var removedKeys:ICollection = keys.removeRange(fromIndex, toIndex); |
| 445 | 1 | var removedValues:ICollection = values.removeRange(fromIndex, toIndex); |
| 446 | ||
| 447 | 1 | var removedMap:IListMap = createEmptyMap(); |
| 448 | 1 | var itKeys:IIterator = removedKeys.iterator(); |
| 449 | 1 | var itValues:IIterator = removedValues.iterator(); |
| 450 | var removedKey:*; | |
| 451 | var removedValue:*; | |
| 452 | ||
| 453 | 1 | while(itKeys.hasNext()) |
| 454 | { | |
| 455 | 1 | removedKey = itKeys.next(); |
| 456 | 1 | removedValue = itValues.next(); |
| 457 | ||
| 458 | 1 | keyRemoved(removedKey); |
| 459 | 1 | valueRemoved(removedValue); |
| 460 | ||
| 461 | 1 | removedMap.put(removedKey, removedValue); |
| 462 | } | |
| 463 | ||
| 464 | 1 | return removedMap; |
| 465 | } | |
| 466 | ||
| 467 | /** | |
| 468 | * Replaces the key at the specified position in this map with the specified key (optional operation). | |
| 469 | * | |
| 470 | * @param index index of the key to replace. | |
| 471 | * @param key key to be stored at the specified position. | |
| 472 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError if the <code>setKeyAt</code> operation is not supported by this map. | |
| 473 | * @throws org.as3coreaddendum.errors.ClassCastError if the class of the specified key prevents it from being added to this map. | |
| 474 | * @throws ArgumentError if the specified key is <code>null</code> and this map does not permit <code>null</code> keys. | |
| 475 | * @throws ArgumentError if this map already contains the specified key. | |
| 476 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index >= size())</code>. | |
| 477 | * @return the key previously at the specified position. | |
| 478 | */ | |
| 479 | override public function setKeyAt(index:int, key:*): * | |
| 480 | { | |
| 481 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This map is empty."); |
| 482 | 1 | checkIndex(index, size() - 1); |
| 483 | ||
| 484 | 1 | var old:* = keys.getAt(index); |
| 485 | ||
| 486 | 1 | if (!EquatableUtil.areEqual(old, key) && containsKey(key)) |
| 487 | { | |
| 488 | 1 | var message:String = "Argument <key> is already in this map (at index: <" + indexOfKey(key) + ">.\n"; |
| 489 | 1 | message += "Provided <index> argument: " + index + "\n"; |
| 490 | 1 | message += "Provided <key> argument: " + key + "\n"; |
| 491 | ||
| 492 | 1 | throw new ArgumentError(); |
| 493 | } | |
| 494 | ||
| 495 | 1 | keys.setAt(index, key); |
| 496 | ||
| 497 | 1 | keyRemoved(old); |
| 498 | 1 | keyAdded(key); |
| 499 | 1 | _modCount -= 2;// keyRemoved() and keyAdded() will undesirably increase modCount. |
| 500 | ||
| 501 | 1 | return old; |
| 502 | } | |
| 503 | ||
| 504 | /** | |
| 505 | * Replaces the value at the specified position in this map with the specified value (optional operation). | |
| 506 | * <p>This implementation always throws an <code>UnsupportedOperationError</code>.</p> | |
| 507 | * | |
| 508 | * @param index index of the value to replace. | |
| 509 | * @param value value to be stored at the specified position. | |
| 510 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError if the <code>setValueAt</code> operation is not supported by this map. | |
| 511 | * @throws org.as3coreaddendum.errors.ClassCastError if the class of the specified value prevents it from being added to this map. | |
| 512 | * @throws ArgumentError if the specified value is <code>null</code> and this map does not permit <code>null</code> values. | |
| 513 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index >= size())</code>. | |
| 514 | * @return the value previously at the specified position. | |
| 515 | */ | |
| 516 | override public function setValueAt(index:int, value:*): * | |
| 517 | { | |
| 518 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This map is empty."); |
| 519 | 1 | checkIndex(index, size() - 1); |
| 520 | ||
| 521 | 1 | var old:* = values.getAt(index); |
| 522 | 1 | values.setAt(index, value); |
| 523 | ||
| 524 | 1 | valueRemoved(old); |
| 525 | 1 | valueAdded(value); |
| 526 | ||
| 527 | 1 | return old; |
| 528 | } | |
| 529 | ||
| 530 | /** | |
| 531 | * Returns a new map that is a view of the portion of this map between the specified <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive. | |
| 532 | * <p>The returned map supports all of the optional map operations supported by this map.</p> | |
| 533 | * | |
| 534 | * @param fromIndex the index to start retrieving mappings (inclusive). | |
| 535 | * @param toIndex the index to stop retrieving mappings (exclusive). | |
| 536 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError if the <code>subMap</code> operation is not supported by this map. | |
| 537 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
| 538 | * @return a new list that is a view of the specified range within this list. | |
| 539 | */ | |
| 540 | override public function subMap(fromIndex:int, toIndex:int): IListMap | |
| 541 | { | |
| 542 | 1 | if (isEmpty()) throw new IllegalOperationError("This ArrayListMap instance is empty."); |
| 543 | ||
| 544 | 1 | checkIndex(fromIndex, size()); |
| 545 | 1 | checkIndex(toIndex, size()); |
| 546 | ||
| 547 | 1 | if (fromIndex > toIndex) throw new ArgumentError("Argument <fromIndex> cannot be greater than argument <toIndex>. fromIndex: " + fromIndex + " | toIndex" + toIndex); |
| 548 | ||
| 549 | 1 | var entryList:IList = (entryCollection() as IList).subList(fromIndex, toIndex); |
| 550 | 1 | var map:IListMap = createEmptyMap(); |
| 551 | ||
| 552 | 1 | var it:IIterator = entryList.iterator(); |
| 553 | ||
| 554 | 1 | while (it.hasNext()) |
| 555 | { | |
| 556 | 1 | map.putEntry(it.next()); |
| 557 | } | |
| 558 | ||
| 559 | 1 | return map; |
| 560 | } | |
| 561 | ||
| 562 | /** | |
| 563 | * @private | |
| 564 | */ | |
| 565 | protected function createEmptyMap(): IListMap | |
| 566 | { | |
| 567 | 1 | return new ArrayListMap(); |
| 568 | } | |
| 569 | ||
| 570 | } | |
| 571 | ||
| 572 | } |