Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revive -To methods #51

Merged
merged 23 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ linter:
- sort_constructors_first
- sort_unnamed_constructors_first
- super_goes_last
- test_types_in_equals
- type_annotate_public_apis
- type_init_formals
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_new # under review (see #1068)
- unnecessary_null_aware_assignments
- unnecessary_null_in_if_null_operators
Expand Down
2 changes: 2 additions & 0 deletions lib/src/collection/list_mutable.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/collection/iterator.dart';
import 'package:dart_kollection/src/extension/collection_extension_mixin.dart';
import 'package:dart_kollection/src/extension/collection_mutable_extension_mixin.dart';
import 'package:dart_kollection/src/extension/iterable_extension_mixin.dart';
import 'package:dart_kollection/src/extension/iterable_mutable_extension_mixin.dart';
import 'package:dart_kollection/src/extension/list_extension_mixin.dart';
Expand All @@ -15,6 +16,7 @@ class DartMutableList<T>
KIterableExtensionsMixin<T>,
KCollectionExtensionMixin<T>,
KMutableIterableExtensionsMixin<T>,
KMutableCollectionExtensionMixin<T>,
KListExtensionsMixin<T>,
KMutableListExtensionsMixin<T>
implements KMutableList<T> {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/collection/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class DartSet<T>
if (other.hashCode != hashCode) return false;
if (other is KSet<T>) {
return containsAll(other);
} else {
return (other as KSet).containsAll(this);
}
return false;
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/collection/set_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class DartMutableSet<T>
if (other.hashCode != hashCode) return false;
if (other is KSet<T>) {
return containsAll(other);
} else {
return (other as KSet).containsAll(this);
}
return false;
}

@override
Expand Down
4 changes: 4 additions & 0 deletions lib/src/extension/collection_mutable_extension_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:dart_kollection/dart_kollection.dart';

abstract class KMutableCollectionExtensionMixin<T>
implements KMutableCollectionExtension<T>, KMutableCollection<T> {}
90 changes: 71 additions & 19 deletions lib/src/extension/iterable_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math' as math;
import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/comparisons.dart';
import 'package:dart_kollection/src/k_iterable.dart';
import 'package:dart_kollection/src/util/errors.dart';

abstract class KIterableExtensionsMixin<T>
implements KIterableExtension<T>, KIterable<T> {
Expand Down Expand Up @@ -88,15 +89,26 @@ abstract class KIterableExtensionsMixin<T>

@override
KMap<T, V> associateWith<V>(V Function(T) valueSelector) {
return associateWithTo(linkedMapOf<T, V>(), valueSelector);
final associated = associateWithTo(linkedMapOf<T, V>(), valueSelector);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type 'DartMutableList<String>' is not a subtype of type 'Null'
return associated;
}

// TODO add @override again
M associateWithTo<V, M extends KMutableMap<T, V>>(
@override
M associateWithTo<V, M extends KMutableMap<dynamic, dynamic>>(
M destination, V Function(T) valueSelector) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (valueSelector == null)
throw ArgumentError("valueSelector can't be null");
if (mutableMapOf<T, V>() is! M)
throw ArgumentError(
"associateWithTo destination has wrong type parameters."
"\nExpected: KMutableMap<$T, $V>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) items aren't subtype of "
"$runtimeType items. Items can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (var element in iter) {
Expand Down Expand Up @@ -277,23 +289,33 @@ abstract class KIterableExtensionsMixin<T>

@override
KList<T> filter(bool Function(T) predicate) {
final list = filterTo(mutableListOf<T>(), predicate);
final filtered = filterTo(mutableListOf<T>(), predicate);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type 'DartMutableList<String>' is not a subtype of type 'Null'
return list;
return filtered;
}

@override
KList<T> filterIndexed(bool Function(int index, T) predicate) {
final list = filterIndexedTo(mutableListOf<T>(), predicate);
return list;
final filtered = filterIndexedTo(mutableListOf<T>(), predicate);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type 'DartMutableList<String>' is not a subtype of type 'Null'
return filtered;
}

// TODO add @override again
C filterIndexedTo<C extends KMutableCollection<T>>(
@override
C filterIndexedTo<C extends KMutableCollection<dynamic>>(
C destination, bool Function(int index, T) predicate) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (predicate == null) throw ArgumentError("predicate can't be null");
if (mutableListOf<T>() is! C)
throw ArgumentError(
"filterIndexedTo destination has wrong type parameters."
"\nExpected: KMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
var i = 0;
Expand Down Expand Up @@ -332,8 +354,19 @@ abstract class KIterableExtensionsMixin<T>
return list;
}

// TODO add @override again
C filterNotNullTo<C extends KMutableCollection<T>>(C destination) {
@override
C filterNotNullTo<C extends KMutableCollection<dynamic>>(C destination) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (mutableListOf<T>() is! C)
throw ArgumentError(
"filterNotNullTo destination has wrong type parameters."
"\nExpected: KMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in iter) {
if (element != null) {
destination.add(element);
Expand All @@ -342,12 +375,18 @@ abstract class KIterableExtensionsMixin<T>
return destination;
}

// TODO add @override again
C filterNotTo<C extends KMutableCollection<T>>(
@override
C filterNotTo<C extends KMutableCollection<dynamic>>(
C destination, bool Function(T) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
if (destination == null) throw ArgumentError("destination can't be null");
if (mutableListOf<T>() is! C)
throw ArgumentError("filterNotTo destination has wrong type parameters."
"\nExpected: KMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in iter) {
Expand All @@ -358,12 +397,18 @@ abstract class KIterableExtensionsMixin<T>
return destination;
}

// TODO add @override again
C filterTo<C extends KMutableCollection<T>>(
@override
C filterTo<C extends KMutableCollection<dynamic>>(
C destination, bool Function(T) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
if (destination == null) throw ArgumentError("destination can't be null");
if (mutableListOf<T>() is! C)
throw ArgumentError("filterTo destination has wrong type parameters."
"\nExpected: KMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in iter) {
Expand Down Expand Up @@ -522,17 +567,23 @@ abstract class KIterableExtensionsMixin<T>
return groups;
}

// TODO add @override again
M groupByTo<K, M extends KMutableMap<K, KMutableList<T>>>(
@override
M groupByTo<K, M extends KMutableMap<K, KMutableList<dynamic>>>(
M destination, K Function(T) keySelector) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (keySelector == null) throw ArgumentError("keySelector can't be null");
if (mutableMapOf<K, KMutableList<T>>() is! M)
throw ArgumentError("groupByTo destination has wrong type parameters."
"\nExpected: KMutableMap<K, KMutableList<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in iter) {
final key = keySelector(element);
final list = destination.getOrPut(key, mutableListOf);
final list = destination.getOrPut(key, () => mutableListOf<T>());
list.add(element);
}
return destination;
Expand All @@ -550,7 +601,7 @@ abstract class KIterableExtensionsMixin<T>
}());
for (final element in iter) {
final key = keySelector(element);
final list = destination.getOrPut(key, mutableListOf);
final list = destination.getOrPut(key, () => mutableListOf<V>());
list.add(valueTransform(element));
}
return destination;
Expand Down Expand Up @@ -1241,6 +1292,7 @@ abstract class KIterableExtensionsMixin<T>
return list.toList();
}

// TODO expose as C extends KMutableCollection<dynamic> https://github.com/dart-lang/sdk/issues/35518
C toCollection<C extends KMutableCollection<T>>(C destination) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
Expand Down
61 changes: 51 additions & 10 deletions lib/src/extension/map_extensions_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/k_map_mutable.dart';
import 'package:dart_kollection/src/util/errors.dart';

abstract class KMapExtensionsMixin<K, V>
implements KMapExtension<K, V>, KMap<K, V> {
Expand All @@ -10,12 +11,18 @@ abstract class KMapExtensionsMixin<K, V>
return filtered;
}

// TODO add @override again
M filterTo<M extends KMutableMap<K, V>>(
@override
M filterTo<M extends KMutableMap<dynamic, dynamic>>(
M destination, bool Function(KMapEntry<K, V> entry) predicate) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (predicate == null) throw ArgumentError("predicate can't be null");
if (mutableMapOf<K, V>() is! M)
throw ArgumentError("filterTo destination has wrong type parameters."
"\nExpected: KMutableMap<$K, $V>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in entries.iter) {
Expand All @@ -33,12 +40,18 @@ abstract class KMapExtensionsMixin<K, V>
return filtered;
}

// TODO add @override again
M filterNotTo<M extends KMutableMap<K, V>>(
@override
M filterNotTo<M extends KMutableMap<dynamic, dynamic>>(
M destination, bool Function(KMapEntry<K, V> entry) predicate) {
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (predicate == null) throw ArgumentError("predicate can't be null");
if (mutableMapOf<K, V>() is! M)
throw ArgumentError("filterNotTo destination has wrong type parameters."
"\nExpected: KMutableMap<$K, $V>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (final element in entries.iter) {
Expand Down Expand Up @@ -80,10 +93,24 @@ abstract class KMapExtensionsMixin<K, V>
return mapped;
}

// TODO add @override again
M mapKeysTo<R, M extends KMutableMap<R, V>>(
@override
M mapKeysTo<R, M extends KMutableMap<dynamic, dynamic>>(
M destination, R Function(KMapEntry<K, V> entry) transform) {
return entries.associateByTo(destination, transform, (it) => it.value);
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (transform == null) throw ArgumentError("transform can't be null");
if (mutableMapOf<R, V>() is! M)
throw ArgumentError("mapKeysTo destination has wrong type parameters."
"\nExpected: KMutableMap<$R, $V>, Actual: ${destination.runtimeType}"
"\nEntries after key transformation with $transform have type KMapEntry<$R, $V> "
"and can't be copied into destination of type ${destination.runtimeType}."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (var element in entries.iter) {
destination.put(transform(element), element.value);
}
return destination;
}

@override
Expand All @@ -92,10 +119,24 @@ abstract class KMapExtensionsMixin<K, V>
return mapped;
}

// TODO add @override again
M mapValuesTo<R, M extends KMutableMap<K, R>>(
@override
M mapValuesTo<R, M extends KMutableMap<dynamic, dynamic>>(
M destination, R Function(KMapEntry<K, V> entry) transform) {
return entries.associateByTo(destination, (it) => it.key, transform);
assert(() {
if (destination == null) throw ArgumentError("destination can't be null");
if (transform == null) throw ArgumentError("transform can't be null");
if (mutableMapOf<K, R>() is! M)
throw ArgumentError("mapValuesTo destination has wrong type parameters."
"\nExpected: KMutableMap<$K, $R>, Actual: ${destination.runtimeType}"
"\nEntries after key transformation with $transform have type KMapEntry<$K, $R> "
"and can't be copied into destination of type ${destination.runtimeType}."
"\n\n$kBug35518GenericTypeError");
return true;
}());
for (var element in entries.iter) {
destination.put(element.key, transform(element));
}
return destination;
}

@override
Expand Down
7 changes: 6 additions & 1 deletion lib/src/k_collection_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import 'package:dart_kollection/dart_kollection.dart';
* @param E the type of elements contained in the collection. The mutable collection is invariant on its element type.
*/
abstract class KMutableCollection<T>
implements KCollection<T>, KMutableIterable<T> {
implements
KCollection<T>,
KMutableIterable<T>,
KMutableCollectionExtension<T> {
// Query Operations
@override
KMutableIterator<T> iterator();
Expand Down Expand Up @@ -55,3 +58,5 @@ abstract class KMutableCollection<T>
*/
void clear();
}

abstract class KMutableCollectionExtension<T> {}
Loading