Skip to content

Commit

Permalink
Add methods where needed for Dart 2.0 core lib changes. (flutter#65)
Browse files Browse the repository at this point in the history
Add methods where needed for Dart 2.0 core lib changes.

Right now, all of the implementations throw (except in trivial cases, like the
EmptyUnmodifiable* classes). They can be updated, typically to call `super` or
reference a wrapped object, once the new methods have been released as part of a
Dart 2.0 release, and this pubspec.yaml updates to guarantee that those
implementations exist.
  • Loading branch information
srawlins authored Jan 2, 2018
1 parent 99ceec8 commit e6ed64c
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 4 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 1.14.4

* Add implementation stubs of upcoming Dart 2.0 core library methods, namely
new methods for classes that implement Iterable, List, Map, Queue, and Set.

## 1.14.3

* Fix MapKeySet.lookup to be a valid override in strong mode.

## 1.14.2

* Add type arguments to SyntheticInvocation.

## 1.14.1

* Make `Equality` implementations accept `null` as argument to `hash`.
Expand Down
46 changes: 46 additions & 0 deletions lib/src/canonicalized_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
other.forEach((key, value) => this[key] = value);
}

// TODO: Dart 2.0 requires this method to be implemented.
void addEntries(Iterable<Object> entries) {
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('addEntries');
}

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> cast<K2, V2>() {
throw new UnimplementedError('cast');
}

void clear() {
_base.clear();
}
Expand All @@ -81,6 +93,13 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
bool containsValue(Object value) =>
_base.values.any((pair) => pair.last == value);

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<Null> get entries {
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('entries');
}

void forEach(void f(K key, V value)) {
_base.forEach((key, pair) => f(pair.first, pair.last));
}
Expand All @@ -93,6 +112,13 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {

int get length => _base.length;

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> map<K2, V2>(Object transform(K key, V value)) {
// Change Object to MapEntry<K2, V2> when
// the MapEntry class has been added.
throw new UnimplementedError('map');
}

V putIfAbsent(K key, V ifAbsent()) {
return _base
.putIfAbsent(_canonicalize(key), () => new Pair(key, ifAbsent()))
Expand All @@ -105,6 +131,26 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
return pair == null ? null : pair.last;
}

// TODO: Dart 2.0 requires this method to be implemented.
void removeWhere(bool test(K key, V value)) {
throw new UnimplementedError('removeWhere');
}

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> retype<K2, V2>() {
throw new UnimplementedError('retype');
}

// TODO: Dart 2.0 requires this method to be implemented.
V update(K key, V update(V value), {V ifAbsent()}) {
throw new UnimplementedError('update');
}

// TODO: Dart 2.0 requires this method to be implemented.
void updateAll(V update(K key, V value)) {
throw new UnimplementedError('updateAll');
}

Iterable<V> get values => _base.values.map((pair) => pair.last);

String toString() => Maps.mapToString(this);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/empty_unmodifiable_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class EmptyUnmodifiableSet<E> extends IterableBase<E>

const EmptyUnmodifiableSet();

EmptyUnmodifiableSet<T> cast<T>() => const EmptyUnmodifiableSet<T>();
bool contains(Object element) => false;
bool containsAll(Iterable<Object> other) => other.isEmpty;
Iterable<E> followedBy(Iterable<E> other) => new Set.from(other);
E lookup(Object element) => null;
EmptyUnmodifiableSet<T> retype<T>() => const EmptyUnmodifiableSet<T>();
E singleWhere(bool test(E element), {E orElse()}) => super.singleWhere(test);
Iterable<T> whereType<T>() => const EmptyUnmodifiableSet<T>();
Set<E> toSet() => new Set();
Set<E> union(Set<E> other) => new Set.from(other);
Set<E> intersection(Set<Object> other) => new Set();
Expand Down
10 changes: 10 additions & 0 deletions lib/src/queue_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ class QueueList<E> extends Object with ListMixin<E> implements Queue<E> {
}
}

// TODO: Dart 2.0 requires this method to be implemented.
QueueList<T> cast<T>() {
throw new UnimplementedError('cast');
}

// TODO: Dart 2.0 requires this method to be implemented.
QueueList<T> retype<T>() {
throw new UnimplementedError('retype');
}

String toString() => IterableBase.iterableToFullString(this, "{", "}");

// Queue interface.
Expand Down
129 changes: 127 additions & 2 deletions lib/src/typed_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ abstract class _TypeSafeIterableBase<E> implements Iterable<E> {

bool any(bool test(E element)) => _base.any(_validate(test));

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> cast<T>() {
throw new UnimplementedError('cast');
}

bool contains(Object element) => _base.contains(element);

E elementAt(int index) => _base.elementAt(index) as E;
Expand All @@ -38,6 +43,11 @@ abstract class _TypeSafeIterableBase<E> implements Iterable<E> {
_base.fold(initialValue,
(previousValue, element) => combine(previousValue, element as E));

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<E> followedBy(Iterable<E> other) {
throw new UnimplementedError('followedBy');
}

void forEach(void f(E element)) => _base.forEach(_validate(f));

bool get isEmpty => _base.isEmpty;
Expand All @@ -60,10 +70,17 @@ abstract class _TypeSafeIterableBase<E> implements Iterable<E> {
E reduce(E combine(E value, E element)) =>
_base.reduce((value, element) => combine(value as E, element as E)) as E;

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> retype<T>() {
throw new UnimplementedError('retype');
}

E get single => _base.single as E;

E singleWhere(bool test(E element)) =>
_base.singleWhere(_validate(test)) as E;
E singleWhere(bool test(E element), {E orElse()}) {
if (orElse != null) throw new UnimplementedError('singleWhere:orElse');
return _base.singleWhere(_validate(test)) as E;
}

Iterable<E> skip(int n) => new TypeSafeIterable<E>(_base.skip(n));

Expand All @@ -83,6 +100,11 @@ abstract class _TypeSafeIterableBase<E> implements Iterable<E> {
Iterable<E> where(bool test(E element)) =>
new TypeSafeIterable<E>(_base.where(_validate(test)));

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> whereType<T>() {
throw new UnimplementedError('whereType');
}

String toString() => _base.toString();

/// Returns a version of [function] that asserts that its argument is an
Expand Down Expand Up @@ -116,6 +138,11 @@ class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> {
_listBase[index] = value;
}

// TODO: Dart 2.0 requires this method to be implemented.
List<E> operator +(List<E> other) {
throw new UnimplementedError('+');
}

void add(E value) {
_listBase.add(value);
}
Expand All @@ -126,6 +153,11 @@ class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> {

Map<int, E> asMap() => new TypeSafeMap<int, E>(_listBase.asMap());

// TODO: Dart 2.0 requires this method to be implemented.
List<T> cast<T>() {
throw new UnimplementedError('cast');
}

void clear() {
_listBase.clear();
}
Expand All @@ -134,11 +166,22 @@ class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> {
_listBase.fillRange(start, end, fillValue);
}

// TODO: Dart 2.0 requires this method to be implemented.
set first(E value) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[0] = value;
}

Iterable<E> getRange(int start, int end) =>
new TypeSafeIterable<E>(_listBase.getRange(start, end));

int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start);

// TODO: Dart 2.0 requires this method to be implemented.
int indexWhere(bool test(E element), [int start = 0]) {
throw new UnimplementedError('indexWhere');
}

void insert(int index, E element) {
_listBase.insert(index, element);
}
Expand All @@ -147,9 +190,20 @@ class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> {
_listBase.insertAll(index, iterable);
}

// TODO: Dart 2.0 requires this method to be implemented.
set last(E value) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[this.length - 1] = value;
}

int lastIndexOf(E element, [int start]) =>
_listBase.lastIndexOf(element, start);

// TODO: Dart 2.0 requires this method to be implemented.
int lastIndexWhere(bool test(E element), [int start]) {
throw new UnimplementedError('lastIndexWhere');
}

set length(int newLength) {
_listBase.length = newLength;
}
Expand All @@ -176,6 +230,11 @@ class TypeSafeList<E> extends TypeSafeIterable<E> implements DelegatingList<E> {
_listBase.retainWhere(_validate(test));
}

// TODO: Dart 2.0 requires this method to be implemented.
List<T> retype<T>() {
throw new UnimplementedError('retype');
}

Iterable<E> get reversed => new TypeSafeIterable<E>(_listBase.reversed);

void setAll(int index, Iterable<E> iterable) {
Expand Down Expand Up @@ -217,6 +276,11 @@ class TypeSafeSet<E> extends TypeSafeIterable<E> implements DelegatingSet<E> {
_setBase.addAll(elements);
}

// TODO: Dart 2.0 requires this method to be implemented.
Set<T> cast<T>() {
throw new UnimplementedError('cast');
}

void clear() {
_setBase.clear();
}
Expand Down Expand Up @@ -249,6 +313,11 @@ class TypeSafeSet<E> extends TypeSafeIterable<E> implements DelegatingSet<E> {
_setBase.retainWhere(_validate(test));
}

// TODO: Dart 2.0 requires this method to be implemented.
Set<T> retype<T>() {
throw new UnimplementedError('retype');
}

Set<E> union(Set<E> other) => new TypeSafeSet<E>(_setBase.union(other));
}

Expand Down Expand Up @@ -278,6 +347,11 @@ class TypeSafeQueue<E> extends TypeSafeIterable<E>
_baseQueue.addLast(value);
}

// TODO: Dart 2.0 requires this method to be implemented.
Queue<T> cast<T>() {
throw new UnimplementedError('cast');
}

void clear() {
_baseQueue.clear();
}
Expand All @@ -292,6 +366,11 @@ class TypeSafeQueue<E> extends TypeSafeIterable<E>
_baseQueue.retainWhere(_validate(test));
}

// TODO: Dart 2.0 requires this method to be implemented.
Queue<T> retype<T>() {
throw new UnimplementedError('retype');
}

E removeFirst() => _baseQueue.removeFirst() as E;

E removeLast() => _baseQueue.removeLast() as E;
Expand All @@ -316,6 +395,18 @@ class TypeSafeMap<K, V> implements DelegatingMap<K, V> {
_base.addAll(other);
}

// TODO: Dart 2.0 requires this method to be implemented.
void addEntries(Iterable<Object> entries) {
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('addEntries');
}

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> cast<K2, V2>() {
throw new UnimplementedError('cast');
}

void clear() {
_base.clear();
}
Expand All @@ -324,6 +415,13 @@ class TypeSafeMap<K, V> implements DelegatingMap<K, V> {

bool containsValue(Object value) => _base.containsValue(value);

// TODO: Dart 2.0 requires this method to be implemented.
Iterable<Null> get entries {
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('entries');
}

void forEach(void f(K key, V value)) {
_base.forEach((key, value) => f(key as K, value as V));
}
Expand All @@ -336,11 +434,38 @@ class TypeSafeMap<K, V> implements DelegatingMap<K, V> {

int get length => _base.length;

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> map<K2, V2>(Object transform(K key, V value)) {
// Change Object to MapEntry<K2, V2> when
// the MapEntry class has been added.
throw new UnimplementedError('map');
}

V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent) as V;

V remove(Object key) => _base.remove(key) as V;

// TODO: Dart 2.0 requires this method to be implemented.
void removeWhere(bool test(K key, V value)) {
throw new UnimplementedError('removeWhere');
}

// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> retype<K2, V2>() {
throw new UnimplementedError('retype');
}

Iterable<V> get values => new TypeSafeIterable<V>(_base.values);

String toString() => _base.toString();

// TODO: Dart 2.0 requires this method to be implemented.
V update(K key, V update(V value), {V ifAbsent()}) {
throw new UnimplementedError('update');
}

// TODO: Dart 2.0 requires this method to be implemented.
void updateAll(V update(K key, V value)) {
throw new UnimplementedError('updateAll');
}
}
8 changes: 8 additions & 0 deletions lib/src/unmodifiable_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ abstract class UnmodifiableMapMixin<K, V> implements Map<K, V> {
/// Throws an [UnsupportedError];
/// operations that change the map are disallowed.
void clear() => _throw();

/// Throws an [UnsupportedError];
/// operations that change the map are disallowed.
set first(_) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the map are disallowed.
set last(_) => _throw();
}
Loading

0 comments on commit e6ed64c

Please sign in to comment.