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