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