OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * The [Iterator] class provides methods to iterate over an object. It | 8 * The [Iterator] class provides methods to iterate over an object. It |
9 * is transparently used by the for-in construct to test for the end | 9 * is transparently used by the for-in construct to test for the end |
10 * of the iteration, and to get the elements. | 10 * of the iteration, and to get the elements. |
11 * | 11 * |
12 * If the object iterated over is changed during the iteration, the | 12 * If the object iterated over is changed during the iteration, the |
13 * behavior is unspecified. | 13 * behavior is unspecified. |
14 * | 14 * |
15 * The [Iterator] is initially positioned before the first element. Before | 15 * The [Iterator] is initially positioned before the first element. Before |
16 * accessing the first element the iterator must thus be advanced ([moveNext]) | 16 * accessing the first element the iterator must thus be advanced ([moveNext]) |
17 * to point to the first element. If there is no element left, then [moveNext] | 17 * to point to the first element. If there is no element left, then [moveNext] |
18 * returns false. | 18 * returns false. |
| 19 * |
| 20 * A typical usage of an [Iterator] looks as follows: |
| 21 * |
| 22 * var it = obj.iterator; |
| 23 * while (it.moveNext()) { |
| 24 * use(it.current); |
| 25 * } |
19 */ | 26 */ |
20 abstract class Iterator<E> { | 27 abstract class Iterator<E> { |
21 /** | 28 /** |
22 * Moves to the next element. Returns true if [current] contains the next | 29 * Moves to the next element. Returns true if [current] contains the next |
23 * element. Returns false, if no element was left. | 30 * element. Returns false, if no element was left. |
24 * | 31 * |
25 * It is safe to invoke [moveNext] even when the iterator is already | 32 * It is safe to invoke [moveNext] even when the iterator is already |
26 * positioned after the last element. In this case [moveNext] has no effect. | 33 * positioned after the last element. In this case [moveNext] has no effect. |
27 */ | 34 */ |
28 bool moveNext(); | 35 bool moveNext(); |
29 | 36 |
30 /** | 37 /** |
31 * Returns the current element. | 38 * Returns the current element. |
32 * | 39 * |
33 * Return [:null:] if the iterator has not yet been moved to the first | 40 * Return [:null:] if the iterator has not yet been moved to the first |
34 * element, or if the iterator has been moved after the last element of the | 41 * element, or if the iterator has been moved after the last element of the |
35 * [Iterable]. | 42 * [Iterable]. |
36 */ | 43 */ |
37 E get current; | 44 E get current; |
38 } | 45 } |
39 | |
40 class HasNextIterator<E> { | |
41 static const int _HAS_NEXT_AND_NEXT_IN_CURRENT = 0; | |
42 static const int _NO_NEXT = 1; | |
43 static const int _NOT_MOVED_YET = 2; | |
44 | |
45 Iterator _iterator; | |
46 int _state = _NOT_MOVED_YET; | |
47 | |
48 HasNextIterator(this._iterator); | |
49 | |
50 bool get hasNext { | |
51 if (_state == _NOT_MOVED_YET) _move(); | |
52 return _state == _HAS_NEXT_AND_NEXT_IN_CURRENT; | |
53 } | |
54 | |
55 E next() { | |
56 // Call to hasNext is necessary to make sure we are positioned at the first | |
57 // element when we start iterating. | |
58 if (!hasNext) throw new StateError("No more elements"); | |
59 assert(_state == _HAS_NEXT_AND_NEXT_IN_CURRENT); | |
60 E result = _iterator.current; | |
61 _move(); | |
62 return result; | |
63 } | |
64 | |
65 void _move() { | |
66 if (_iterator.moveNext()) { | |
67 _state = _HAS_NEXT_AND_NEXT_IN_CURRENT; | |
68 } else { | |
69 _state = _NO_NEXT; | |
70 } | |
71 } | |
72 } | |
OLD | NEW |