Skip to content

Commit

Permalink
Merge branch 'master' into core-libs-dart-4
Browse files Browse the repository at this point in the history
* master:
  Add new Dart 2 Iterable and List methods (google#422)
  • Loading branch information
srawlins committed Mar 20, 2018
2 parents 767a692 + d84e3af commit 6c84324
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#### Master

* New: DelegatingIterable now includes a real implementation of
`followedBy`, and accepts the `orElse` parameter on `singleWhere`.
* New: DelegatingList now includes real implementations of `operator +`,
`indexWhere`, and `lastIndexWhere`.
* New: LruMap now includes a real implementation of `addEntries`, `get
entries`, `removeWhere`, `update`, and `updateAll`.

Expand Down
12 changes: 3 additions & 9 deletions lib/src/collection/delegates/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ abstract class DelegatingIterable<E> implements Iterable<E> {
delegate.fold(initialValue, combine);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
Iterable<E> followedBy(Iterable<E> other) {
throw new UnimplementedError("followedBy");
}
Iterable<E> followedBy(Iterable<E> other) => delegate.followedBy(other);

void forEach(void f(E element)) => delegate.forEach(f);

Expand Down Expand Up @@ -89,10 +85,8 @@ abstract class DelegatingIterable<E> implements Iterable<E> {

E get single => delegate.single;

E singleWhere(bool test(E element), {E orElse()}) {
if (orElse != null) throw new UnimplementedError("singleWhere:orElse");
return delegate.singleWhere(test);
}
E singleWhere(bool test(E element), {E orElse()}) =>
delegate.singleWhere(test, orElse: orElse);

Iterable<E> skip(int n) => delegate.skip(n);

Expand Down
22 changes: 5 additions & 17 deletions lib/src/collection/delegates/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
List<E> operator +(List<E> other) {
throw new UnimplementedError("+");
}
List<E> operator +(List<E> other) => delegate + other;

void add(E value) => delegate.add(value);

Expand Down Expand Up @@ -79,20 +75,15 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
int indexOf(E element, [int start = 0]) => delegate.indexOf(element, start);

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

void insert(int index, E element) => delegate.insert(index, element);

void insertAll(int index, Iterable<E> iterable) =>
delegate.insertAll(index, iterable);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_setter
void set last(E element) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[this.length - 1] = element;
Expand All @@ -102,11 +93,8 @@ abstract class DelegatingList<E> extends DelegatingIterable<E>
delegate.lastIndexOf(element, start);

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
int lastIndexWhere(bool test(E element), [int start]) {
throw new UnimplementedError("lastIndexWhere");
}
int lastIndexWhere(bool test(E element), [int start]) =>
delegate.lastIndexWhere(test, start);

void set length(int newLength) {
delegate.length = newLength;
Expand Down
25 changes: 9 additions & 16 deletions lib/src/collection/multimap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,9 @@ class _WrappedIterable<K, V, C extends Iterable<V>> implements Iterable<V> {
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
Iterable<V> followedBy(Iterable<V> other) {
throw new UnimplementedError("followedBy");
_syncDelegate();
return _delegate.followedBy(other);
}

void forEach(void f(V element)) {
Expand Down Expand Up @@ -499,9 +498,8 @@ class _WrappedIterable<K, V, C extends Iterable<V>> implements Iterable<V> {
}

V singleWhere(bool test(V element), {V orElse()}) {
if (orElse != null) throw new UnimplementedError("singleWhere:orElse");
_syncDelegate();
return _delegate.singleWhere(test);
return _delegate.singleWhere(test, orElse: orElse);
}

Iterable<V> skip(int n) {
Expand Down Expand Up @@ -565,10 +563,9 @@ class _WrappedList<K, V> extends _WrappedIterable<K, V, List<V>>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
List<V> operator +(List<V> other) {
throw new UnimplementedError("+");
_syncDelegate();
return _delegate + other;
}

void add(V value) {
Expand Down Expand Up @@ -627,10 +624,9 @@ class _WrappedList<K, V> extends _WrappedIterable<K, V, List<V>>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
int indexWhere(bool test(V element), [int start = 0]) {
throw new UnimplementedError("indexWhere");
_syncDelegate();
return _delegate.indexWhere(test, start);
}

void insert(int index, V element) {
Expand All @@ -648,8 +644,6 @@ class _WrappedList<K, V> extends _WrappedIterable<K, V, List<V>>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_setter
void set last(V value) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[this.length - 1] = value;
Expand All @@ -661,10 +655,9 @@ class _WrappedList<K, V> extends _WrappedIterable<K, V, List<V>>
}

@override
// TODO: Dart 2.0 requires this method to be implemented.
// ignore: override_on_non_overriding_method
int lastIndexWhere(bool test(V element), [int start]) {
throw new UnimplementedError("lastIndexWhere");
_syncDelegate();
return _delegate.lastIndexWhere(test, start);
}

void set length(int newLength) {
Expand Down
9 changes: 9 additions & 0 deletions test/collection/delegates/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ void main() {
expect(new MyIterable([]).isNotEmpty, isFalse);
});

test('followedBy', () {
expect(delegatingIterable.followedBy(['d', 'e']),
equals(['a', 'b', 'cc', 'd', 'e']));
expect(delegatingIterable.followedBy(delegatingIterable),
equals(['a', 'b', 'cc', 'a', 'b', 'cc']));
});

test('forEach', () {
final it = delegatingIterable.iterator;
expect(it.current, isNull);
Expand Down Expand Up @@ -138,6 +145,8 @@ void main() {
expect(delegatingIterable.singleWhere((e) => e == 'b'), equals('b'));
expect(() => delegatingIterable.singleWhere((e) => e == 'd'),
throwsStateError);
expect(delegatingIterable.singleWhere((e) => e == 'd', orElse: () => 'X'),
equals('X'));
});

test('skip', () {
Expand Down
15 changes: 15 additions & 0 deletions test/collection/delegates/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void main() {
expect(delegatingList, equals(['d', 'b', 'cc']));
});

test('+', () {
var sum = delegatingList + ['d', 'e'];
expect(sum, equals(['a', 'b', 'cc', 'd', 'e']));
});

test('add', () {
delegatingList.add('d');
expect(delegatingList, equals(['a', 'b', 'cc', 'd']));
Expand Down Expand Up @@ -82,6 +87,11 @@ void main() {
expect(delegatingList.indexOf('cc', 1), equals(2));
});

test('indexWhere', () {
delegatingList.add('bb');
expect(delegatingList.indexWhere((e) => e.length > 1), equals(2));
});

test('insert', () {
delegatingList.insert(1, 'd');
expect(delegatingList, equals(['a', 'd', 'b', 'cc']));
Expand All @@ -98,6 +108,11 @@ void main() {
expect(delegatingList.lastIndexOf('cc', 1), equals(-1));
});

test('lastIndexWhere', () {
delegatingList.add('bb');
expect(delegatingList.lastIndexWhere((e) => e.length > 1), equals(3));
});

test('set length', () {
delegatingList.length = 2;
expect(delegatingList, equals(['a', 'b']));
Expand Down

0 comments on commit 6c84324

Please sign in to comment.