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 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 /** | 66 /** |
67 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all | 67 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all |
68 * mutating operations throw [UnsupportedOperationException] upon invocation. | 68 * mutating operations throw [UnsupportedOperationException] upon invocation. |
69 */ | 69 */ |
70 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { | 70 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> { |
71 final Map<K,V> _map; | 71 final Map<K,V> _map; |
72 | 72 |
73 ImmutableMapWrapper(this._map); | 73 ImmutableMapWrapper(this._map); |
74 | 74 |
75 int get length() => _map.length; | 75 int get length => _map.length; |
76 | 76 |
77 V operator [](K key) { | 77 V operator [](K key) { |
78 if (key is K) { | 78 if (key is K) { |
79 return _map[key]; | 79 return _map[key]; |
80 } | 80 } |
81 return null; | 81 return null; |
82 } | 82 } |
83 | 83 |
84 void forEach(void f(K key, V value)) { | 84 void forEach(void f(K key, V value)) { |
85 _map.forEach(f); | 85 _map.forEach(f); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 /** | 89 /** |
90 * A [Filter] function returns [:true:] iff [value] should be included. | 90 * A [Filter] function returns [:true:] iff [value] should be included. |
91 */ | 91 */ |
92 typedef bool Filter<V>(V value); | 92 typedef bool Filter<V>(V value); |
93 | 93 |
94 /** | 94 /** |
95 * An immutable map wrapper capable of filtering the input map. | 95 * An immutable map wrapper capable of filtering the input map. |
96 */ | 96 */ |
97 class FilteredImmutableMap<K,V> extends ImmutableMapWrapper<K,V> { | 97 class FilteredImmutableMap<K,V> extends ImmutableMapWrapper<K,V> { |
98 final Filter<V> _filter; | 98 final Filter<V> _filter; |
99 | 99 |
100 FilteredImmutableMap(Map<K,V> map, this._filter) : super(map); | 100 FilteredImmutableMap(Map<K,V> map, this._filter) : super(map); |
101 | 101 |
102 int get length() { | 102 int get length { |
103 var count = 0; | 103 var count = 0; |
104 forEach((k,v) { | 104 forEach((k,v) { |
105 count++; | 105 count++; |
106 }); | 106 }); |
107 return count; | 107 return count; |
108 } | 108 } |
109 | 109 |
110 void forEach(void f(K key, V value)) { | 110 void forEach(void f(K key, V value)) { |
111 _map.forEach((K k, V v) { | 111 _map.forEach((K k, V v) { |
112 if (_filter(v)) { | 112 if (_filter(v)) { |
(...skipping 15 matching lines...) Expand all Loading... |
128 * It takes an [AsFilter] function which converts the original values of type | 128 * It takes an [AsFilter] function which converts the original values of type |
129 * [Vin] into values of type [Vout], or returns [:null:] if the value should | 129 * [Vin] into values of type [Vout], or returns [:null:] if the value should |
130 * not be included in the filtered map. | 130 * not be included in the filtered map. |
131 */ | 131 */ |
132 class AsFilteredImmutableMap<K, Vin, Vout> extends AbstractMap<K, Vout> { | 132 class AsFilteredImmutableMap<K, Vin, Vout> extends AbstractMap<K, Vout> { |
133 final Map<K, Vin> _map; | 133 final Map<K, Vin> _map; |
134 final AsFilter<Vin, Vout> _filter; | 134 final AsFilter<Vin, Vout> _filter; |
135 | 135 |
136 AsFilteredImmutableMap(this._map, this._filter); | 136 AsFilteredImmutableMap(this._map, this._filter); |
137 | 137 |
138 int get length() { | 138 int get length { |
139 var count = 0; | 139 var count = 0; |
140 forEach((k,v) { | 140 forEach((k,v) { |
141 count++; | 141 count++; |
142 }); | 142 }); |
143 return count; | 143 return count; |
144 } | 144 } |
145 | 145 |
146 Vout operator [](K key) { | 146 Vout operator [](K key) { |
147 if (key is K) { | 147 if (key is K) { |
148 return _filter(_map[key]); | 148 return _filter(_map[key]); |
149 } | 149 } |
150 return null; | 150 return null; |
151 } | 151 } |
152 | 152 |
153 void forEach(void f(K key, Vout value)) { | 153 void forEach(void f(K key, Vout value)) { |
154 _map.forEach((K k, Vin v) { | 154 _map.forEach((K k, Vin v) { |
155 var value = _filter(v); | 155 var value = _filter(v); |
156 if (value !== null) { | 156 if (value !== null) { |
157 f(k, value); | 157 f(k, value); |
158 } | 158 } |
159 }); | 159 }); |
160 } | 160 } |
161 } | 161 } |
OLD | NEW |