| 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 AbstractMap.from(Map<K,V> other) { | 16 AbstractMap.from(Map<K,V> other) { |
| 16 other.forEach((k,v) => this[k] = v); | 17 other.forEach((k,v) => this[k] = v); |
| 17 } | 18 } |
| 19 |
| 18 void operator []=(K key, value) { | 20 void operator []=(K key, value) { |
| 19 throw new UnsupportedOperationException('[]= is not supported'); | 21 throw new UnsupportedOperationException('[]= is not supported'); |
| 20 } | 22 } |
| 23 |
| 21 void clear() { | 24 void clear() { |
| 22 throw new UnsupportedOperationException('clear() is not supported'); | 25 throw new UnsupportedOperationException('clear() is not supported'); |
| 23 } | 26 } |
| 27 |
| 24 bool containsKey(K key) { | 28 bool containsKey(K key) { |
| 25 var found = false; | 29 var found = false; |
| 26 forEach((k,_) { | 30 forEach((k,_) { |
| 27 if (k == key) { | 31 if (k == key) { |
| 28 found = true; | 32 found = true; |
| 29 } | 33 } |
| 30 }); | 34 }); |
| 31 return found; | 35 return found; |
| 32 } | 36 } |
| 37 |
| 33 bool containsValue(V value) { | 38 bool containsValue(V value) { |
| 34 var found = false; | 39 var found = false; |
| 35 forEach((_,v) { | 40 forEach((_,v) { |
| 36 if (v == value) { | 41 if (v == value) { |
| 37 found = true; | 42 found = true; |
| 38 } | 43 } |
| 39 }); | 44 }); |
| 40 return found; | 45 return found; |
| 41 } | 46 } |
| 47 |
| 42 Collection<K> getKeys() { | 48 Collection<K> getKeys() { |
| 43 var keys = <K>[]; | 49 var keys = <K>[]; |
| 44 forEach((k,_) => keys.add(k)); | 50 forEach((k,_) => keys.add(k)); |
| 45 return keys; | 51 return keys; |
| 46 } | 52 } |
| 53 |
| 47 Collection<V> getValues() { | 54 Collection<V> getValues() { |
| 48 var values = <V>[]; | 55 var values = <V>[]; |
| 49 forEach((_,v) => values.add(v)); | 56 forEach((_,v) => values.add(v)); |
| 50 return values; | 57 return values; |
| 51 } | 58 } |
| 59 |
| 52 bool isEmpty() => length == 0; | 60 bool isEmpty() => length == 0; |
| 53 V putIfAbsent(K key, V ifAbsent()) { | 61 V putIfAbsent(K key, V ifAbsent()) { |
| 54 if (!containsKey(key)) { | 62 if (!containsKey(key)) { |
| 55 V value = this[key]; | 63 V value = this[key]; |
| 56 this[key] = ifAbsent(); | 64 this[key] = ifAbsent(); |
| 57 return value; | 65 return value; |
| 58 } | 66 } |
| 59 return null; | 67 return null; |
| 60 } | 68 } |
| 69 |
| 61 V remove(K key) { | 70 V remove(K key) { |
| 62 throw new UnsupportedOperationException('V remove(K key) is not supported'); | 71 throw new UnsupportedOperationException('V remove(K key) is not supported'); |
| 63 } | 72 } |
| 64 } | 73 } |
| 65 | 74 |
| 66 /** | 75 /** |
| 67 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all | 76 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all |
| 68 * mutating operations throw [UnsupportedOperationException] upon invocation. | 77 * mutating operations throw [UnsupportedOperationException] upon invocation. |
| 69 */ | 78 */ |
| 70 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { | 79 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 var count = 0; | 148 var count = 0; |
| 140 forEach((k,v) { | 149 forEach((k,v) { |
| 141 count++; | 150 count++; |
| 142 }); | 151 }); |
| 143 return count; | 152 return count; |
| 144 } | 153 } |
| 145 | 154 |
| 146 Vout operator [](K key) { | 155 Vout operator [](K key) { |
| 147 if (key is K) { | 156 if (key is K) { |
| 148 Vin value = _map[key]; | 157 Vin value = _map[key]; |
| 149 if (value !== null) { | 158 if (value != null) { |
| 150 return _filter(value); | 159 return _filter(value); |
| 151 } | 160 } |
| 152 } | 161 } |
| 153 return null; | 162 return null; |
| 154 } | 163 } |
| 155 | 164 |
| 156 void forEach(void f(K key, Vout value)) { | 165 void forEach(void f(K key, Vout value)) { |
| 157 _map.forEach((K k, Vin v) { | 166 _map.forEach((K k, Vin v) { |
| 158 var value = _filter(v); | 167 var value = _filter(v); |
| 159 if (value !== null) { | 168 if (value != null) { |
| 160 f(k, value); | 169 f(k, value); |
| 161 } | 170 } |
| 162 }); | 171 }); |
| 163 } | 172 } |
| 164 } | 173 } |
| OLD | NEW |