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

Deprecate .set .list and .map in favour of asList, asSet and asMap #85

Merged
merged 15 commits into from
Mar 26, 2019
7 changes: 4 additions & 3 deletions lib/src/collection/extension/iterable_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,8 @@ abstract class KtIterableExtensionsMixin<T>
return true;
}());
final mutableList = toMutableList();
mutableList.list.sort(comparator);
// delegate to darts list implementation for sorting which is highly optimized
mutableList.asList().sort(comparator);
return mutableList;
}

Expand Down Expand Up @@ -1458,8 +1459,8 @@ class _MovingSubList<T> {
_MovingSubList(this.list);

KtList<T> list;
var _fromIndex = 0;
var _size = 0;
int _fromIndex = 0;
int _size = 0;

void move(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > list.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ abstract class KtMutableListExtensionsMixin<T>
if (comparator == null) throw ArgumentError("comparator can't be null");
return true;
}());
list.sort(comparator);
// delegate to darts list implementation for sorting which is highly optimized
asList().sort(comparator);
}

@override
Expand Down
70 changes: 35 additions & 35 deletions lib/src/collection/extension/map_extensions_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@ import 'package:kt_dart/src/util/errors.dart';

abstract class KtMapExtensionsMixin<K, V>
implements KtMapExtension<K, V>, KtMap<K, V> {
@override
bool all(Function(K key, V value) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
return true;
}());
if (isEmpty()) {
return true;
}
for (KtMapEntry<K, V> entry in entries.iter) {
if (!predicate(entry.key, entry.value)) {
return false;
}
}
return true;
}

@override
bool any(Function(K key, V value) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
return true;
}());
if (isEmpty()) {
return false;
}
for (KtMapEntry<K, V> entry in entries.iter) {
if (predicate(entry.key, entry.value)) {
return true;
}
}
return false;
}

@override
KtMap<K, V> filter(bool Function(KtMapEntry<K, V> entry) predicate) {
final filtered = filterTo(linkedMapFrom<K, V>(), predicate);
Expand Down Expand Up @@ -196,7 +230,7 @@ abstract class KtMapExtensionsMixin<K, V>

@override
KtMutableMap<K, V> toMutableMap() {
return mutableMapFrom(map);
return mutableMapFrom(asMap());
}

@override
Expand Down Expand Up @@ -225,40 +259,6 @@ abstract class KtMapExtensionsMixin<K, V>
return true;
}

@override
bool all(Function(K key, V value) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
return true;
}());
if (isEmpty()) {
return true;
}
for (KtMapEntry<K, V> entry in entries.iter) {
if (!predicate(entry.key, entry.value)) {
return false;
}
}
return true;
}

@override
bool any(Function(K key, V value) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
return true;
}());
if (isEmpty()) {
return false;
}
for (KtMapEntry<K, V> entry in entries.iter) {
if (predicate(entry.key, entry.value)) {
return true;
}
}
return false;
}

@override
String toString() {
return entries.joinToString(
Expand Down
84 changes: 84 additions & 0 deletions lib/src/collection/impl/dart_unmodifiable_set_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'dart:collection';

/// An unmodifiable set.
///
/// An UnmodifiableSetView contains a [Set] object and ensures
/// that it does not change.
/// Methods that would change the set,
/// such as [add] and [remove], throw an [UnsupportedError].
/// Permitted operations defer to the wrapped set.
class UnmodifiableSetView<E> with IterableMixin<E> implements Set<E> {
UnmodifiableSetView(Set<E> set) : _set = set;

final Set<E> _set;

@override
Iterator<E> get iterator => _set.iterator;

@override
int get length => _set.length;

@override
Set<T> cast<T>() => _set.cast<T>();

@override
bool containsAll(Iterable<Object> other) => _set.containsAll(other);

@override
Set<E> difference(Set<Object> other) => _set.difference(other);

@override
Set<E> intersection(Set<Object> other) => _set.intersection(other);

@override
E lookup(Object object) => _set.lookup(object);

@override
Set<E> union(Set<E> other) => _set.union(other);

@override
Set<E> toSet() => _set.toSet();

static T _throw<T>() =>
throw UnsupportedError("Cannot modify an unmodifiable Set");

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
bool add(E value) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void addAll(Iterable<E> elements) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
bool remove(Object value) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void removeAll(Iterable elements) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void retainAll(Iterable elements) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void removeWhere(bool test(E element)) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void retainWhere(bool test(E element)) => _throw();

/// Throws an [UnsupportedError];
/// operations that change the set are disallowed.
@override
void clear() => _throw();
}
2 changes: 1 addition & 1 deletion lib/src/collection/impl/iterator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class InterOpKIterator<T> implements KtIterator<T> {
final Iterator<T> iterator;
T nextValue;
T lastReturned;
var _hasNext = false;
bool _hasNext;

@override
bool hasNext() {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/collection/impl/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DartList<T>
implements KtList<T> {
/// Create an immutable [KtList] by copying the incoming [iterable] into a [List]
DartList([Iterable<T> iterable = const []])
: _list = List.from(iterable, growable: false),
: _list = List.unmodifiable(iterable),
super();

final List<T> _list;
Expand All @@ -28,6 +28,9 @@ class DartList<T>
@override
List<T> get list => _list;

@override
List<T> asList() => _list;

@override
bool contains(T element) => _list.contains(element);

Expand Down
5 changes: 4 additions & 1 deletion lib/src/collection/impl/list_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class EmptyList<T>
KtListExtensionsMixin<T>
implements KtList<T> {
@override
List<T> get list => <T>[];
List<T> get list => List.unmodifiable([]);

@override
List<T> asList() => List.unmodifiable([]);

@override
bool contains(T element) => false;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/collection/impl/list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class DartMutableList<T>
@override
List<T> get list => _list;

@override
List<T> asList() => _list;

@override
bool contains(T element) => _list.contains(element);

Expand Down
10 changes: 6 additions & 4 deletions lib/src/collection/impl/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class DartMap<K, V> with KtMapExtensionsMixin<K, V> implements KtMap<K, V> {
int _hashCode;

@override
Map<K, V> get map => _map;
Iterable<MapEntry<K, V>> get iter => _map.entries;

@override
Map<K, V> asMap() => _map;

@override
bool containsKey(K key) => _map.containsKey(key);
Expand Down Expand Up @@ -70,9 +73,8 @@ class DartMap<K, V> with KtMapExtensionsMixin<K, V> implements KtMap<K, V> {
class _Entry<K, V> extends KtMapEntry<K, V> {
_Entry(this.key, this.value);

_Entry.from(MapEntry<K, V> entry)
: key = entry.key,
value = entry.value;
factory _Entry.from(MapEntry<K, V> entry) => _Entry(entry.key, entry.value);

@override
final K key;

Expand Down
5 changes: 4 additions & 1 deletion lib/src/collection/impl/map_empty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import 'package:kt_dart/src/collection/extension/map_extensions_mixin.dart';

class EmptyMap<K, V> with KtMapExtensionsMixin<K, V> implements KtMap<K, V> {
@override
Map<K, V> get map => {};
Iterable<MapEntry<K, V>> get iter => List.unmodifiable([]);

@override
Map<K, V> asMap() => Map.unmodifiable({});

@override
V operator [](K key) => null;
Expand Down
10 changes: 6 additions & 4 deletions lib/src/collection/impl/map_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class DartMutableMap<K, V>
final Map<K, V> _map;

@override
Map<K, V> get map => _map;
Iterable<MapEntry<K, V>> get iter => _map.entries;

@override
Map<K, V> asMap() => _map;

@override
bool containsKey(K key) => _map.containsKey(key);
Expand Down Expand Up @@ -117,9 +120,8 @@ class DartMutableMap<K, V>
class _MutableEntry<K, V> implements KtMutableMapEntry<K, V> {
_MutableEntry(this._key, this._value);

_MutableEntry.from(MapEntry<K, V> entry)
: _key = entry.key,
_value = entry.value;
factory _MutableEntry.from(MapEntry<K, V> entry) =>
_MutableEntry(entry.key, entry.value);

K _key;
V _value;
Expand Down
8 changes: 6 additions & 2 deletions lib/src/collection/impl/set.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:kt_dart/collection.dart';
import 'package:kt_dart/src/collection/extension/collection_extension_mixin.dart';
import 'package:kt_dart/src/collection/extension/iterable_extension_mixin.dart';
import 'package:kt_dart/src/collection/impl/dart_unmodifiable_set_view.dart';
import 'package:kt_dart/src/util/hash.dart';

class DartSet<T>
with KtIterableExtensionsMixin<T>, KtCollectionExtensionMixin<T>
implements KtSet<T> {
DartSet([Iterable<T> iterable = const []])
: _set = Set.from(iterable),
: _set = UnmodifiableSetView(Set.from(iterable)),
super();

final Set<T> _set;
Expand All @@ -23,6 +24,9 @@ class DartSet<T>
return Set.of(_set);
}

@override
Set<T> asSet() => _set;

@override
bool contains(T element) => _set.contains(element);

Expand Down Expand Up @@ -75,7 +79,7 @@ class _DartToKIterator<T> extends KtIterator<T> {
final Iterator<T> iterator;
T nextValue;
T lastReturned;
var _hasNext = false;
bool _hasNext;

@override
bool hasNext() {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/collection/impl/set_empty.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:kt_dart/collection.dart';
import 'package:kt_dart/src/collection/extension/collection_extension_mixin.dart';
import 'package:kt_dart/src/collection/extension/iterable_extension_mixin.dart';
Expand All @@ -7,7 +8,10 @@ class EmptySet<T>
with KtIterableExtensionsMixin<T>, KtCollectionExtensionMixin<T>
implements KtSet<T> {
@override
Set<T> get set => Set();
Set<T> get set => UnmodifiableSetView(Set());

@override
Set<T> asSet() => UnmodifiableSetView(Set());

@override
bool contains(T element) => false;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/collection/impl/set_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class DartMutableSet<T>
@override
Set<T> get set => _set;

@override
Set<T> asSet() => _set;

@override
bool contains(T element) => _set.contains(element);

Expand Down Expand Up @@ -127,7 +130,7 @@ class _MutableSetIterator<T> extends KtMutableIterator<T> {
final Iterator<T> _iterator;
T nextValue;
T lastReturned;
var _hasNext = false;
bool _hasNext;

@override
bool hasNext() {
Expand Down
Loading