| 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 dart2js.mirrors.util; | 5 library dart2js.mirrors.util; |
| 6 | 6 |
| 7 import 'dart:collection' show Maps; | 7 import 'dart:collection' show Maps; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * An abstract map implementation. This class can be used as a superclass for | 10 * An abstract map implementation. This class can be used as a superclass for |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 128 } |
| 129 | 129 |
| 130 void forEach(void f(K key, V value)) { | 130 void forEach(void f(K key, V value)) { |
| 131 _map.forEach((K k, V v) { | 131 _map.forEach((K k, V v) { |
| 132 if (_filter(v)) { | 132 if (_filter(v)) { |
| 133 f(k, v); | 133 f(k, v); |
| 134 } | 134 } |
| 135 }); | 135 }); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | |
| 139 /** | |
| 140 * An [AsFilter] takes a [value] of type [V1] and returns [value] iff it is of | |
| 141 * type [V2] or [:null:] otherwise. An [AsFilter] therefore behaves like the | |
| 142 * [:as:] expression. | |
| 143 */ | |
| 144 typedef V2 AsFilter<V1, V2>(V1 value); | |
| 145 | |
| 146 /** | |
| 147 * An immutable map wrapper capable of filtering the input map based on types. | |
| 148 * It takes an [AsFilter] function which converts the original values of type | |
| 149 * [Vin] into values of type [Vout], or returns [:null:] if the value should | |
| 150 * not be included in the filtered map. | |
| 151 */ | |
| 152 class AsFilteredImmutableMap<K, Vin, Vout> extends AbstractMap<K, Vout> { | |
| 153 final Map<K, Vin> _map; | |
| 154 final AsFilter<Vin, Vout> _filter; | |
| 155 | |
| 156 AsFilteredImmutableMap(this._map, this._filter); | |
| 157 | |
| 158 int get length { | |
| 159 var count = 0; | |
| 160 forEach((k,v) { | |
| 161 count++; | |
| 162 }); | |
| 163 return count; | |
| 164 } | |
| 165 | |
| 166 Vout operator [](K key) { | |
| 167 if (key is K) { | |
| 168 Vin value = _map[key]; | |
| 169 if (value != null) { | |
| 170 return _filter(value); | |
| 171 } | |
| 172 } | |
| 173 return null; | |
| 174 } | |
| 175 | |
| 176 void forEach(void f(K key, Vout value)) { | |
| 177 _map.forEach((K k, Vin v) { | |
| 178 var value = _filter(v); | |
| 179 if (value != null) { | |
| 180 f(k, value); | |
| 181 } | |
| 182 }); | |
| 183 } | |
| 184 } | |
| OLD | NEW |