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

Unified Diff: sdk/lib/collection/collections.dart

Issue 1319783003: Add static ensure() methods to unmodifiable views. Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Code review changes Created 5 years, 4 months 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 | « CHANGELOG.md ('k') | sdk/lib/collection/maps.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/collection/collections.dart
diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart
index fb9a94b8832dcbfb685b08ca6d39ab0750444a8e..e8dd28847935e11328a5b9a611ed2b3c540b4c9e 100644
--- a/sdk/lib/collection/collections.dart
+++ b/sdk/lib/collection/collections.dart
@@ -12,8 +12,21 @@ part of dart.collection;
*/
class UnmodifiableListView<E> extends UnmodifiableListBase<E> {
final Iterable<E> _source;
+
/** Create an unmodifiable list backed by [source]. */
UnmodifiableListView(Iterable<E> source) : _source = source;
+
+ /**
+ * Returns a view of [source] that's guaranteed to be unmodifiable.
+ *
+ * If [source] already extends [UnmodifiableListBase] or
+ * [UnmodifiableListView], it is returned without re-wrapping it.
+ */
+ static List ensure(Iterable source) =>
+ source is UnmodifiableListBase
+ ? source
+ : new UnmodifiableListView(source);
+
int get length => _source.length;
E operator[](int index) => _source.elementAt(index);
}
« no previous file with comments | « CHANGELOG.md ('k') | sdk/lib/collection/maps.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698