| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
| 6 * Unmodifiable wrappers for [List], [Set] and [Map] objects. | 6 * Wrappers that prevent List, Set, or Map objects from being modified. |
| 7 * | 7 * |
| 8 * The wrappers allow reading from the source list, but writing is prohibited. | 8 * The [Set] and [Map] wrappers allow reading from the wrapped collection, |
| 9 * but prohibit writing. |
| 9 * | 10 * |
| 10 * A non-growable list wrapper allows writing as well, but not changing the | 11 * The [List] wrapper prevents changes to the length of the wrapped list, |
| 11 * list's length. | 12 * but allows changes to the contents. |
| 12 */ | 13 */ |
| 13 library unmodifiable_collection; | 14 library unmodifiable_collection; |
| 14 | 15 |
| 15 export "dart:collection" show UnmodifiableListView; | 16 export "dart:collection" show UnmodifiableListView; |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * A [List] wrapper that acts as a non-growable list. | 19 * A fixed-length list. |
| 19 * | 20 * |
| 20 * Writes to the list are written through to the source list, but operations | 21 * A NonGrowableListView contains a [List] object and ensures that |
| 21 * that change the length is not allowed. | 22 * its length does not change. |
| 23 * Methods that would change the length of the list, |
| 24 * such as [add] and [remove], throw an [UnsupportedError]. |
| 25 * |
| 26 * This class _does_ allow changes to the contents of the wrapped list. |
| 27 * You can, for example, [sort] the list. |
| 28 * Permitted operations defer to the wrapped list. |
| 22 */ | 29 */ |
| 23 class NonGrowableListView<E> extends _IterableView<E> | 30 class NonGrowableListView<E> extends _IterableView<E> |
| 24 implements List<E> { | 31 implements List<E> { |
| 25 List<E> _source; | 32 List<E> _source; |
| 26 NonGrowableListView(List<E> source) : _source = source; | 33 NonGrowableListView(List<E> source) : _source = source; |
| 27 | 34 |
| 28 static void _throw() { | 35 static void _throw() { |
| 29 throw new UnsupportedError( | 36 throw new UnsupportedError( |
| 30 "Cannot change the length of a fixed-length list"); | 37 "Cannot change the length of a fixed-length list"); |
| 31 } | 38 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 58 | 65 |
| 59 void fillRange(int start, int end, [E fillValue]) { | 66 void fillRange(int start, int end, [E fillValue]) { |
| 60 _source.fillRange(start, end, fillValue); | 67 _source.fillRange(start, end, fillValue); |
| 61 } | 68 } |
| 62 | 69 |
| 63 void setAll(int index, Iterable<E> iterable) { | 70 void setAll(int index, Iterable<E> iterable) { |
| 64 _source.setAll(index, iterable); | 71 _source.setAll(index, iterable); |
| 65 } | 72 } |
| 66 | 73 |
| 67 | 74 |
| 75 /** |
| 76 * Throws an [UnsupportedError]; |
| 77 * operations that change the length of the list are disallowed. |
| 78 */ |
| 68 void set length(int newLength) => _throw(); | 79 void set length(int newLength) => _throw(); |
| 69 | 80 |
| 81 /** |
| 82 * Throws an [UnsupportedError]; |
| 83 * operations that change the length of the list are disallowed. |
| 84 */ |
| 70 void add(E value) => _throw(); | 85 void add(E value) => _throw(); |
| 71 | 86 |
| 87 /** |
| 88 * Throws an [UnsupportedError]; |
| 89 * operations that change the length of the list are disallowed. |
| 90 */ |
| 72 void addAll(Iterable<E> iterable) => _throw(); | 91 void addAll(Iterable<E> iterable) => _throw(); |
| 73 | 92 |
| 93 /** |
| 94 * Throws an [UnsupportedError]; |
| 95 * operations that change the length of the list are disallowed. |
| 96 */ |
| 74 void insert(int index, E element) => _throw(); | 97 void insert(int index, E element) => _throw(); |
| 75 | 98 |
| 99 /** |
| 100 * Throws an [UnsupportedError]; |
| 101 * operations that change the length of the list are disallowed. |
| 102 */ |
| 76 void insertAll(int index, Iterable<E> iterable) => _throw(); | 103 void insertAll(int index, Iterable<E> iterable) => _throw(); |
| 77 | 104 |
| 105 /** |
| 106 * Throws an [UnsupportedError]; |
| 107 * operations that change the length of the list are disallowed. |
| 108 */ |
| 78 bool remove(Object value) { _throw(); } | 109 bool remove(Object value) { _throw(); } |
| 79 | 110 |
| 111 /** |
| 112 * Throws an [UnsupportedError]; |
| 113 * operations that change the length of the list are disallowed. |
| 114 */ |
| 80 E removeAt(int index) { _throw(); } | 115 E removeAt(int index) { _throw(); } |
| 81 | 116 |
| 117 /** |
| 118 * Throws an [UnsupportedError]; |
| 119 * operations that change the length of the list are disallowed. |
| 120 */ |
| 82 E removeLast() { _throw(); } | 121 E removeLast() { _throw(); } |
| 83 | 122 |
| 123 /** |
| 124 * Throws an [UnsupportedError]; |
| 125 * operations that change the length of the list are disallowed. |
| 126 */ |
| 84 void removeWhere(bool test(E element)) => _throw(); | 127 void removeWhere(bool test(E element)) => _throw(); |
| 85 | 128 |
| 129 /** |
| 130 * Throws an [UnsupportedError]; |
| 131 * operations that change the length of the list are disallowed. |
| 132 */ |
| 86 void retainWhere(bool test(E element)) => _throw(); | 133 void retainWhere(bool test(E element)) => _throw(); |
| 87 | 134 |
| 135 /** |
| 136 * Throws an [UnsupportedError]; |
| 137 * operations that change the length of the list are disallowed. |
| 138 */ |
| 88 void removeRange(int start, int end) => _throw(); | 139 void removeRange(int start, int end) => _throw(); |
| 89 | 140 |
| 141 /** |
| 142 * Throws an [UnsupportedError]; |
| 143 * operations that change the length of the list are disallowed. |
| 144 */ |
| 90 void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); | 145 void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); |
| 91 | 146 |
| 147 /** |
| 148 * Throws an [UnsupportedError]; |
| 149 * operations that change the length of the list are disallowed. |
| 150 */ |
| 92 void clear() => _throw(); | 151 void clear() => _throw(); |
| 93 } | 152 } |
| 94 | 153 |
| 95 /** | 154 /** |
| 96 * A [Set] wrapper that acts as an unmodifiable set. | 155 * An unmodifiable set. |
| 156 * |
| 157 * An UnmodifiableSetView contains a [Set] object and ensures |
| 158 * that it does not change. |
| 159 * Methods that would change the set, |
| 160 * such as [add] and [remove], throw an [UnsupportedError]. |
| 161 * Permitted operations defer to the wrapped set. |
| 97 */ | 162 */ |
| 98 class UnmodifiableSetView<E> extends _IterableView<E> | 163 class UnmodifiableSetView<E> extends _IterableView<E> |
| 99 implements Set<E> { | 164 implements Set<E> { |
| 100 Set<E> _source; | 165 Set<E> _source; |
| 101 UnmodifiableSetView(Set<E> source) : _source = source; | 166 UnmodifiableSetView(Set<E> source) : _source = source; |
| 102 | 167 |
| 103 void _throw() { | 168 void _throw() { |
| 104 throw new UnsupportedError("Cannot modify an unmodifiable Set"); | 169 throw new UnsupportedError("Cannot modify an unmodifiable Set"); |
| 105 } | 170 } |
| 106 | 171 |
| 107 bool containsAll(Iterable<E> other) => _source.containsAll(other); | 172 bool containsAll(Iterable<E> other) => _source.containsAll(other); |
| 108 | 173 |
| 109 Set<E> intersection(Set<E> other) => _source.intersection(other); | 174 Set<E> intersection(Set<E> other) => _source.intersection(other); |
| 110 | 175 |
| 111 Set<E> union(Set<E> other) => _source.union(other); | 176 Set<E> union(Set<E> other) => _source.union(other); |
| 112 | 177 |
| 113 Set<E> difference(Set<E> other) => _source.difference(other); | 178 Set<E> difference(Set<E> other) => _source.difference(other); |
| 114 | 179 |
| 115 | 180 |
| 181 /** |
| 182 * Throws an [UnsupportedError]; |
| 183 * operations that change the set are disallowed. |
| 184 */ |
| 116 void add(E value) => _throw(); | 185 void add(E value) => _throw(); |
| 117 | 186 |
| 187 /** |
| 188 * Throws an [UnsupportedError]; |
| 189 * operations that change the set are disallowed. |
| 190 */ |
| 118 void addAll(Iterable<E> elements) => _throw(); | 191 void addAll(Iterable<E> elements) => _throw(); |
| 119 | 192 |
| 193 /** |
| 194 * Throws an [UnsupportedError]; |
| 195 * operations that change the set are disallowed. |
| 196 */ |
| 120 bool remove(Object value) { _throw(); } | 197 bool remove(Object value) { _throw(); } |
| 121 | 198 |
| 199 /** |
| 200 * Throws an [UnsupportedError]; |
| 201 * operations that change the set are disallowed. |
| 202 */ |
| 122 void removeAll(Iterable elements) => _throw(); | 203 void removeAll(Iterable elements) => _throw(); |
| 123 | 204 |
| 205 /** |
| 206 * Throws an [UnsupportedError]; |
| 207 * operations that change the set are disallowed. |
| 208 */ |
| 124 void retainAll(Iterable elements) => _throw(); | 209 void retainAll(Iterable elements) => _throw(); |
| 125 | 210 |
| 211 /** |
| 212 * Throws an [UnsupportedError]; |
| 213 * operations that change the set are disallowed. |
| 214 */ |
| 126 void removeWhere(bool test(E element)) => _throw(); | 215 void removeWhere(bool test(E element)) => _throw(); |
| 127 | 216 |
| 217 /** |
| 218 * Throws an [UnsupportedError]; |
| 219 * operations that change the set are disallowed. |
| 220 */ |
| 128 void retainWhere(bool test(E element)) => _throw(); | 221 void retainWhere(bool test(E element)) => _throw(); |
| 129 | 222 |
| 223 /** |
| 224 * Throws an [UnsupportedError]; |
| 225 * operations that change the set are disallowed. |
| 226 */ |
| 130 void clear() => _throw(); | 227 void clear() => _throw(); |
| 131 } | 228 } |
| 132 | 229 |
| 133 /** | 230 /** |
| 134 * A [Map] wrapper that acts as an unmodifiable map. | 231 * An unmodifiable map. |
| 232 * |
| 233 * An UnmodifiableMapView contains a [Map] object and ensures |
| 234 * that it does not change. |
| 235 * Methods that would change the map, |
| 236 * such as [addAll] and [remove], throw an [UnsupportedError]. |
| 237 * Permitted operations defer to the wrapped map. |
| 135 */ | 238 */ |
| 136 class UnmodifiableMapView<K, V> implements Map<K, V> { | 239 class UnmodifiableMapView<K, V> implements Map<K, V> { |
| 137 Map<K, V> _source; | 240 Map<K, V> _source; |
| 138 UnmodifiableMapView(Map<K, V> source) : _source = source; | 241 UnmodifiableMapView(Map<K, V> source) : _source = source; |
| 139 | 242 |
| 140 static void _throw() { | 243 static void _throw() { |
| 141 throw new UnsupportedError("Cannot modify an unmodifiable Map"); | 244 throw new UnsupportedError("Cannot modify an unmodifiable Map"); |
| 142 } | 245 } |
| 143 | 246 |
| 144 int get length => _source.length; | 247 int get length => _source.length; |
| 145 | 248 |
| 146 bool get isEmpty => _source.isEmpty; | 249 bool get isEmpty => _source.isEmpty; |
| 147 | 250 |
| 148 bool get isNotEmpty => _source.isNotEmpty; | 251 bool get isNotEmpty => _source.isNotEmpty; |
| 149 | 252 |
| 150 V operator [](K key) => _source[key]; | 253 V operator [](K key) => _source[key]; |
| 151 | 254 |
| 152 bool containsKey(K key) => _source.containsKey(key); | 255 bool containsKey(K key) => _source.containsKey(key); |
| 153 | 256 |
| 154 bool containsValue(V value) => _source.containsValue(value); | 257 bool containsValue(V value) => _source.containsValue(value); |
| 155 | 258 |
| 156 void forEach(void f(K key, V value)) => _source.forEach(f); | 259 void forEach(void f(K key, V value)) => _source.forEach(f); |
| 157 | 260 |
| 158 Iterable<K> get keys => _source.keys; | 261 Iterable<K> get keys => _source.keys; |
| 159 | 262 |
| 160 Iterable<V> get values => _source.values; | 263 Iterable<V> get values => _source.values; |
| 161 | 264 |
| 162 | 265 |
| 266 /** |
| 267 * Throws an [UnsupportedError]; |
| 268 * operations that change the map are disallowed. |
| 269 */ |
| 163 void operator []=(K key, V value) => _throw(); | 270 void operator []=(K key, V value) => _throw(); |
| 164 | 271 |
| 272 /** |
| 273 * Throws an [UnsupportedError]; |
| 274 * operations that change the map are disallowed. |
| 275 */ |
| 165 V putIfAbsent(K key, V ifAbsent()) { _throw(); } | 276 V putIfAbsent(K key, V ifAbsent()) { _throw(); } |
| 166 | 277 |
| 278 /** |
| 279 * Throws an [UnsupportedError]; |
| 280 * operations that change the map are disallowed. |
| 281 */ |
| 167 void addAll(Map<K, V> other) => _throw(); | 282 void addAll(Map<K, V> other) => _throw(); |
| 168 | 283 |
| 284 /** |
| 285 * Throws an [UnsupportedError]; |
| 286 * operations that change the map are disallowed. |
| 287 */ |
| 169 V remove(K key) { _throw(); } | 288 V remove(K key) { _throw(); } |
| 170 | 289 |
| 290 /** |
| 291 * Throws an [UnsupportedError]; |
| 292 * operations that change the map are disallowed. |
| 293 */ |
| 171 void clear() => _throw(); | 294 void clear() => _throw(); |
| 172 } | 295 } |
| 173 | 296 |
| 174 abstract class _IterableView<E> { | 297 abstract class _IterableView<E> { |
| 175 Iterable<E> get _source; | 298 Iterable<E> get _source; |
| 176 | 299 |
| 177 bool any(bool test(E element)) => _source.any(test); | 300 bool any(bool test(E element)) => _source.any(test); |
| 178 | 301 |
| 179 bool contains(E element) => _source.contains(element); | 302 bool contains(E element) => _source.contains(element); |
| 180 | 303 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 Iterable<E> take(int n) => _source.take(n); | 348 Iterable<E> take(int n) => _source.take(n); |
| 226 | 349 |
| 227 Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test); | 350 Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test); |
| 228 | 351 |
| 229 List<E> toList({ bool growable: true }) => _source.toList(growable: growable); | 352 List<E> toList({ bool growable: true }) => _source.toList(growable: growable); |
| 230 | 353 |
| 231 Set<E> toSet() => _source.toSet(); | 354 Set<E> toSet() => _source.toSet(); |
| 232 | 355 |
| 233 Iterable<E> where(bool test(E element)) => _source.where(test); | 356 Iterable<E> where(bool test(E element)) => _source.where(test); |
| 234 } | 357 } |
| OLD | NEW |