| 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 UnsupportedOperationException('[]= is not supported'); | 21 throw new StateError('[]= is not supported'); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void clear() { | 24 void clear() { |
| 25 throw new UnsupportedOperationException('clear() is not supported'); | 25 throw new StateError('clear() is not supported'); |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool containsKey(K key) { | 28 bool containsKey(K key) { |
| 29 var found = false; | 29 var found = false; |
| 30 forEach((k,_) { | 30 forEach((k,_) { |
| 31 if (k == key) { | 31 if (k == key) { |
| 32 found = true; | 32 found = true; |
| 33 } | 33 } |
| 34 }); | 34 }); |
| 35 return found; | 35 return found; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 61 V putIfAbsent(K key, V ifAbsent()) { | 61 V putIfAbsent(K key, V ifAbsent()) { |
| 62 if (!containsKey(key)) { | 62 if (!containsKey(key)) { |
| 63 V value = this[key]; | 63 V value = this[key]; |
| 64 this[key] = ifAbsent(); | 64 this[key] = ifAbsent(); |
| 65 return value; | 65 return value; |
| 66 } | 66 } |
| 67 return null; | 67 return null; |
| 68 } | 68 } |
| 69 | 69 |
| 70 V remove(K key) { | 70 V remove(K key) { |
| 71 throw new UnsupportedOperationException('V remove(K key) is not supported'); | 71 throw new StateError('V remove(K key) is not supported'); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all | 76 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all |
| 77 * mutating operations throw [UnsupportedOperationException] upon invocation. | 77 * mutating operations throw [StateError] upon invocation. |
| 78 */ | 78 */ |
| 79 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { | 79 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { |
| 80 final Map<K,V> _map; | 80 final Map<K,V> _map; |
| 81 | 81 |
| 82 ImmutableMapWrapper(this._map); | 82 ImmutableMapWrapper(this._map); |
| 83 | 83 |
| 84 int get length => _map.length; | 84 int get length => _map.length; |
| 85 | 85 |
| 86 V operator [](K key) { | 86 V operator [](K key) { |
| 87 if (key is K) { | 87 if (key is K) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 164 |
| 165 void forEach(void f(K key, Vout value)) { | 165 void forEach(void f(K key, Vout value)) { |
| 166 _map.forEach((K k, Vin v) { | 166 _map.forEach((K k, Vin v) { |
| 167 var value = _filter(v); | 167 var value = _filter(v); |
| 168 if (value != null) { | 168 if (value != null) { |
| 169 f(k, value); | 169 f(k, value); |
| 170 } | 170 } |
| 171 }); | 171 }); |
| 172 } | 172 } |
| 173 } | 173 } |
| OLD | NEW |