| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayList |
|
| 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.lists |
| 31 | { | |
| 32 | import org.as3collections.AbstractList; | |
| 33 | import org.as3collections.ICollection; | |
| 34 | import org.as3collections.IIterator; | |
| 35 | import org.as3collections.IList; | |
| 36 | import org.as3collections.IListIterator; | |
| 37 | import org.as3collections.errors.IndexOutOfBoundsError; | |
| 38 | import org.as3collections.iterators.ArrayIterator; | |
| 39 | import org.as3collections.iterators.ListIterator; | |
| 40 | ||
| 41 | /** | |
| 42 | * Resizable-array implementation of the <code>IList</code> interface. | |
| 43 | * Implements all optional list operations, and permits all elements, including <code>null</code>. | |
| 44 | * <p>Each <code>ArrayList</code> instance has a capacity. | |
| 45 | * The capacity is the size of the array used to store the elements in the list. | |
| 46 | * It is always at least as large as the list size. | |
| 47 | * As elements are added to an <code>ArrayList</code> object, its capacity grows automatically.</p> | |
| 48 | * <p>In addition to implementing the <code>IList</code> interface, this class provides the <code>ensureCapacity</code> method to arbitrarily manipulate the size of the array (this usage is not common) that is used internally to store the elements. | |
| 49 | * Check the examples at the bottom of the page for further information about usage.</p> | |
| 50 | * <p>It's possible to create unique lists, typed lists and even unique typed lists. | |
| 51 | * You just send the <code>ArrayList</code> object to the wrappers <code>UniqueList</code> or <code>TypedList</code> or uses the <code>ListUtil.getUniqueList</code>, <code>ListUtil.getTypedList</code> or <code>ListUtil.getUniqueTypedList</code>.</p> | |
| 52 | * <p>This documentation is partially based in the <em>Java Collections Framework</em> JavaDoc documentation. | |
| 53 | * For further information see <a href="http://download.oracle.com/javase/6/docs/technotes/guides/collections/index.html" target="_blank">Java Collections Framework</a></p> | |
| 54 | * | |
| 55 | * @example | |
| 56 | * | |
| 57 | * <b>Example 1</b> | |
| 58 | * <listing version="3.0"> | |
| 59 | * import org.as3collections.IList; | |
| 60 | * import org.as3collections.lists.ArrayList; | |
| 61 | * | |
| 62 | * var list1:IList = new ArrayList(); | |
| 63 | * | |
| 64 | * list1 // [] | |
| 65 | * | |
| 66 | * list1.size() // 0 | |
| 67 | * list1.contains(null) // false | |
| 68 | * list1.contains("abc") // false | |
| 69 | * list1.isEmpty() // true | |
| 70 | * list1.modCount // 0 | |
| 71 | * | |
| 72 | * list1.clear() | |
| 73 | * | |
| 74 | * list1.modCount // 0 | |
| 75 | * list1.isEmpty() // true | |
| 76 | * list1.size() // 0 | |
| 77 | * | |
| 78 | * list1.add(null) // true | |
| 79 | * list1 // [null] | |
| 80 | * list1.isEmpty() // false | |
| 81 | * list1.size() // 1 | |
| 82 | * list1.modCount // 1 | |
| 83 | * list1.contains(null) // true | |
| 84 | * list1.contains("abc") // false | |
| 85 | * | |
| 86 | * list1.add("abc") // true | |
| 87 | * list1 // [null,abc] | |
| 88 | * list1.size() // 2 | |
| 89 | * list1.modCount // 2 | |
| 90 | * list1.contains("abc") // true | |
| 91 | * | |
| 92 | * list1.add(null) // true | |
| 93 | * list1 // [null,abc,null] | |
| 94 | * list1.size() // 3 | |
| 95 | * list1.modCount // 3 | |
| 96 | * list1.indexOf(null) // 0 | |
| 97 | * list1.lastIndexOf(null) // 2 | |
| 98 | * | |
| 99 | * list1.addAt(0, 123) // true | |
| 100 | * list1 // [123,null,abc,null] | |
| 101 | * list1.size() // 4 | |
| 102 | * list1.modCount // 4 | |
| 103 | * | |
| 104 | * list1.addAt(4, "def") // true | |
| 105 | * list1 // [123,null,abc,null,def] | |
| 106 | * list1.size() // 5 | |
| 107 | * | |
| 108 | * list1.addAt(4, "abc") // true | |
| 109 | * list1 // [123,null,abc,null,abc,def] | |
| 110 | * list1.size() // 6 | |
| 111 | * list1.modCount // 6 | |
| 112 | * | |
| 113 | * list1.getAt(0) // 123 | |
| 114 | * list1.getAt(2) // abc | |
| 115 | * list1.getAt(5) // def | |
| 116 | * | |
| 117 | * list1.removeAt(0) // 123 | |
| 118 | * list1 // [null,abc,null,abc,def] | |
| 119 | * list1.size() // 5 | |
| 120 | * list1.modCount // 7 | |
| 121 | * | |
| 122 | * list1.removeAt(4) // def | |
| 123 | * list1 // [null,abc,null,abc] | |
| 124 | * list1.size() // 4 | |
| 125 | * list1.modCount // 8 | |
| 126 | * | |
| 127 | * list1.removeAt(0) // null | |
| 128 | * list1 // [abc,null,abc] | |
| 129 | * list1.size() // 3 | |
| 130 | * list1.modCount // 9 | |
| 131 | * | |
| 132 | * var list2:IList = list1.clone(); | |
| 133 | * | |
| 134 | * list2 // [abc,null,abc] | |
| 135 | * | |
| 136 | * list1.containsAll(list1) // true | |
| 137 | * list1.containsAll(list2) // true | |
| 138 | * list2.containsAll(list1) // true | |
| 139 | * list1.equals(list2) // true | |
| 140 | * | |
| 141 | * list2.remove("abc") // true | |
| 142 | * list2.remove("abc") // true | |
| 143 | * list2.add(null) // true | |
| 144 | * list2 // [null,null] | |
| 145 | * | |
| 146 | * list1.containsAll(list2) // true | |
| 147 | * list2.containsAll(list1) // false | |
| 148 | * list1.equals(list2) // false | |
| 149 | * | |
| 150 | * list1 // [abc,null,abc] | |
| 151 | * list1.size() // 3 | |
| 152 | * list1.setAt(2, "ghi") // abc | |
| 153 | * list1 // [abc,null,ghi] | |
| 154 | * list1.size() // 3 | |
| 155 | * list1.modCount // 9 | |
| 156 | * | |
| 157 | * list1.clear() | |
| 158 | * | |
| 159 | * list1.modCount // 10 | |
| 160 | * list1.isEmpty() // true | |
| 161 | * list1.size() // 0 | |
| 162 | * </listing> | |
| 163 | * | |
| 164 | * <b>Example 2</b> | |
| 165 | * <listing version="3.0"> | |
| 166 | * import org.as3collections.IList; | |
| 167 | * import org.as3collections.lists.ArrayList; | |
| 168 | * | |
| 169 | * var arr:Array = [1, 2, 3, 4]; | |
| 170 | * var list1:IList = new ArrayList(arr); | |
| 171 | * | |
| 172 | * list1 // [1,2,3,4] | |
| 173 | * list1.size() // 4 | |
| 174 | * list1.isEmpty() // false | |
| 175 | * list1.modCount // 0 | |
| 176 | * | |
| 177 | * var list2:IList = new ArrayList([9, 10, 11, 12]); | |
| 178 | * | |
| 179 | * list2 // [9,10,11,12] | |
| 180 | * list2.size() // 4 | |
| 181 | * list2.isEmpty() // false | |
| 182 | * list2.modCount // 0 | |
| 183 | * | |
| 184 | * list1.addAll(list2) // true | |
| 185 | * list1 // [1,2,3,4,9,10,11,12] | |
| 186 | * list1.size() // 8 | |
| 187 | * list1.modCount // 4 | |
| 188 | * | |
| 189 | * var list3:IList = new ArrayList([5, 6, 7, 8]); | |
| 190 | * | |
| 191 | * list3 // [5,6,7,8] | |
| 192 | * list3.size() // 4 | |
| 193 | * list3.isEmpty() // false | |
| 194 | * list3.modCount // 0 | |
| 195 | * | |
| 196 | * list1.addAllAt(4, list3) // true | |
| 197 | * list1 // [1,2,3,4,5,6,7,8,9,10,11,12] | |
| 198 | * list1.size() // 12 | |
| 199 | * list1.modCount // 8 | |
| 200 | * | |
| 201 | * list1.containsAll(list3) // true | |
| 202 | * list3.containsAll(list1) // false | |
| 203 | * | |
| 204 | * list1.removeAll(list3) // true | |
| 205 | * list1 // [1,2,3,4,9,10,11,12] | |
| 206 | * list1.size() // 8 | |
| 207 | * list1.modCount // 12 | |
| 208 | * | |
| 209 | * list1.removeAll(list3) // false | |
| 210 | * list1 // [1,2,3,4,9,10,11,12] | |
| 211 | * list1.size() // 8 | |
| 212 | * list1.modCount // 12 | |
| 213 | * | |
| 214 | * list1.retainAll(list2) // true | |
| 215 | * list1 // [9,10,11,12] | |
| 216 | * list1.size() // 4 | |
| 217 | * list1.modCount // 16 | |
| 218 | * | |
| 219 | * list1.subList(0, 1) // [9] | |
| 220 | * list1.subList(0, 2) // [9,10] | |
| 221 | * list1.subList(0, 4) // [9,10,11,12] | |
| 222 | * list1.subList(0, list1.size()) // [9,10,11,12] | |
| 223 | * | |
| 224 | * list1.removeRange(1, 3) // [10,11] | |
| 225 | * list1 // [9,12] | |
| 226 | * list1.size() // 2 | |
| 227 | * list1.modCount // 17 | |
| 228 | * | |
| 229 | * list1.remove(9) // true | |
| 230 | * list1 // [12] | |
| 231 | * list1.size() // 1 | |
| 232 | * list1.modCount // 18 | |
| 233 | * | |
| 234 | * list1.retainAll(list3) // true | |
| 235 | * list1 // [] | |
| 236 | * list1.size() // 0 | |
| 237 | * list1.modCount // 19 | |
| 238 | * </listing> | |
| 239 | * | |
| 240 | * <b>Example 3</b> | |
| 241 | * <listing version="3.0"> | |
| 242 | * import org.as3collections.IList; | |
| 243 | * import org.as3collections.lists.ArrayList; | |
| 244 | * | |
| 245 | * var list1:ArrayList = new ArrayList(); | |
| 246 | * | |
| 247 | * list1 // [] | |
| 248 | * list1.addAt(3, 4) // IndexOutOfBoundsError: The 'index' argument is out of bounds: 3 (min: 0, max: 0) | |
| 249 | * | |
| 250 | * list1.ensureCapacity(5) | |
| 251 | * | |
| 252 | * list1 // [undefined,undefined,undefined,undefined,undefined] | |
| 253 | * list1.modCount // 1 | |
| 254 | * list1.isEmpty() // false | |
| 255 | * list1.size() // 5 | |
| 256 | * | |
| 257 | * list1.addAt(3, 4) // true | |
| 258 | * list1 // [undefined,undefined,undefined,4,undefined,undefined] | |
| 259 | * list1.modCount // 2 | |
| 260 | * list1.size() // 6 | |
| 261 | * | |
| 262 | * list1.getAt(1) // undefined | |
| 263 | * | |
| 264 | * list1.ensureCapacity(3) | |
| 265 | * | |
| 266 | * list1 // [undefined,undefined,undefined,4,undefined,undefined] | |
| 267 | * list1.modCount // 2 | |
| 268 | * list1.size() // 6 | |
| 269 | * | |
| 270 | * list1.setAt(2, 3) // undefined | |
| 271 | * list1 // [undefined,undefined,3,4,undefined,undefined] | |
| 272 | * list1.modCount // 2 | |
| 273 | * list1.size() // 6 | |
| 274 | * | |
| 275 | * list1.remove(undefined) // true | |
| 276 | * list1.remove(undefined) // true | |
| 277 | * list1 // [3,4,undefined,undefined] | |
| 278 | * list1.modCount // 4 | |
| 279 | * list1.size() // 4 | |
| 280 | * </listing> | |
| 281 | * | |
| 282 | * <b>Example 4 - Using equality (org.as3coreaddendum.system.IEquatable)</b> | |
| 283 | * | |
| 284 | * <listing version="3.0"> | |
| 285 | * package test | |
| 286 | * { | |
| 287 | * import org.as3coreaddendum.system.IEquatable; | |
| 288 | * | |
| 289 | * public class TestEquatableObject implements IEquatable | |
| 290 | * { | |
| 291 | * private var _id:String; | |
| 292 | * | |
| 293 | * public function get id(): String { return _id; } | |
| 294 | * | |
| 295 | * public function set id(value:String): void { _id = value; } | |
| 296 | * | |
| 297 | * public function TestEquatableObject(id:String) | |
| 298 | * { | |
| 299 | * _id = id; | |
| 300 | * } | |
| 301 | * | |
| 302 | * public function equals(other:*): Boolean | |
| 303 | * { | |
| 304 | * return other is TestEquatableObject && _id == (other as TestEquatableObject).id; | |
| 305 | * } | |
| 306 | * | |
| 307 | * public function toString(): String | |
| 308 | * { | |
| 309 | * return "[TestEquatableObject " + _id + "]"; | |
| 310 | * } | |
| 311 | * } | |
| 312 | * } | |
| 313 | * </listing> | |
| 314 | * | |
| 315 | * <listing version="3.0"> | |
| 316 | * import test.TestEquatableObject; | |
| 317 | * | |
| 318 | * import org.as3collections.IList; | |
| 319 | * import org.as3collections.lists.ArrayList; | |
| 320 | * import org.as3collections.lists.UniqueList; | |
| 321 | * | |
| 322 | * var list1:ArrayList = new ArrayList(); | |
| 323 | * | |
| 324 | * list1 // [] | |
| 325 | * | |
| 326 | * var o1:TestEquatableObject = new TestEquatableObject("o1"); | |
| 327 | * var o2:TestEquatableObject = new TestEquatableObject("o2"); | |
| 328 | * var o3:TestEquatableObject = new TestEquatableObject("o3"); | |
| 329 | * var o4:TestEquatableObject = new TestEquatableObject("o4"); | |
| 330 | * | |
| 331 | * list1.contains(o1) // false | |
| 332 | * list1.add(o1) // true | |
| 333 | * list1 // [[TestEquatableObject o1]] | |
| 334 | * list1.contains(o1) // true | |
| 335 | * | |
| 336 | * var o5:TestEquatableObject = new TestIndexablePriority("o1"); // -> Attention to the id, which is "o1" | |
| 337 | * | |
| 338 | * list1.contains(o5) // true -> without equality would return false, because o1 and o5 are different objects. | |
| 339 | * | |
| 340 | * list1.add(o5) // true | |
| 341 | * list1 // [[TestEquatableObject o1],[TestEquatableObject o1]] | |
| 342 | * | |
| 343 | * o1.equals(o5) // true | |
| 344 | * o1.equals("abc") // false | |
| 345 | * | |
| 346 | * var list2:ArrayList = new ArrayList(); | |
| 347 | * | |
| 348 | * list2.equals(list1) // false | |
| 349 | * list2.add(o5) // true | |
| 350 | * list1 // [[TestEquatableObject o1],[TestEquatableObject o1]] | |
| 351 | * list2 // [[TestEquatableObject o1]] | |
| 352 | * list2.equals(list1) // false | |
| 353 | * | |
| 354 | * list2.add(o5) // true | |
| 355 | * list1 // [[TestEquatableObject o1],[TestEquatableObject o1]] | |
| 356 | * list2 // [[TestEquatableObject o1],[TestEquatableObject o1]] | |
| 357 | * list2.equals(list1) // true | |
| 358 | * | |
| 359 | * list2.remove(o1) // true -> equality used | |
| 360 | * list2 // [[TestEquatableObject o1]] | |
| 361 | * | |
| 362 | * var uniqueList:UniqueList = new UniqueList(new ArrayList()); | |
| 363 | * | |
| 364 | * uniqueList.contains(o1) // false | |
| 365 | * uniqueList.add(o1) // true | |
| 366 | * uniqueList // [[TestEquatableObject o1]] | |
| 367 | * uniqueList.contains(o1) // true | |
| 368 | * | |
| 369 | * uniqueList.add(o5) // false | |
| 370 | * uniqueList.contains(o5) // true -> by equality the object o5 is in the list because its 'id' is the same of the object o1. | |
| 371 | * uniqueList // [[TestEquatableObject o1]] | |
| 372 | * </listing> | |
| 373 | * | |
| 374 | * @see org.as3collections.AbstractList AbstractList | |
| 375 | * @see org.as3collections.lists.TypedList TypedList | |
| 376 | * @see org.as3collections.lists.UniqueList UniqueList | |
| 377 | * @see org.as3collections.lists.SortedArrayList SortedArrayList | |
| 378 | * @see org.as3collections.utils.ListUtil#getUniqueList() ListUtil.getUniqueList() | |
| 379 | * @see org.as3collections.utils.ListUtil#getTypedList() ListUtil.getTypedList() | |
| 380 | * @see org.as3collections.utils.ListUtil#getUniqueTypedList() ListUtil.getUniqueTypedList() | |
| 381 | * @author Flávio Silva | |
| 382 | */ | |
| 383 | public class ArrayList extends AbstractList | |
| 384 | { | |
| 385 | /** | |
| 386 | * Constructor, creates a new <code>ArrayList</code> object. | |
| 387 | * | |
| 388 | * @param source an array to fill the list. | |
| 389 | */ | |
| 390 | public function ArrayList(source:Array = null) | |
| 391 | { | |
| 392 | 1 | super(source); |
| 393 | 1 | } |
| 394 | ||
| 395 | /** | |
| 396 | * Inserts the specified element at the specified position in this list. | |
| 397 | * Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). | |
| 398 | * | |
| 399 | * @param index index at which the specified element is to be inserted. | |
| 400 | * @param element the element to be added. | |
| 401 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index > size())</code>. | |
| 402 | * @return <code>true</code> if this list changed as a result of the call. | |
| 403 | */ | |
| 404 | override public function addAt(index:int, element:*): Boolean | |
| 405 | { | |
| 406 | 1 | checkIndex(index, size()); |
| 407 | 1 | data.splice(index, 0, element); |
| 408 | 1 | elementAdded(element); |
| 409 | 1 | return true; |
| 410 | } | |
| 411 | ||
| 412 | /** | |
| 413 | * Removes all of the elements from this list. | |
| 414 | * The list will be empty after this method returns. | |
| 415 | */ | |
| 416 | override public function clear(): void | |
| 417 | { | |
| 418 | 1 | if (isEmpty()) return; |
| 419 | 1 | _modCount++; |
| 420 | 1 | data.splice(0); |
| 421 | 1 | _totalEquatable = 0; |
| 422 | 1 | } |
| 423 | ||
| 424 | /** | |
| 425 | * Creates and return a new <code>ArrayList</code> object containing all elements in this list (in the same order). | |
| 426 | * | |
| 427 | * @return a new <code>ArrayList</code> object containing all elements in this list (in the same order). | |
| 428 | */ | |
| 429 | override public function clone(): * | |
| 430 | { | |
| 431 | 1 | return new ArrayList(data); |
| 432 | } | |
| 433 | ||
| 434 | /** | |
| 435 | * Increases the capacity of this <code>ArrayList</code> instance, if necessary, to ensure that it can hold at least the number of elements specified by the <code>minCapacity</code> argument. | |
| 436 | * <p>This implementation uses <code>Array.length = minCapacity</code> of the internal array object.</p> | |
| 437 | */ | |
| 438 | public function ensureCapacity(minCapacity:int): void | |
| 439 | { | |
| 440 | 1 | if (minCapacity <= data.length) return; |
| 441 | 1 | _modCount++; |
| 442 | 1 | data.length = minCapacity; |
| 443 | 1 | } |
| 444 | ||
| 445 | /** | |
| 446 | * Returns an iterator over a set of elements. | |
| 447 | * <p>This implementation returns an <code>ArrayIterator</code> object.</p> | |
| 448 | * | |
| 449 | * @return an iterator over a set of elements. | |
| 450 | * @see org.as3collections.iterators.ArrayIterator ArrayIterator | |
| 451 | */ | |
| 452 | override public function iterator(): IIterator | |
| 453 | { | |
| 454 | 1 | return new ArrayIterator(data); |
| 455 | } | |
| 456 | ||
| 457 | /** | |
| 458 | * Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
| 459 | * The specified index indicates the first element that would be returned by an initial call to <code>next</code>. | |
| 460 | * An initial call to <code>previous</code> would return the element with the specified index minus one. | |
| 461 | * <p>This implementation returns an <code>ListIterator</code> object.</p> | |
| 462 | * | |
| 463 | * @param index index of first element to be returned from the list iterator (by a call to the <code>next</code> method) | |
| 464 | * @return a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
| 465 | * @see org.as3collections.iterators.ListIterator ListIterator | |
| 466 | */ | |
| 467 | override public function listIterator(index:int = 0): IListIterator | |
| 468 | { | |
| 469 | 1 | return new ListIterator(this, index); |
| 470 | } | |
| 471 | ||
| 472 | /** | |
| 473 | * Removes the element at the specified position in this list. | |
| 474 | * Shifts any subsequent elements to the left (subtracts one from their indices). | |
| 475 | * Returns the element that was removed from the list. | |
| 476 | * | |
| 477 | * @param index the index of the element to be removed. | |
| 478 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index >= size())</code>. | |
| 479 | * @return the element previously at the specified position. | |
| 480 | */ | |
| 481 | override public function removeAt(index:int): * | |
| 482 | { | |
| 483 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This list is empty."); |
| 484 | ||
| 485 | 1 | checkIndex(index, size() - 1); |
| 486 | ||
| 487 | 1 | var e:* = data.splice(index, 1)[0]; |
| 488 | 1 | elementRemoved(e); |
| 489 | ||
| 490 | 1 | return e; |
| 491 | } | |
| 492 | ||
| 493 | /** | |
| 494 | * Removes all of the elements whose index is between <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive. | |
| 495 | * Shifts any subsequent elements to the left (subtracts their indices). | |
| 496 | * <p>If <code>toIndex == fromIndex</code>, this operation has no effect.</p> | |
| 497 | * | |
| 498 | * @param fromIndex the index to start removing elements (inclusive). | |
| 499 | * @param toIndex the index to stop removing elements (exclusive). | |
| 500 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
| 501 | * @return a new <code>ArrayList</code> object containing all the removed elements. | |
| 502 | */ | |
| 503 | override public function removeRange(fromIndex:int, toIndex:int): ICollection | |
| 504 | { | |
| 505 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This list is empty."); |
| 506 | ||
| 507 | 1 | checkIndex(fromIndex, size()); |
| 508 | 1 | checkIndex(toIndex, size()); |
| 509 | ||
| 510 | 1 | var l:IList = new ArrayList(data.splice(fromIndex, toIndex - fromIndex)); |
| 511 | ||
| 512 | 1 | if (_totalEquatable < 1) |
| 513 | { | |
| 514 | 1 | _modCount += l.size(); |
| 515 | 1 | return l; |
| 516 | } | |
| 517 | ||
| 518 | 1 | var it:IIterator = l.iterator(); |
| 519 | var e:*; | |
| 520 | ||
| 521 | 1 | while(it.hasNext()) |
| 522 | { | |
| 523 | 1 | e = it.next(); |
| 524 | 1 | elementRemoved(e); |
| 525 | } | |
| 526 | ||
| 527 | 1 | return l; |
| 528 | } | |
| 529 | ||
| 530 | /** | |
| 531 | * Replaces the element at the specified position in this list with the specified element. | |
| 532 | * | |
| 533 | * @param index index of the element to replace. | |
| 534 | * @param element element to be stored at the specified position. | |
| 535 | * @throws org.as3collections.errors.IndexOutOfBoundsError if the index is out of range <code>(index < 0 || index >= size())</code>. | |
| 536 | * @return the element previously at the specified position. | |
| 537 | */ | |
| 538 | override public function setAt(index:int, element:*): * | |
| 539 | { | |
| 540 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This list is empty."); |
| 541 | 1 | checkIndex(index, size() - 1); |
| 542 | ||
| 543 | 1 | var old:* = data[index]; |
| 544 | 1 | data[index] = element; |
| 545 | ||
| 546 | 1 | elementRemoved(old); |
| 547 | 1 | elementAdded(element); |
| 548 | 1 | _modCount -= 2;// elementRemoved() and elementAdded() will increase modCount undesirably |
| 549 | ||
| 550 | 1 | return old; |
| 551 | } | |
| 552 | ||
| 553 | /** | |
| 554 | * Returns a new <code>ArrayList</code> that is a view of the portion of this <code>ArrayList</code> between the specified <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive. | |
| 555 | * This method uses native <code>Array.slice</code> method. | |
| 556 | * <p>Modifications in the returned <code>ArrayList</code> object doesn't affect this list.</p> | |
| 557 | * <p>This list is not modified.</p> | |
| 558 | * | |
| 559 | * @param fromIndex the index to start retrieving elements (inclusive). | |
| 560 | * @param toIndex the index to stop retrieving elements (exclusive). | |
| 561 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
| 562 | * @return a new <code>ArrayList</code> that is a view of the specified range within this list. | |
| 563 | */ | |
| 564 | override public function subList(fromIndex:int, toIndex:int): IList | |
| 565 | { | |
| 566 | 1 | if (isEmpty()) throw new IndexOutOfBoundsError("This list is empty."); |
| 567 | ||
| 568 | 1 | checkIndex(fromIndex, size()); |
| 569 | 1 | checkIndex(toIndex, size()); |
| 570 | ||
| 571 | 1 | if (fromIndex > toIndex) throw new ArgumentError("Argument <fromIndex> cannot be greater than argument <toIndex>. fromIndex: " + fromIndex + " | toIndex" + toIndex); |
| 572 | ||
| 573 | 1 | var l:IList = new ArrayList(data.slice(fromIndex, toIndex)); |
| 574 | ||
| 575 | 1 | return l; |
| 576 | } | |
| 577 | ||
| 578 | } | |
| 579 | ||
| 580 | } |