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

Side by Side Diff: pkg/unmodifiable_collection/lib/unmodifiable_collection.dart

Issue 17315017: Add unmodifiable_collection package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments. Created 7 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/unmodifiable_collection/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * Unmodifiable wrappers for [List], [Set] and [Map] objects.
7 *
8 * The wrappers allow reading from the source list, but writing is prohibited.
9 *
10 * A non-growable list wrapper allows writing as well, but not changing the
11 * list's length.
12 */
13 library unmodifiable_collection;
14
15 export "dart:collection" show UnmodifiableListView;
16
17 /**
18 * A [List] wrapper that acts as a non-growable list.
19 *
20 * Writes to the list are written through to the source list, but operations
21 * that change the length is not allowed.
22 */
23 class NonGrowableListView<E> extends _IterableView<E>
24 implements List<E> {
25 List<E> _source;
26 NonGrowableListView(List<E> source) : _source = source;
27
28 static void _throw() {
29 throw new UnsupportedError(
30 "Cannot change the length of a fixed-length list");
31 }
32
33 int get length => _source.length;
34
35 E operator [](int index) => _source[index];
36
37 int indexOf(E element, [int start = 0]) => _source.indexOf(element, start);
38
39 int lastIndexOf(E element, [int start])
40 => _source.lastIndexOf(element, start);
41
42 Iterable<E> getRange(int start, int end) => _source.getRange(start, end);
43
44 List<E> sublist(int start, [int end]) => _source.sublist(start, end);
45
46 Iterable<E> get reversed => _source.reversed;
47
48 Map<int, E> asMap() => _source.asMap();
49
50
51 void operator []=(int index, E value) { _source[index] = value; }
52
53 void sort([int compare(E a, E b)]) { _source.sort(compare); }
54
55 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
56 _source.setRange(start, end, iterable, skipCount);
57 }
58
59 void fillRange(int start, int end, [E fillValue]) {
60 _source.fillRange(start, end, fillValue);
61 }
62
63 void setAll(int index, Iterable<E> iterable) {
64 _source.setAll(index, iterable);
65 }
66
67
68 void set length(int newLength) => _throw();
69
70 void add(E value) => _throw();
71
72 void addAll(Iterable<E> iterable) => _throw();
73
74 void insert(int index, E element) => _throw();
75
76 void insertAll(int index, Iterable<E> iterable) => _throw();
77
78 bool remove(Object value) { _throw(); }
79
80 E removeAt(int index) { _throw(); }
81
82 E removeLast() { _throw(); }
83
84 void removeWhere(bool test(E element)) => _throw();
85
86 void retainWhere(bool test(E element)) => _throw();
87
88 void removeRange(int start, int end) => _throw();
89
90 void replaceRange(int start, int end, Iterable<E> iterable) => _throw();
91
92 void clear() => _throw();
93 }
94
95 /**
96 * A [Set] wrapper that acts as an unmodifiable set.
97 */
98 class UnmodifiableSetView<E> extends _IterableView<E>
99 implements Set<E> {
100 Set<E> _source;
101 UnmodifiableSetView(Set<E> source) : _source = source;
102
103 void _throw() {
104 throw new UnsupportedError("Cannot modify an unmodifiable Set");
105 }
106
107 bool containsAll(Iterable<E> other) => _source.containsAll(other);
108
109 Set<E> intersection(Set<E> other) => _source.intersection(other);
110
111 Set<E> union(Set<E> other) => _source.union(other);
112
113 Set<E> difference(Set<E> other) => _source.difference(other);
114
115
116 void add(E value) => _throw();
117
118 void addAll(Iterable<E> elements) => _throw();
119
120 bool remove(Object value) { _throw(); }
121
122 void removeAll(Iterable elements) => _throw();
123
124 void retainAll(Iterable elements) => _throw();
125
126 void removeWhere(bool test(E element)) => _throw();
127
128 void retainWhere(bool test(E element)) => _throw();
129
130 void clear() => _throw();
131 }
132
133 /**
134 * A [Map] wrapper that acts as an unmodifiable map.
135 */
136 class UnmodifiableMapView<K, V> implements Map<K, V> {
Søren Gjesse 2013/07/31 12:33:08 Could we consider having this in the dart:collecti
ahe 2013/08/07 15:57:59 +1 I need it for dart:mirrors.
floitsch 2013/11/05 01:23:25 I discussed this with Lasse, and we will probably
137 Map<K, V> _source;
138 UnmodifiableMapView(Map<K, V> source) : _source = source;
139
140 static void _throw() {
141 throw new UnsupportedError("Cannot modify an unmodifiable Map");
142 }
143
144 int get length => _source.length;
145
146 bool get isEmpty => _source.isEmpty;
147
148 bool get isNotEmpty => _source.isNotEmpty;
149
150 V operator [](K key) => _source[key];
151
152 bool containsKey(K key) => _source.containsKey(key);
153
154 bool containsValue(V value) => _source.containsValue(value);
155
156 void forEach(void f(K key, V value)) => _source.forEach(f);
157
158 Iterable<K> get keys => _source.keys;
159
160 Iterable<V> get values => _source.values;
161
162
163 void operator []=(K key, V value) => _throw();
164
165 V putIfAbsent(K key, V ifAbsent()) { _throw(); }
166
167 void addAll(Map<K, V> other) => _throw();
168
169 V remove(K key) { _throw(); }
170
171 void clear() => _throw();
172 }
173
174 abstract class _IterableView<E> {
175 Iterable<E> get _source;
176
177 bool any(bool test(E element)) => _source.any(test);
178
179 bool contains(E element) => _source.contains(element);
180
181 E elementAt(int index) => _source.elementAt(index);
182
183 bool every(bool test(E element)) => _source.every(test);
184
185 Iterable expand(Iterable f(E element)) => _source.expand(f);
186
187 E get first => _source.first;
188
189 E firstWhere(bool test(E element), { E orElse() })
190 => _source.firstWhere(test, orElse: orElse);
191
192 dynamic fold(var initialValue,
193 dynamic combine(var previousValue, E element))
194 => _source.fold(initialValue, combine);
195
196 void forEach(void f(E element)) => _source.forEach(f);
197
198 bool get isEmpty => _source.isEmpty;
199
200 bool get isNotEmpty => _source.isNotEmpty;
201
202 Iterator<E> get iterator => _source.iterator;
203
204 String join([String separator = ""]) => _source.join(separator);
205
206 E get last => _source.last;
207
208 E lastWhere(bool test(E element), {E orElse()})
209 => _source.lastWhere(test, orElse: orElse);
210
211 int get length => _source.length;
212
213 Iterable map(f(E element)) => _source.map(f);
214
215 E reduce(E combine(E value, E element)) => _source.reduce(combine);
216
217 E get single => _source.single;
218
219 E singleWhere(bool test(E element)) => _source.singleWhere(test);
220
221 Iterable<E> skip(int n) => _source.skip(n);
222
223 Iterable<E> skipWhile(bool test(E value)) => _source.skipWhile(test);
224
225 Iterable<E> take(int n) => _source.take(n);
226
227 Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test);
228
229 List<E> toList({ bool growable: true }) => _source.toList(growable: growable);
230
231 Set<E> toSet() => _source.toSet();
232
233 Iterable<E> where(bool test(E element)) => _source.where(test);
234 }
OLDNEW
« no previous file with comments | « no previous file | pkg/unmodifiable_collection/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698