OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import "dart:collection"; |
| 6 import "dart:math" as math; |
| 7 |
| 8 import "wrappers.dart"; |
| 9 |
| 10 typedef F _UnaryFunction<E, F>(E argument); |
| 11 |
| 12 /// The base class for delegating, type-asserting iterables. |
| 13 /// |
| 14 /// Subclasses can provide a [_base] that should be delegated to. Unlike |
| 15 /// [TypeSafeIterable], this allows the base to be created on demand. |
| 16 abstract class _TypeSafeIterableBase<E> implements Iterable<E> { |
| 17 /// The base iterable to which operations are delegated. |
| 18 Iterable get _base; |
| 19 |
| 20 _TypeSafeIterableBase(); |
| 21 |
| 22 bool any(bool test(E element)) => _base.any(_validate(test)); |
| 23 |
| 24 bool contains(Object element) => _base.contains(element); |
| 25 |
| 26 E elementAt(int index) => _base.elementAt(index) as E; |
| 27 |
| 28 bool every(bool test(E element)) => _base.every(_validate(test)); |
| 29 |
| 30 Iterable<T> expand<T>(Iterable<T> f(E element)) => _base.expand(_validate(f)); |
| 31 |
| 32 E get first => _base.first as E; |
| 33 |
| 34 E firstWhere(bool test(E element), {E orElse()}) => |
| 35 _base.firstWhere(_validate(test), orElse: orElse) as E; |
| 36 |
| 37 T fold<T>(T initialValue, T combine(T previousValue, E element)) => |
| 38 _base.fold(initialValue, |
| 39 (previousValue, element) => combine(previousValue, element as E)); |
| 40 |
| 41 void forEach(void f(E element)) => _base.forEach(_validate(f)); |
| 42 |
| 43 bool get isEmpty => _base.isEmpty; |
| 44 |
| 45 bool get isNotEmpty => _base.isNotEmpty; |
| 46 |
| 47 Iterator<E> get iterator => _base.map((element) => element as E).iterator; |
| 48 |
| 49 String join([String separator = ""]) => _base.join(separator); |
| 50 |
| 51 E get last => _base.last as E; |
| 52 |
| 53 E lastWhere(bool test(E element), {E orElse()}) => |
| 54 _base.lastWhere(_validate(test), orElse: orElse) as E; |
| 55 |
| 56 int get length => _base.length; |
| 57 |
| 58 Iterable<T> map<T>(T f(E element)) => _base.map(_validate(f)); |
| 59 |
| 60 E reduce(E combine(E value, E element)) => |
| 61 _base.reduce((value, element) => combine(value as E, element as E)) as E; |
| 62 |
| 63 E get single => _base.single as E; |
| 64 |
| 65 E singleWhere(bool test(E element)) => |
| 66 _base.singleWhere(_validate(test)) as E; |
| 67 |
| 68 Iterable<E> skip(int n) => new TypeSafeIterable<E>(_base.skip(n)); |
| 69 |
| 70 Iterable<E> skipWhile(bool test(E value)) => |
| 71 new TypeSafeIterable<E>(_base.skipWhile(_validate(test))); |
| 72 |
| 73 Iterable<E> take(int n) => new TypeSafeIterable<E>(_base.take(n)); |
| 74 |
| 75 Iterable<E> takeWhile(bool test(E value)) => |
| 76 new TypeSafeIterable<E>(_base.takeWhile(_validate(test))); |
| 77 |
| 78 List<E> toList({bool growable: true}) => |
| 79 new TypeSafeList<E>(_base.toList(growable: growable)); |
| 80 |
| 81 Set<E> toSet() => new TypeSafeSet<E>(_base.toSet()); |
| 82 |
| 83 Iterable<E> where(bool test(E element)) => |
| 84 new TypeSafeIterable<E>(_base.where(_validate(test))); |
| 85 |
| 86 String toString() => _base.toString(); |
| 87 |
| 88 /// Returns a version of [function] that asserts that its argument is an |
| 89 /// instance of `E`. |
| 90 _UnaryFunction<dynamic, F> _validate<F>(F function(E value)) => |
| 91 (value) => function(value as E); |
| 92 } |
| 93 |
| 94 /// An [Iterable] that asserts the types of values in a base iterable. |
| 95 /// |
| 96 /// This is instantiated using [DelegatingIterable.typed]. |
| 97 class TypeSafeIterable<E> extends _TypeSafeIterableBase<E> |
| 98 implements DelegatingIterable<E> { |
| 99 final Iterable _base; |
| 100 |
| 101 TypeSafeIterable(Iterable base) : _base = base; |
| 102 } |
| 103 |
| 104 /// A [List] that asserts the types of values in a base list. |
| 105 /// |
| 106 /// This is instantiated using [DelegatingList.typed]. |
| 107 class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> { |
| 108 TypeSafeList(List base) : super(base); |
| 109 |
| 110 /// A [List]-typed getter for [_base]. |
| 111 List get _listBase => _base; |
| 112 |
| 113 E operator [](int index) => _listBase[index] as E; |
| 114 |
| 115 void operator []=(int index, E value) { |
| 116 _listBase[index] = value; |
| 117 } |
| 118 |
| 119 void add(E value) { |
| 120 _listBase.add(value); |
| 121 } |
| 122 |
| 123 void addAll(Iterable<E> iterable) { |
| 124 _listBase.addAll(iterable); |
| 125 } |
| 126 |
| 127 Map<int, E> asMap() => new TypeSafeMap<int, E>(_listBase.asMap()); |
| 128 |
| 129 void clear() { |
| 130 _listBase.clear(); |
| 131 } |
| 132 |
| 133 void fillRange(int start, int end, [E fillValue]) { |
| 134 _listBase.fillRange(start, end, fillValue); |
| 135 } |
| 136 |
| 137 Iterable<E> getRange(int start, int end) => |
| 138 new TypeSafeIterable<E>(_listBase.getRange(start, end)); |
| 139 |
| 140 int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start); |
| 141 |
| 142 void insert(int index, E element) { |
| 143 _listBase.insert(index, element); |
| 144 } |
| 145 |
| 146 void insertAll(int index, Iterable<E> iterable) { |
| 147 _listBase.insertAll(index, iterable); |
| 148 } |
| 149 |
| 150 int lastIndexOf(E element, [int start]) => |
| 151 _listBase.lastIndexOf(element, start); |
| 152 |
| 153 void set length(int newLength) { |
| 154 _listBase.length = newLength; |
| 155 } |
| 156 |
| 157 bool remove(Object value) => _listBase.remove(value); |
| 158 |
| 159 E removeAt(int index) => _listBase.removeAt(index) as E; |
| 160 |
| 161 E removeLast() => _listBase.removeLast() as E; |
| 162 |
| 163 void removeRange(int start, int end) { |
| 164 _listBase.removeRange(start, end); |
| 165 } |
| 166 |
| 167 void removeWhere(bool test(E element)) { |
| 168 _listBase.removeWhere(_validate(test)); |
| 169 } |
| 170 |
| 171 void replaceRange(int start, int end, Iterable<E> iterable) { |
| 172 _listBase.replaceRange(start, end, iterable); |
| 173 } |
| 174 |
| 175 void retainWhere(bool test(E element)) { |
| 176 _listBase.retainWhere(_validate(test)); |
| 177 } |
| 178 |
| 179 Iterable<E> get reversed => new TypeSafeIterable<E>(_listBase.reversed); |
| 180 |
| 181 void setAll(int index, Iterable<E> iterable) { |
| 182 _listBase.setAll(index, iterable); |
| 183 } |
| 184 |
| 185 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| 186 _listBase.setRange(start, end, iterable, skipCount); |
| 187 } |
| 188 |
| 189 void shuffle([math.Random random]) { |
| 190 _listBase.shuffle(random); |
| 191 } |
| 192 |
| 193 void sort([int compare(E a, E b)]) { |
| 194 if (compare == null) { |
| 195 _listBase.sort(); |
| 196 } else { |
| 197 _listBase.sort((a, b) => compare(a as E, b as E)); |
| 198 } |
| 199 } |
| 200 |
| 201 List<E> sublist(int start, [int end]) => |
| 202 new TypeSafeList<E>(_listBase.sublist(start, end)); |
| 203 } |
| 204 |
| 205 /// A [Set] that asserts the types of values in a base set. |
| 206 /// |
| 207 /// This is instantiated using [DelegatingSet.typed]. |
| 208 class TypeSafeSet<E> extends TypeSafeIterable<E> implements DelegatingSet<E> { |
| 209 TypeSafeSet(Set base) : super(base); |
| 210 |
| 211 /// A [Set]-typed getter for [_base]. |
| 212 Set get _setBase => _base; |
| 213 |
| 214 bool add(E value) => _setBase.add(value); |
| 215 |
| 216 void addAll(Iterable<E> elements) { |
| 217 _setBase.addAll(elements); |
| 218 } |
| 219 |
| 220 void clear() { |
| 221 _setBase.clear(); |
| 222 } |
| 223 |
| 224 bool containsAll(Iterable<Object> other) => _setBase.containsAll(other); |
| 225 |
| 226 Set<E> difference(Set<Object> other) => |
| 227 new TypeSafeSet<E>(_setBase.difference(other)); |
| 228 |
| 229 Set<E> intersection(Set<Object> other) => |
| 230 new TypeSafeSet<E>(_setBase.intersection(other)); |
| 231 |
| 232 E lookup(Object element) => _setBase.lookup(element) as E; |
| 233 |
| 234 bool remove(Object value) => _setBase.remove(value); |
| 235 |
| 236 void removeAll(Iterable<Object> elements) { |
| 237 _setBase.removeAll(elements); |
| 238 } |
| 239 |
| 240 void removeWhere(bool test(E element)) { |
| 241 _setBase.removeWhere(_validate(test)); |
| 242 } |
| 243 |
| 244 void retainAll(Iterable<Object> elements) { |
| 245 _setBase.retainAll(elements); |
| 246 } |
| 247 |
| 248 void retainWhere(bool test(E element)) { |
| 249 _setBase.retainWhere(_validate(test)); |
| 250 } |
| 251 |
| 252 Set<E> union(Set<E> other) => new TypeSafeSet<E>(_setBase.union(other)); |
| 253 } |
| 254 |
| 255 /// A [Queue] that asserts the types of values in a base queue. |
| 256 /// |
| 257 /// This is instantiated using [DelegatingQueue.typed]. |
| 258 class TypeSafeQueue<E> extends TypeSafeIterable<E> |
| 259 implements DelegatingQueue<E> { |
| 260 TypeSafeQueue(Queue queue) : super(queue); |
| 261 |
| 262 /// A [Queue]-typed getter for [_base]. |
| 263 Queue get _baseQueue => _base; |
| 264 |
| 265 void add(E value) { |
| 266 _baseQueue.add(value); |
| 267 } |
| 268 |
| 269 void addAll(Iterable<E> iterable) { |
| 270 _baseQueue.addAll(iterable); |
| 271 } |
| 272 |
| 273 void addFirst(E value) { |
| 274 _baseQueue.addFirst(value); |
| 275 } |
| 276 |
| 277 void addLast(E value) { |
| 278 _baseQueue.addLast(value); |
| 279 } |
| 280 |
| 281 void clear() { |
| 282 _baseQueue.clear(); |
| 283 } |
| 284 |
| 285 bool remove(Object object) => _baseQueue.remove(object); |
| 286 |
| 287 void removeWhere(bool test(E element)) { |
| 288 _baseQueue.removeWhere(_validate(test)); |
| 289 } |
| 290 |
| 291 void retainWhere(bool test(E element)) { |
| 292 _baseQueue.retainWhere(_validate(test)); |
| 293 } |
| 294 |
| 295 E removeFirst() => _baseQueue.removeFirst() as E; |
| 296 |
| 297 E removeLast() => _baseQueue.removeLast() as E; |
| 298 } |
| 299 |
| 300 /// A [Map] that asserts the types of keys and values in a base map. |
| 301 /// |
| 302 /// This is instantiated using [DelegatingMap.typed]. |
| 303 class TypeSafeMap<K, V> implements DelegatingMap<K, V> { |
| 304 /// The base map to which operations are delegated. |
| 305 final Map _base; |
| 306 |
| 307 TypeSafeMap(Map base) : _base = base; |
| 308 |
| 309 V operator [](Object key) => _base[key] as V; |
| 310 |
| 311 void operator []=(K key, V value) { |
| 312 _base[key] = value; |
| 313 } |
| 314 |
| 315 void addAll(Map<K, V> other) { |
| 316 _base.addAll(other); |
| 317 } |
| 318 |
| 319 void clear() { |
| 320 _base.clear(); |
| 321 } |
| 322 |
| 323 bool containsKey(Object key) => _base.containsKey(key); |
| 324 |
| 325 bool containsValue(Object value) => _base.containsValue(value); |
| 326 |
| 327 void forEach(void f(K key, V value)) { |
| 328 _base.forEach((key, value) => f(key as K, value as V)); |
| 329 } |
| 330 |
| 331 bool get isEmpty => _base.isEmpty; |
| 332 |
| 333 bool get isNotEmpty => _base.isNotEmpty; |
| 334 |
| 335 Iterable<K> get keys => new TypeSafeIterable<K>(_base.keys); |
| 336 |
| 337 int get length => _base.length; |
| 338 |
| 339 V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent) as V; |
| 340 |
| 341 V remove(Object key) => _base.remove(key) as V; |
| 342 |
| 343 Iterable<V> get values => new TypeSafeIterable<V>(_base.values); |
| 344 |
| 345 String toString() => _base.toString(); |
| 346 } |
OLD | NEW |