| OLD | NEW |
| (Empty) | |
| 1 |
| 2 |
| 3 /** |
| 4 * Instances of the class `SingleMapIterator` implement an iterator that can be
used to access |
| 5 * the entries in a single map. |
| 6 */ |
| 7 class SingleMapIterator<K, V> implements MapIterator<K, V> { |
| 8 /** |
| 9 * Returns a new [SingleMapIterator] instance for the given [Map]. |
| 10 */ |
| 11 static SingleMapIterator forMap(Map map) => new SingleMapIterator(map); |
| 12 |
| 13 /** |
| 14 * The [Map] containing the entries to be iterated over. |
| 15 */ |
| 16 final Map<K, V> _map; |
| 17 |
| 18 /** |
| 19 * The iterator used to access the entries. |
| 20 */ |
| 21 Iterator<K> _keyIterator; |
| 22 |
| 23 /** |
| 24 * The current key, or `null` if there is no current key. |
| 25 */ |
| 26 K _currentKey; |
| 27 |
| 28 /** |
| 29 * The current value. |
| 30 */ |
| 31 V _currentValue; |
| 32 |
| 33 /** |
| 34 * Initialize a newly created iterator to return the entries from the given ma
p. |
| 35 * |
| 36 * @param map the map containing the entries to be iterated over |
| 37 */ |
| 38 SingleMapIterator(this._map) { |
| 39 this._keyIterator = _map.keys.iterator; |
| 40 } |
| 41 |
| 42 @override |
| 43 K get key { |
| 44 if (_currentKey == null) { |
| 45 throw new NoSuchElementException(); |
| 46 } |
| 47 return _currentKey; |
| 48 } |
| 49 |
| 50 @override |
| 51 V get value { |
| 52 if (_currentKey == null) { |
| 53 throw new NoSuchElementException(); |
| 54 } |
| 55 return _currentValue; |
| 56 } |
| 57 |
| 58 @override |
| 59 bool moveNext() { |
| 60 if (_keyIterator.moveNext()) { |
| 61 _currentKey = _keyIterator.current; |
| 62 _currentValue = _map[_currentKey]; |
| 63 return true; |
| 64 } else { |
| 65 _currentKey = null; |
| 66 return false; |
| 67 } |
| 68 } |
| 69 |
| 70 @override |
| 71 void set value(V newValue) { |
| 72 if (_currentKey == null) { |
| 73 throw new NoSuchElementException(); |
| 74 } |
| 75 _currentValue = newValue; |
| 76 _map[_currentKey] = newValue; |
| 77 } |
| 78 } |
| OLD | NEW |