Chromium Code Reviews| Index: lib/src/typed_wrappers.dart |
| diff --git a/lib/src/typed_wrappers.dart b/lib/src/typed_wrappers.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..47462a113f958f306fc7a3a45aa0b3c6954dfe59 |
| --- /dev/null |
| +++ b/lib/src/typed_wrappers.dart |
| @@ -0,0 +1,337 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "dart:collection"; |
| +import "dart:math" as math; |
| + |
| +import "wrappers.dart"; |
| + |
| +typedef F _UnaryFunction<E, F>(E argument); |
| + |
| +/// The base class for delegating, type-asserting iterables. |
| +/// |
| +/// Subclasses can provide a [_base] that should be delegated to. Unlike |
| +/// [TypedIterable], this allows the base to be created on demand. |
| +abstract class _TypedIterableBase<E> implements Iterable<E> { |
| + /// The base iterable to which operations are delegated. |
| + Iterable get _base; |
| + |
| + const _TypedIterableBase(); |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Don't make a const constructor unless you intend t
nweiz
2016/03/29 20:18:05
Done.
|
| + |
| + bool any(bool test(E element)) => _base.any(_assert(test)); |
| + |
| + bool contains(Object element) => _base.contains(element); |
| + |
| + E elementAt(int index) => _base.elementAt(index) as E; |
| + |
| + bool every(bool test(E element)) => _base.every(_assert(test)); |
| + |
| + Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => _base.expand(_assert(f)); |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Long line
nweiz
2016/03/29 20:18:05
Done.
|
| + |
| + E get first => _base.first as E; |
| + |
| + E firstWhere(bool test(E element), {E orElse()}) => |
| + _base.firstWhere(_assert(test), orElse: orElse) as E; |
| + |
| + /*=T*/ fold/*<T>*/( |
| + /*=T*/ initialValue, |
| + /*=T*/ combine(/*=T*/ previousValue, E element)) => |
| + _base.fold(initialValue, |
| + (previousValue, element) => combine(previousValue, element as E)); |
| + |
| + void forEach(void f(E element)) => _base.forEach(_assert(f)); |
| + |
| + bool get isEmpty => _base.isEmpty; |
| + |
| + bool get isNotEmpty => _base.isNotEmpty; |
| + |
| + Iterator<E> get iterator => _base.map((element) => element as E).iterator; |
| + |
| + String join([String separator = ""]) => _base.join(separator); |
| + |
| + E get last => _base.last as E; |
| + |
| + E lastWhere(bool test(E element), {E orElse()}) => |
| + _base.lastWhere(_assert(test), orElse: orElse) as E; |
| + |
| + int get length => _base.length; |
| + |
| + Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => _base.map(_assert(f)); |
| + |
| + E reduce(E combine(E value, E element)) => |
| + _base.reduce((value, element) => combine(value as E, element as E)) as E; |
| + |
| + E get single => _base.single as E; |
| + |
| + E singleWhere(bool test(E element)) => _base.singleWhere(_assert(test)) as E; |
| + |
| + Iterable<E> skip(int n) => new TypedIterable<E>(_base.skip(n)); |
| + |
| + Iterable<E> skipWhile(bool test(E value)) => |
| + new TypedIterable<E>(_base.skipWhile(_assert(test))); |
| + |
| + Iterable<E> take(int n) => new TypedIterable<E>(_base.take(n)); |
| + |
| + Iterable<E> takeWhile(bool test(E value)) => |
| + new TypedIterable<E>(_base.takeWhile(_assert(test))); |
| + |
| + List<E> toList({bool growable: true}) => |
| + new TypedList<E>(_base.toList(growable: growable)); |
| + |
| + Set<E> toSet() => new TypedSet<E>(_base.toSet()); |
| + |
| + Iterable<E> where(bool test(E element)) => |
| + new TypedIterable<E>(_base.where(_assert(test))); |
| + |
| + String toString() => _base.toString(); |
| + |
| + /// Returns a version of [function] that asserts that its argument is an |
| + /// instance of `E`. |
| + _UnaryFunction/*<dynamic, F>*/ _assert/*<F>*/( |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Consider renaming to _validate or _ensureType. The
nweiz
2016/03/29 20:18:05
Done.
|
| + _UnaryFunction/*<E, F>*/ function) => |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Just (F function(E argument)), no need for the typ
nweiz
2016/03/29 20:18:05
Done.
|
| + (value) => function(value as E); |
| +} |
| + |
| +/// An [Iterable] that asserts the types of values in a base iterable. |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Consider referring to where the class is used from
nweiz
2016/03/29 20:18:05
Done.
|
| +class TypedIterable<E> extends _TypedIterableBase<E> |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
The name TypedIterable is a little too close to th
nweiz
2016/03/29 20:18:05
Done.
|
| + implements DelegatingIterable<E> { |
| + final Iterable _base; |
| + |
| + const TypedIterable(Iterable base) : _base = base; |
| +} |
| + |
| +/// A [List] that asserts the types of values in a base list. |
| +class TypedList<E> extends TypedIterable<E> implements DelegatingList<E> { |
| + const TypedList(List base) : super(base); |
| + |
| + /// A [List]-typed getter for [_base]. |
| + List get _listBase => _base; |
| + |
| + E operator [](int index) => _listBase[index] as E; |
| + |
| + void operator []=(int index, E value) { |
| + _listBase[index] = value; |
| + } |
| + |
| + void add(E value) { |
| + _listBase.add(value); |
| + } |
| + |
| + void addAll(Iterable<E> iterable) { |
| + _listBase.addAll(iterable); |
| + } |
| + |
| + Map<int, E> asMap() => new TypedMap<int, E>(_listBase.asMap()); |
| + |
| + void clear() { |
| + _listBase.clear(); |
| + } |
| + |
| + void fillRange(int start, int end, [E fillValue]) { |
| + _listBase.fillRange(start, end, fillValue); |
| + } |
| + |
| + Iterable<E> getRange(int start, int end) => |
| + new TypedIterable<E>(_listBase.getRange(start, end)); |
| + |
| + int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start); |
| + |
| + void insert(int index, E element) { |
| + _listBase.insert(index, element); |
| + } |
| + |
| + void insertAll(int index, Iterable<E> iterable) { |
|
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Does this mean that the input iterable isn't check
nweiz
2016/03/29 20:18:05
In strong mode, I believe that's not possible. Oth
|
| + _listBase.insertAll(index, iterable); |
| + } |
| + |
| + int lastIndexOf(E element, [int start]) => |
| + _listBase.lastIndexOf(element, start); |
| + |
| + void set length(int newLength) { |
| + _listBase.length = newLength; |
| + } |
| + |
| + bool remove(Object value) => _listBase.remove(value); |
| + |
| + E removeAt(int index) => _listBase.removeAt(index) as E; |
| + |
| + E removeLast() => _listBase.removeLast() as E; |
| + |
| + void removeRange(int start, int end) { |
| + _listBase.removeRange(start, end); |
| + } |
| + |
| + void removeWhere(bool test(E element)) { |
| + _listBase.removeWhere(_assert(test)); |
| + } |
| + |
| + void replaceRange(int start, int end, Iterable<E> iterable) { |
| + _listBase.replaceRange(start, end, iterable); |
| + } |
| + |
| + void retainWhere(bool test(E element)) { |
| + _listBase.retainWhere(_assert(test)); |
| + } |
| + |
| + Iterable<E> get reversed => new TypedIterable<E>(_listBase.reversed); |
| + |
| + void setAll(int index, Iterable<E> iterable) { |
| + _listBase.setAll(index, iterable); |
| + } |
| + |
| + void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| + _listBase.setRange(start, end, iterable, skipCount); |
| + } |
| + |
| + void shuffle([math.Random random]) { |
| + _listBase.shuffle(random); |
| + } |
| + |
| + void sort([int compare(E a, E b)]) { |
| + if (compare == null) { |
| + _listBase.sort(); |
| + } else { |
| + _listBase.sort((a, b) => compare(a as E, b as E)); |
| + } |
| + } |
| + |
| + List<E> sublist(int start, [int end]) => |
| + new TypedList<E>(_listBase.sublist(start, end)); |
| +} |
| + |
| +/// A [Set] that asserts the types of values in a base set. |
| +class TypedSet<E> extends TypedIterable<E> implements DelegatingSet<E> { |
| + const TypedSet(Set base) : super(base); |
| + |
| + /// A [Set]-typed getter for [_base]. |
| + Set get _setBase => _base; |
| + |
| + bool add(E value) => _setBase.add(value); |
| + |
| + void addAll(Iterable<E> elements) { |
| + _setBase.addAll(elements); |
| + } |
| + |
| + void clear() { |
| + _setBase.clear(); |
| + } |
| + |
| + bool containsAll(Iterable<Object> other) => _setBase.containsAll(other); |
| + |
| + Set<E> difference(Set<E> other) => |
| + new TypedSet<E>(_setBase.difference(other)); |
| + |
| + Set<E> intersection(Set<Object> other) => |
| + new TypedSet<E>(_setBase.intersection(other)); |
| + |
| + E lookup(Object element) => _setBase.lookup(element) as E; |
| + |
| + bool remove(Object value) => _setBase.remove(value); |
| + |
| + void removeAll(Iterable<Object> elements) { |
| + _setBase.removeAll(elements); |
| + } |
| + |
| + void removeWhere(bool test(E element)) { |
| + _setBase.removeWhere(_assert(test)); |
| + } |
| + |
| + void retainAll(Iterable<Object> elements) { |
| + _setBase.retainAll(elements); |
| + } |
| + |
| + void retainWhere(bool test(E element)) { |
| + _setBase.retainWhere(_assert(test)); |
| + } |
| + |
| + Set<E> union(Set<E> other) => new TypedSet<E>(_setBase.union(other)); |
| +} |
| + |
| +/// A [Queue] that asserts the types of values in a base queue. |
| +class TypedQueue<E> extends TypedIterable<E> implements DelegatingQueue<E> { |
| + const TypedQueue(Queue queue) : super(queue); |
| + |
| + /// A [Queue]-typed getter for [_base]. |
| + Queue get _baseQueue => _base; |
| + |
| + void add(E value) { |
| + _baseQueue.add(value); |
| + } |
| + |
| + void addAll(Iterable<E> iterable) { |
| + _baseQueue.addAll(iterable); |
| + } |
| + |
| + void addFirst(E value) { |
| + _baseQueue.addFirst(value); |
| + } |
| + |
| + void addLast(E value) { |
| + _baseQueue.addLast(value); |
| + } |
| + |
| + void clear() { |
| + _baseQueue.clear(); |
| + } |
| + |
| + bool remove(Object object) => _baseQueue.remove(object); |
| + |
| + void removeWhere(bool test(E element)) { |
| + _baseQueue.removeWhere(_assert(test)); |
| + } |
| + |
| + void retainWhere(bool test(E element)) { |
| + _baseQueue.retainWhere(_assert(test)); |
| + } |
| + |
| + E removeFirst() => _baseQueue.removeFirst() as E; |
| + |
| + E removeLast() => _baseQueue.removeLast() as E; |
| +} |
| + |
| +/// A [Map] that asserts the types of keys and values in a base map. |
| +class TypedMap<K, V> implements DelegatingMap<K, V> { |
| + /// The base map to which operations are delegated. |
| + final Map _base; |
| + |
| + const TypedMap(Map base) : _base = base; |
| + |
| + V operator [](Object key) => _base[key] as V; |
| + |
| + void operator []=(K key, V value) { |
| + _base[key] = value; |
| + } |
| + |
| + void addAll(Map<K, V> other) { |
| + _base.addAll(other); |
| + } |
| + |
| + void clear() { |
| + _base.clear(); |
| + } |
| + |
| + bool containsKey(Object key) => _base.containsKey(key); |
| + |
| + bool containsValue(Object value) => _base.containsValue(value); |
| + |
| + void forEach(void f(K key, V value)) { |
| + _base.forEach((key, value) => f(key as K, value as V)); |
| + } |
| + |
| + bool get isEmpty => _base.isEmpty; |
| + |
| + bool get isNotEmpty => _base.isNotEmpty; |
| + |
| + Iterable<K> get keys => new TypedIterable<K>(_base.keys); |
| + |
| + int get length => _base.length; |
| + |
| + V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent) as V; |
| + |
| + V remove(Object key) => _base.remove(key) as V; |
| + |
| + Iterable<V> get values => new TypedIterable<V>(_base.values); |
| + |
| + String toString() => _base.toString(); |
| +} |