| 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 library util; | 5 library util; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An abstract map implementation. This class can be used as a superclass for | 8 * An abstract map implementation. This class can be used as a superclass for |
| 9 * implementing maps, requiring only the further implementation of the | 9 * implementing maps, requiring only the further implementation of the |
| 10 * [:operator []:], [:forEach:] and [:length:] methods to provide a fully | 10 * [:operator []:], [:forEach:] and [:length:] methods to provide a fully |
| 11 * implemented immutable map. | 11 * implemented immutable map. |
| 12 */ | 12 */ |
| 13 abstract class AbstractMap<K,V> implements Map<K,V> { | 13 abstract class AbstractMap<K, V> implements Map<K, V> { |
| 14 AbstractMap(); | 14 AbstractMap(); |
| 15 | 15 |
| 16 AbstractMap.from(Map<K,V> other) { | 16 AbstractMap.from(Map<K, V> other) { |
| 17 other.forEach((k,v) => this[k] = v); | 17 other.forEach((k,v) => this[k] = v); |
| 18 } | 18 } |
| 19 | 19 |
| 20 void operator []=(K key, value) { | 20 void operator []=(K key, value) { |
| 21 throw new UnsupportedError('[]= is not supported'); | 21 throw new UnsupportedError('[]= is not supported'); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void clear() { | 24 void clear() { |
| 25 throw new UnsupportedError('clear() is not supported'); | 25 throw new UnsupportedError('clear() is not supported'); |
| 26 } | 26 } |
| 27 |
| 28 void addAll(Map<K, V> other) { |
| 29 throw new UnsupportedError('addAll() is not supported'); |
| 30 } |
| 27 | 31 |
| 28 bool containsKey(K key) { | 32 bool containsKey(K key) { |
| 29 var found = false; | 33 var found = false; |
| 30 forEach((k,_) { | 34 forEach((k,_) { |
| 31 if (k == key) { | 35 if (k == key) { |
| 32 found = true; | 36 found = true; |
| 33 } | 37 } |
| 34 }); | 38 }); |
| 35 return found; | 39 return found; |
| 36 } | 40 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 74 |
| 71 V remove(K key) { | 75 V remove(K key) { |
| 72 throw new UnsupportedError('V remove(K key) is not supported'); | 76 throw new UnsupportedError('V remove(K key) is not supported'); |
| 73 } | 77 } |
| 74 } | 78 } |
| 75 | 79 |
| 76 /** | 80 /** |
| 77 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all | 81 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all |
| 78 * mutating operations throw [UnsupportedError] upon invocation. | 82 * mutating operations throw [UnsupportedError] upon invocation. |
| 79 */ | 83 */ |
| 80 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { | 84 class ImmutableMapWrapper<K, V> extends AbstractMap<K, V> { |
| 81 final Map<K,V> _map; | 85 final Map<K, V> _map; |
| 82 | 86 |
| 83 ImmutableMapWrapper(this._map); | 87 ImmutableMapWrapper(this._map); |
| 84 | 88 |
| 85 int get length => _map.length; | 89 int get length => _map.length; |
| 86 | 90 |
| 87 V operator [](K key) { | 91 V operator [](K key) { |
| 88 if (key is K) { | 92 if (key is K) { |
| 89 return _map[key]; | 93 return _map[key]; |
| 90 } | 94 } |
| 91 return null; | 95 return null; |
| 92 } | 96 } |
| 93 | 97 |
| 94 void forEach(void f(K key, V value)) { | 98 void forEach(void f(K key, V value)) { |
| 95 _map.forEach(f); | 99 _map.forEach(f); |
| 96 } | 100 } |
| 97 } | 101 } |
| 98 | 102 |
| 99 /** | 103 /** |
| 100 * A [Filter] function returns [:true:] iff [value] should be included. | 104 * A [Filter] function returns [:true:] iff [value] should be included. |
| 101 */ | 105 */ |
| 102 typedef bool Filter<V>(V value); | 106 typedef bool Filter<V>(V value); |
| 103 | 107 |
| 104 /** | 108 /** |
| 105 * An immutable map wrapper capable of filtering the input map. | 109 * An immutable map wrapper capable of filtering the input map. |
| 106 */ | 110 */ |
| 107 class FilteredImmutableMap<K,V> extends ImmutableMapWrapper<K,V> { | 111 class FilteredImmutableMap<K, V> extends ImmutableMapWrapper<K, V> { |
| 108 final Filter<V> _filter; | 112 final Filter<V> _filter; |
| 109 | 113 |
| 110 FilteredImmutableMap(Map<K,V> map, this._filter) : super(map); | 114 FilteredImmutableMap(Map<K, V> map, this._filter) : super(map); |
| 111 | 115 |
| 112 int get length { | 116 int get length { |
| 113 var count = 0; | 117 var count = 0; |
| 114 forEach((k,v) { | 118 forEach((k,v) { |
| 115 count++; | 119 count++; |
| 116 }); | 120 }); |
| 117 return count; | 121 return count; |
| 118 } | 122 } |
| 119 | 123 |
| 120 void forEach(void f(K key, V value)) { | 124 void forEach(void f(K key, V value)) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 169 |
| 166 void forEach(void f(K key, Vout value)) { | 170 void forEach(void f(K key, Vout value)) { |
| 167 _map.forEach((K k, Vin v) { | 171 _map.forEach((K k, Vin v) { |
| 168 var value = _filter(v); | 172 var value = _filter(v); |
| 169 if (value != null) { | 173 if (value != null) { |
| 170 f(k, value); | 174 f(k, value); |
| 171 } | 175 } |
| 172 }); | 176 }); |
| 173 } | 177 } |
| 174 } | 178 } |
| OLD | NEW |