Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Unified Diff: pkg/collection_helpers/lib/unmodifiable_wrappers.dart

Issue 70073003: Combine unmodifiable_collection package into collection_helpers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Remove references from other packages. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/collection_helpers/lib/equality.dart ('k') | pkg/collection_helpers/lib/wrappers.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/collection_helpers/lib/unmodifiable_wrappers.dart
diff --git a/pkg/unmodifiable_collection/lib/unmodifiable_collection.dart b/pkg/collection_helpers/lib/unmodifiable_wrappers.dart
similarity index 57%
rename from pkg/unmodifiable_collection/lib/unmodifiable_collection.dart
rename to pkg/collection_helpers/lib/unmodifiable_wrappers.dart
index 9fd35e8a4c6db9338dcc3189d3b81a13ff54c31d..b44337f844a6eb8820e36e0416de1b99183024be 100644
--- a/pkg/unmodifiable_collection/lib/unmodifiable_collection.dart
+++ b/pkg/collection_helpers/lib/unmodifiable_wrappers.dart
@@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
/**
- * Wrappers that prevent List, Set, or Map objects from being modified.
+ * Wrappers that prevent a List, Set, or Map object from being modified.
*
* The [Set] and [Map] wrappers allow reading from the wrapped collection,
* but prohibit writing.
@@ -11,69 +11,36 @@
* The [List] wrapper prevents changes to the length of the wrapped list,
* but allows changes to the contents.
*/
-library unmodifiable_collection;
-
-import "dart:math" show Random;
-export "dart:collection" show UnmodifiableListView;
+part of dart.collection_helpers.wrappers;
/**
* A fixed-length list.
*
- * A NonGrowableListView contains a [List] object and ensures that
+ * A `NonGrowableListView` contains a [List] object and ensures that
* its length does not change.
* Methods that would change the length of the list,
* such as [add] and [remove], throw an [UnsupportedError].
+ * All other methods work directly on the underlying list.
*
* This class _does_ allow changes to the contents of the wrapped list.
* You can, for example, [sort] the list.
* Permitted operations defer to the wrapped list.
*/
-class NonGrowableListView<E> extends _IterableView<E>
- implements List<E> {
- List<E> _source;
- NonGrowableListView(List<E> source) : _source = source;
+class NonGrowableListView<E> extends DelegatingList<E>
+ with NonGrowableListMixin<E> {
+ NonGrowableListView(List<E> listBase) : super(listBase);
+}
+/**
+ * Mixin class that implements a throwing version of all list operations that
+ * change the List's length.
+ */
+abstract class NonGrowableListMixin<E> implements List<E> {
static void _throw() {
throw new UnsupportedError(
"Cannot change the length of a fixed-length list");
}
- int get length => _source.length;
-
- E operator [](int index) => _source[index];
-
- int indexOf(E element, [int start = 0]) => _source.indexOf(element, start);
-
- int lastIndexOf(E element, [int start])
- => _source.lastIndexOf(element, start);
-
- Iterable<E> getRange(int start, int end) => _source.getRange(start, end);
-
- List<E> sublist(int start, [int end]) => _source.sublist(start, end);
-
- Iterable<E> get reversed => _source.reversed;
-
- Map<int, E> asMap() => _source.asMap();
-
- void operator []=(int index, E value) { _source[index] = value; }
-
- void sort([int compare(E a, E b)]) { _source.sort(compare); }
-
- void shuffle([Random random]) { _source.shuffle(random); }
-
- void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
- _source.setRange(start, end, iterable, skipCount);
- }
-
- void fillRange(int start, int end, [E fillValue]) {
- _source.fillRange(start, end, fillValue);
- }
-
- void setAll(int index, Iterable<E> iterable) {
- _source.setAll(index, iterable);
- }
-
-
/**
* Throws an [UnsupportedError];
* operations that change the length of the list are disallowed.
@@ -164,25 +131,20 @@ class NonGrowableListView<E> extends _IterableView<E>
* such as [add] and [remove], throw an [UnsupportedError].
* Permitted operations defer to the wrapped set.
*/
-class UnmodifiableSetView<E> extends _IterableView<E>
- implements Set<E> {
- Set<E> _source;
- UnmodifiableSetView(Set<E> source) : _source = source;
+class UnmodifiableSetView<E> extends DelegatingSet<E>
+ with UnmodifiableSetMixin<E> {
+ UnmodifiableSetView(Set<E> setBase) : super(setBase);
+}
+/**
+ * Mixin class that implements a throwing version of all set operations that
+ * change the Set.
+ */
+abstract class UnmodifiableSetMixin<E> implements Set<E> {
void _throw() {
throw new UnsupportedError("Cannot modify an unmodifiable Set");
}
- bool containsAll(Iterable<E> other) => _source.containsAll(other);
-
- Set<E> intersection(Set<E> other) => _source.intersection(other);
-
- Set<E> union(Set<E> other) => _source.union(other);
-
- Set<E> difference(Set<E> other) => _source.difference(other);
-
- E lookup(Object object) => _source.lookup(object);
-
/**
* Throws an [UnsupportedError];
* operations that change the set are disallowed.
@@ -243,33 +205,20 @@ class UnmodifiableSetView<E> extends _IterableView<E>
* such as [addAll] and [remove], throw an [UnsupportedError].
* Permitted operations defer to the wrapped map.
*/
-class UnmodifiableMapView<K, V> implements Map<K, V> {
- Map<K, V> _source;
- UnmodifiableMapView(Map<K, V> source) : _source = source;
+class UnmodifiableMapView<K, V> extends DelegatingMap<K, V>
+ with UnmodifiableMapMixin<K, V> {
+ UnmodifiableMapView(Map<K, V> baseMap) : super(baseMap);
+}
+/**
+ * Mixin class that implements a throwing version of all map operations that
+ * change the Map.
+ */
+abstract class UnmodifiableMapMixin<K, V> implements Map<K, V> {
static void _throw() {
throw new UnsupportedError("Cannot modify an unmodifiable Map");
}
- int get length => _source.length;
-
- bool get isEmpty => _source.isEmpty;
-
- bool get isNotEmpty => _source.isNotEmpty;
-
- V operator [](K key) => _source[key];
-
- bool containsKey(K key) => _source.containsKey(key);
-
- bool containsValue(V value) => _source.containsValue(value);
-
- void forEach(void f(K key, V value)) => _source.forEach(f);
-
- Iterable<K> get keys => _source.keys;
-
- Iterable<V> get values => _source.values;
-
-
/**
* Throws an [UnsupportedError];
* operations that change the map are disallowed.
@@ -300,65 +249,3 @@ class UnmodifiableMapView<K, V> implements Map<K, V> {
*/
void clear() => _throw();
}
-
-abstract class _IterableView<E> {
- Iterable<E> get _source;
-
- bool any(bool test(E element)) => _source.any(test);
-
- bool contains(E element) => _source.contains(element);
-
- E elementAt(int index) => _source.elementAt(index);
-
- bool every(bool test(E element)) => _source.every(test);
-
- Iterable expand(Iterable f(E element)) => _source.expand(f);
-
- E get first => _source.first;
-
- E firstWhere(bool test(E element), { E orElse() })
- => _source.firstWhere(test, orElse: orElse);
-
- dynamic fold(var initialValue,
- dynamic combine(var previousValue, E element))
- => _source.fold(initialValue, combine);
-
- void forEach(void f(E element)) => _source.forEach(f);
-
- bool get isEmpty => _source.isEmpty;
-
- bool get isNotEmpty => _source.isNotEmpty;
-
- Iterator<E> get iterator => _source.iterator;
-
- String join([String separator = ""]) => _source.join(separator);
-
- E get last => _source.last;
-
- E lastWhere(bool test(E element), {E orElse()})
- => _source.lastWhere(test, orElse: orElse);
-
- int get length => _source.length;
-
- Iterable map(f(E element)) => _source.map(f);
-
- E reduce(E combine(E value, E element)) => _source.reduce(combine);
-
- E get single => _source.single;
-
- E singleWhere(bool test(E element)) => _source.singleWhere(test);
-
- Iterable<E> skip(int n) => _source.skip(n);
-
- Iterable<E> skipWhile(bool test(E value)) => _source.skipWhile(test);
-
- Iterable<E> take(int n) => _source.take(n);
-
- Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test);
-
- List<E> toList({ bool growable: true }) => _source.toList(growable: growable);
-
- Set<E> toSet() => _source.toSet();
-
- Iterable<E> where(bool test(E element)) => _source.where(test);
-}
« no previous file with comments | « pkg/collection_helpers/lib/equality.dart ('k') | pkg/collection_helpers/lib/wrappers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698