| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Wrappers that prevent List, Set, or Map objects from being modified. | |
| 7 * | |
| 8 * The [Set] and [Map] wrappers allow reading from the wrapped collection, | |
| 9 * but prohibit writing. | |
| 10 * | |
| 11 * The [List] wrapper prevents changes to the length of the wrapped list, | |
| 12 * but allows changes to the contents. | |
| 13 */ | |
| 14 library unmodifiable_collection; | |
| 15 | |
| 16 import "dart:math" show Random; | |
| 17 export "dart:collection" show UnmodifiableListView; | |
| 18 | |
| 19 /** | |
| 20 * A fixed-length list. | |
| 21 * | |
| 22 * A NonGrowableListView contains a [List] object and ensures that | |
| 23 * its length does not change. | |
| 24 * Methods that would change the length of the list, | |
| 25 * such as [add] and [remove], throw an [UnsupportedError]. | |
| 26 * | |
| 27 * This class _does_ allow changes to the contents of the wrapped list. | |
| 28 * You can, for example, [sort] the list. | |
| 29 * Permitted operations defer to the wrapped list. | |
| 30 */ | |
| 31 class NonGrowableListView<E> extends _IterableView<E> | |
| 32 implements List<E> { | |
| 33 List<E> _source; | |
| 34 NonGrowableListView(List<E> source) : _source = source; | |
| 35 | |
| 36 static void _throw() { | |
| 37 throw new UnsupportedError( | |
| 38 "Cannot change the length of a fixed-length list"); | |
| 39 } | |
| 40 | |
| 41 int get length => _source.length; | |
| 42 | |
| 43 E operator [](int index) => _source[index]; | |
| 44 | |
| 45 int indexOf(E element, [int start = 0]) => _source.indexOf(element, start); | |
| 46 | |
| 47 int lastIndexOf(E element, [int start]) | |
| 48 => _source.lastIndexOf(element, start); | |
| 49 | |
| 50 Iterable<E> getRange(int start, int end) => _source.getRange(start, end); | |
| 51 | |
| 52 List<E> sublist(int start, [int end]) => _source.sublist(start, end); | |
| 53 | |
| 54 Iterable<E> get reversed => _source.reversed; | |
| 55 | |
| 56 Map<int, E> asMap() => _source.asMap(); | |
| 57 | |
| 58 void operator []=(int index, E value) { _source[index] = value; } | |
| 59 | |
| 60 void sort([int compare(E a, E b)]) { _source.sort(compare); } | |
| 61 | |
| 62 void shuffle([Random random]) { _source.shuffle(random); } | |
| 63 | |
| 64 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | |
| 65 _source.setRange(start, end, iterable, skipCount); | |
| 66 } | |
| 67 | |
| 68 void fillRange(int start, int end, [E fillValue]) { | |
| 69 _source.fillRange(start, end, fillValue); | |
| 70 } | |
| 71 | |
| 72 void setAll(int index, Iterable<E> iterable) { | |
| 73 _source.setAll(index, iterable); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 /** | |
| 78 * Throws an [UnsupportedError]; | |
| 79 * operations that change the length of the list are disallowed. | |
| 80 */ | |
| 81 void set length(int newLength) => _throw(); | |
| 82 | |
| 83 /** | |
| 84 * Throws an [UnsupportedError]; | |
| 85 * operations that change the length of the list are disallowed. | |
| 86 */ | |
| 87 bool add(E value) { | |
| 88 _throw(); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Throws an [UnsupportedError]; | |
| 93 * operations that change the length of the list are disallowed. | |
| 94 */ | |
| 95 void addAll(Iterable<E> iterable) => _throw(); | |
| 96 | |
| 97 /** | |
| 98 * Throws an [UnsupportedError]; | |
| 99 * operations that change the length of the list are disallowed. | |
| 100 */ | |
| 101 void insert(int index, E element) => _throw(); | |
| 102 | |
| 103 /** | |
| 104 * Throws an [UnsupportedError]; | |
| 105 * operations that change the length of the list are disallowed. | |
| 106 */ | |
| 107 void insertAll(int index, Iterable<E> iterable) => _throw(); | |
| 108 | |
| 109 /** | |
| 110 * Throws an [UnsupportedError]; | |
| 111 * operations that change the length of the list are disallowed. | |
| 112 */ | |
| 113 bool remove(Object value) { _throw(); } | |
| 114 | |
| 115 /** | |
| 116 * Throws an [UnsupportedError]; | |
| 117 * operations that change the length of the list are disallowed. | |
| 118 */ | |
| 119 E removeAt(int index) { _throw(); } | |
| 120 | |
| 121 /** | |
| 122 * Throws an [UnsupportedError]; | |
| 123 * operations that change the length of the list are disallowed. | |
| 124 */ | |
| 125 E removeLast() { _throw(); } | |
| 126 | |
| 127 /** | |
| 128 * Throws an [UnsupportedError]; | |
| 129 * operations that change the length of the list are disallowed. | |
| 130 */ | |
| 131 void removeWhere(bool test(E element)) => _throw(); | |
| 132 | |
| 133 /** | |
| 134 * Throws an [UnsupportedError]; | |
| 135 * operations that change the length of the list are disallowed. | |
| 136 */ | |
| 137 void retainWhere(bool test(E element)) => _throw(); | |
| 138 | |
| 139 /** | |
| 140 * Throws an [UnsupportedError]; | |
| 141 * operations that change the length of the list are disallowed. | |
| 142 */ | |
| 143 void removeRange(int start, int end) => _throw(); | |
| 144 | |
| 145 /** | |
| 146 * Throws an [UnsupportedError]; | |
| 147 * operations that change the length of the list are disallowed. | |
| 148 */ | |
| 149 void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); | |
| 150 | |
| 151 /** | |
| 152 * Throws an [UnsupportedError]; | |
| 153 * operations that change the length of the list are disallowed. | |
| 154 */ | |
| 155 void clear() => _throw(); | |
| 156 } | |
| 157 | |
| 158 /** | |
| 159 * An unmodifiable set. | |
| 160 * | |
| 161 * An UnmodifiableSetView contains a [Set] object and ensures | |
| 162 * that it does not change. | |
| 163 * Methods that would change the set, | |
| 164 * such as [add] and [remove], throw an [UnsupportedError]. | |
| 165 * Permitted operations defer to the wrapped set. | |
| 166 */ | |
| 167 class UnmodifiableSetView<E> extends _IterableView<E> | |
| 168 implements Set<E> { | |
| 169 Set<E> _source; | |
| 170 UnmodifiableSetView(Set<E> source) : _source = source; | |
| 171 | |
| 172 void _throw() { | |
| 173 throw new UnsupportedError("Cannot modify an unmodifiable Set"); | |
| 174 } | |
| 175 | |
| 176 bool containsAll(Iterable<E> other) => _source.containsAll(other); | |
| 177 | |
| 178 Set<E> intersection(Set<E> other) => _source.intersection(other); | |
| 179 | |
| 180 Set<E> union(Set<E> other) => _source.union(other); | |
| 181 | |
| 182 Set<E> difference(Set<E> other) => _source.difference(other); | |
| 183 | |
| 184 E lookup(Object object) => _source.lookup(object); | |
| 185 | |
| 186 /** | |
| 187 * Throws an [UnsupportedError]; | |
| 188 * operations that change the set are disallowed. | |
| 189 */ | |
| 190 bool add(E value) { | |
| 191 _throw(); | |
| 192 } | |
| 193 | |
| 194 /** | |
| 195 * Throws an [UnsupportedError]; | |
| 196 * operations that change the set are disallowed. | |
| 197 */ | |
| 198 void addAll(Iterable<E> elements) => _throw(); | |
| 199 | |
| 200 /** | |
| 201 * Throws an [UnsupportedError]; | |
| 202 * operations that change the set are disallowed. | |
| 203 */ | |
| 204 bool remove(Object value) { _throw(); } | |
| 205 | |
| 206 /** | |
| 207 * Throws an [UnsupportedError]; | |
| 208 * operations that change the set are disallowed. | |
| 209 */ | |
| 210 void removeAll(Iterable elements) => _throw(); | |
| 211 | |
| 212 /** | |
| 213 * Throws an [UnsupportedError]; | |
| 214 * operations that change the set are disallowed. | |
| 215 */ | |
| 216 void retainAll(Iterable elements) => _throw(); | |
| 217 | |
| 218 /** | |
| 219 * Throws an [UnsupportedError]; | |
| 220 * operations that change the set are disallowed. | |
| 221 */ | |
| 222 void removeWhere(bool test(E element)) => _throw(); | |
| 223 | |
| 224 /** | |
| 225 * Throws an [UnsupportedError]; | |
| 226 * operations that change the set are disallowed. | |
| 227 */ | |
| 228 void retainWhere(bool test(E element)) => _throw(); | |
| 229 | |
| 230 /** | |
| 231 * Throws an [UnsupportedError]; | |
| 232 * operations that change the set are disallowed. | |
| 233 */ | |
| 234 void clear() => _throw(); | |
| 235 } | |
| 236 | |
| 237 /** | |
| 238 * An unmodifiable map. | |
| 239 * | |
| 240 * An UnmodifiableMapView contains a [Map] object and ensures | |
| 241 * that it does not change. | |
| 242 * Methods that would change the map, | |
| 243 * such as [addAll] and [remove], throw an [UnsupportedError]. | |
| 244 * Permitted operations defer to the wrapped map. | |
| 245 */ | |
| 246 class UnmodifiableMapView<K, V> implements Map<K, V> { | |
| 247 Map<K, V> _source; | |
| 248 UnmodifiableMapView(Map<K, V> source) : _source = source; | |
| 249 | |
| 250 static void _throw() { | |
| 251 throw new UnsupportedError("Cannot modify an unmodifiable Map"); | |
| 252 } | |
| 253 | |
| 254 int get length => _source.length; | |
| 255 | |
| 256 bool get isEmpty => _source.isEmpty; | |
| 257 | |
| 258 bool get isNotEmpty => _source.isNotEmpty; | |
| 259 | |
| 260 V operator [](K key) => _source[key]; | |
| 261 | |
| 262 bool containsKey(K key) => _source.containsKey(key); | |
| 263 | |
| 264 bool containsValue(V value) => _source.containsValue(value); | |
| 265 | |
| 266 void forEach(void f(K key, V value)) => _source.forEach(f); | |
| 267 | |
| 268 Iterable<K> get keys => _source.keys; | |
| 269 | |
| 270 Iterable<V> get values => _source.values; | |
| 271 | |
| 272 | |
| 273 /** | |
| 274 * Throws an [UnsupportedError]; | |
| 275 * operations that change the map are disallowed. | |
| 276 */ | |
| 277 void operator []=(K key, V value) => _throw(); | |
| 278 | |
| 279 /** | |
| 280 * Throws an [UnsupportedError]; | |
| 281 * operations that change the map are disallowed. | |
| 282 */ | |
| 283 V putIfAbsent(K key, V ifAbsent()) { _throw(); } | |
| 284 | |
| 285 /** | |
| 286 * Throws an [UnsupportedError]; | |
| 287 * operations that change the map are disallowed. | |
| 288 */ | |
| 289 void addAll(Map<K, V> other) => _throw(); | |
| 290 | |
| 291 /** | |
| 292 * Throws an [UnsupportedError]; | |
| 293 * operations that change the map are disallowed. | |
| 294 */ | |
| 295 V remove(K key) { _throw(); } | |
| 296 | |
| 297 /** | |
| 298 * Throws an [UnsupportedError]; | |
| 299 * operations that change the map are disallowed. | |
| 300 */ | |
| 301 void clear() => _throw(); | |
| 302 } | |
| 303 | |
| 304 abstract class _IterableView<E> { | |
| 305 Iterable<E> get _source; | |
| 306 | |
| 307 bool any(bool test(E element)) => _source.any(test); | |
| 308 | |
| 309 bool contains(E element) => _source.contains(element); | |
| 310 | |
| 311 E elementAt(int index) => _source.elementAt(index); | |
| 312 | |
| 313 bool every(bool test(E element)) => _source.every(test); | |
| 314 | |
| 315 Iterable expand(Iterable f(E element)) => _source.expand(f); | |
| 316 | |
| 317 E get first => _source.first; | |
| 318 | |
| 319 E firstWhere(bool test(E element), { E orElse() }) | |
| 320 => _source.firstWhere(test, orElse: orElse); | |
| 321 | |
| 322 dynamic fold(var initialValue, | |
| 323 dynamic combine(var previousValue, E element)) | |
| 324 => _source.fold(initialValue, combine); | |
| 325 | |
| 326 void forEach(void f(E element)) => _source.forEach(f); | |
| 327 | |
| 328 bool get isEmpty => _source.isEmpty; | |
| 329 | |
| 330 bool get isNotEmpty => _source.isNotEmpty; | |
| 331 | |
| 332 Iterator<E> get iterator => _source.iterator; | |
| 333 | |
| 334 String join([String separator = ""]) => _source.join(separator); | |
| 335 | |
| 336 E get last => _source.last; | |
| 337 | |
| 338 E lastWhere(bool test(E element), {E orElse()}) | |
| 339 => _source.lastWhere(test, orElse: orElse); | |
| 340 | |
| 341 int get length => _source.length; | |
| 342 | |
| 343 Iterable map(f(E element)) => _source.map(f); | |
| 344 | |
| 345 E reduce(E combine(E value, E element)) => _source.reduce(combine); | |
| 346 | |
| 347 E get single => _source.single; | |
| 348 | |
| 349 E singleWhere(bool test(E element)) => _source.singleWhere(test); | |
| 350 | |
| 351 Iterable<E> skip(int n) => _source.skip(n); | |
| 352 | |
| 353 Iterable<E> skipWhile(bool test(E value)) => _source.skipWhile(test); | |
| 354 | |
| 355 Iterable<E> take(int n) => _source.take(n); | |
| 356 | |
| 357 Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test); | |
| 358 | |
| 359 List<E> toList({ bool growable: true }) => _source.toList(growable: growable); | |
| 360 | |
| 361 Set<E> toSet() => _source.toSet(); | |
| 362 | |
| 363 Iterable<E> where(bool test(E element)) => _source.where(test); | |
| 364 } | |
| OLD | NEW |