| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayIterator |
|
| 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.iterators |
| 31 | { | |
| 32 | import org.as3collections.IIterator; | |
| 33 | import org.as3collections.errors.NoSuchElementError; | |
| 34 | import org.as3coreaddendum.errors.IllegalStateError; | |
| 35 | ||
| 36 | /** | |
| 37 | * An iterator to iterate over an <code>Array</code> object. | |
| 38 | * | |
| 39 | * @example | |
| 40 | * | |
| 41 | * <listing version="3.0"> | |
| 42 | * import org.as3collections.IIterator; | |
| 43 | * import org.as3collections.IList; | |
| 44 | * import org.as3collections.lists.ArrayList; | |
| 45 | * | |
| 46 | * var list1:IList = new ArrayList([1, 3, 5, 7]); | |
| 47 | * | |
| 48 | * list1 // [1,3,5,7] | |
| 49 | * | |
| 50 | * var it:IIterator = list1.iterator(); | |
| 51 | * var e:int; | |
| 52 | * | |
| 53 | * while (it.hasNext()) | |
| 54 | * { | |
| 55 | * ITERATION N.1 | |
| 56 | * | |
| 57 | * it.pointer() // -1 | |
| 58 | * | |
| 59 | * e = it.next(); | |
| 60 | * e // 1 | |
| 61 | * | |
| 62 | * it.pointer() // 0 | |
| 63 | * | |
| 64 | * ITERATION N.2 | |
| 65 | * | |
| 66 | * it.pointer() // 0 | |
| 67 | * | |
| 68 | * e = it.next(); | |
| 69 | * e // 3 | |
| 70 | * | |
| 71 | * it.pointer() // 1 | |
| 72 | * | |
| 73 | * if (e == 3) | |
| 74 | * { | |
| 75 | * it.remove(); | |
| 76 | * list1 // [1,5,7] | |
| 77 | * } | |
| 78 | * | |
| 79 | * ITERATION N.3 | |
| 80 | * | |
| 81 | * it.pointer() // 0 | |
| 82 | * | |
| 83 | * e = it.next(); | |
| 84 | * e // 5 | |
| 85 | * | |
| 86 | * it.pointer() // 1 | |
| 87 | * | |
| 88 | * ITERATION N.4 | |
| 89 | * | |
| 90 | * it.pointer() // 1 | |
| 91 | * | |
| 92 | * e = it.next(); | |
| 93 | * e // 7 | |
| 94 | * | |
| 95 | * it.pointer() // 2 | |
| 96 | * } | |
| 97 | * </listing> | |
| 98 | * | |
| 99 | * @author Flávio Silva | |
| 100 | */ | |
| 101 | public class ArrayIterator implements IIterator | |
| 102 | { | |
| 103 | private var _allowRemove: Boolean; | |
| 104 | private var _pointer: int = -1; | |
| 105 | private var _source: Array; | |
| 106 | ||
| 107 | /** | |
| 108 | * Constructor, creates a new ArrayIterator object. | |
| 109 | * | |
| 110 | * @param source the source array to iterate over. | |
| 111 | * @throws ArgumentError if the <code>source</code> argument is <code>null</code>. | |
| 112 | */ | |
| 113 | public function ArrayIterator(source:Array) | |
| 114 | 1 | { |
| 115 | 1 | if (!source) throw new ArgumentError("The 'source' argument must not be 'null'."); |
| 116 | 1 | _source = source; |
| 117 | 1 | } |
| 118 | ||
| 119 | /** | |
| 120 | * @inheritDoc | |
| 121 | */ | |
| 122 | public function hasNext(): Boolean | |
| 123 | { | |
| 124 | 1 | return _pointer < _source.length - 1; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * @inheritDoc | |
| 129 | * @throws org.as3collections.errors.NoSuchElementError if the iteration has no more elements. | |
| 130 | */ | |
| 131 | public function next(): * | |
| 132 | { | |
| 133 | 1 | if (!hasNext()) throw new NoSuchElementError("Iterator has no next element. Call hasNext() method before."); |
| 134 | 1 | _allowRemove = true; |
| 135 | 1 | return _source[++_pointer]; |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * @inheritDoc | |
| 140 | */ | |
| 141 | public function pointer(): * | |
| 142 | { | |
| 143 | 1 | return _pointer; |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * @inheritDoc | |
| 148 | * @throws org.as3coreaddendum.errors.IllegalStateError if the <code>next</code> method has not yet been called, or the <code>remove</code> method has already been called after the last call to the <code>next</code> method. | |
| 149 | */ | |
| 150 | public function remove(): void | |
| 151 | { | |
| 152 | 1 | if (!_allowRemove) throw new IllegalStateError("The next method has not yet been called or the remove method has already been called after the last call to the next method."); |
| 153 | 1 | _allowRemove = false; |
| 154 | 1 | _source.splice(_pointer--, 1); |
| 155 | 1 | } |
| 156 | ||
| 157 | /** | |
| 158 | * @inheritDoc | |
| 159 | */ | |
| 160 | public function reset(): void | |
| 161 | { | |
| 162 | 1 | _pointer = -1; |
| 163 | 1 | } |
| 164 | ||
| 165 | } | |
| 166 | ||
| 167 | } |