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

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: 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
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 /**
16 * A [List] wrapper that acts as an unmodifiable list.
17 */
18 class UnmodifiableListView<E> extends _IterableView<E>
floitsch 2013/06/20 13:07:33 Just reexport the one from dart:collection.
Lasse Reichstein Nielsen 2013/06/20 14:04:19 Done.
19 implements List<E> {
20 List<E> _source;
21 UnmodifiableListView(List<E> source) : _source = source;
22
23 static void _throw() {
24 throw new UnsupportedError("Cannot modify an unmodifiable list");
25 }
26
27 int get length => _source.length;
28
29 E operator [](int index) => _source[index];
30
31 int indexOf(E element, [int start = 0]) => _source.indexOf(element, start);
32
33 int lastIndexOf(E element, [int start])
34 => _source.lastIndexOf(element, start);
35
36 Iterable<E> getRange(int start, int end) => _source.getRange(start, end);
37
38 List<E> sublist(int start, [int end]) => _source.sublist(start, end);
39
40 Iterable<E> get reversed => _source.reversed;
41
42 Map<int, E> asMap() => _source.asMap();
43
44
45 void operator []=(int index, E value) => _throw();
46
47 void sort([int compare(E a, E b)]) => _throw();
48
49 void fillRange(int start, int end, [E fillValue]) => _throw();
50
51 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
52 _throw();
53 }
54
55 void setAll(int index, Iterable<E> iterable) => _throw();
56
57
58 void set length(int newLength) => _throw();
59
60 void add(E value) => _throw();
61
62 void addAll(Iterable<E> iterable) => _throw();
63
64 void insert(int index, E element) => _throw();
65
66 void insertAll(int index, Iterable<E> iterable) => _throw();
67
68 bool remove(Object value) => _throw();
69
70 E removeAt(int index) => _throw();
71
72 E removeLast() => _throw();
73
74 void removeWhere(bool test(E element)) => _throw();
75
76 void retainWhere(bool test(E element)) => _throw();
77
78 void removeRange(int start, int end) => _throw();
79
80 void replaceRange(int start, int end, Iterable<E> iterable) => _throw();
81
82 void clear() => _throw();
83 }
84
85 /**
86 * A [List] wrapper that acts as a non-growable list.
87 *
88 * Writes to the list are written through to the source list, but operations
89 * that change the length is not allowed.
90 */
91 class NonGrowableListView<E> extends _IterableView<E>
92 implements List<E> {
93 List<E> _source;
94 NonGrowableListView(List<E> source) : _source = source;
95
96 static void _throw() {
97 throw new UnsupportedError(
98 "Cannot change the length of a fixed-length list");
99 }
100
101 int get length => _source.length;
102
103 E operator [](int index) => _source[index];
104
105 int indexOf(E element, [int start = 0]) => _source.indexOf(element, start);
106
107 int lastIndexOf(E element, [int start])
108 => _source.lastIndexOf(element, start);
109
110 Iterable<E> getRange(int start, int end) => _source.getRange(start, end);
111
112 List<E> sublist(int start, [int end]) => _source.sublist(start, end);
113
114 Iterable<E> get reversed => _source.reversed;
115
116 Map<int, E> asMap() => _source.asMap();
117
118
119 void operator []=(int index, E value) { _source[index] = value; }
120
121 void sort([int compare(E a, E b)]) { _source.sort(compare); }
122
123 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
124 _source.setRange(start, end, iterable, skipCount);
125 }
126
127 void fillRange(int start, int end, [E fillValue]) {
128 _source.fillRange(start, end, fillValue);
129 }
130
131 void setAll(int index, Iterable<E> iterable) {
132 _source.setAll(index, iterable);
133 }
134
135
136 void set length(int newLength) => _throw();
137
138 void add(E value) => _throw();
139
140 void addAll(Iterable<E> iterable) => _throw();
141
142 void insert(int index, E element) => _throw();
143
144 void insertAll(int index, Iterable<E> iterable) => _throw();
145
146 bool remove(Object value) => _throw();
147
148 E removeAt(int index) => _throw();
149
150 E removeLast() => _throw();
151
152 void removeWhere(bool test(E element)) => _throw();
153
154 void retainWhere(bool test(E element)) => _throw();
155
156 void removeRange(int start, int end) => _throw();
157
158 void replaceRange(int start, int end, Iterable<E> iterable) => _throw();
159
160 void clear() => _throw();
161 }
162
163 /**
164 * A [Set] wrapper that acts as an unmodifiable set.
165 */
166 class UnmodifiableSetView<E> extends _IterableView<E>
167 implements Set<E> {
168 Set<E> _source;
169 UnmodifiableSetView(Set<E> source) : _source = source;
170
171 void _throw() {
172 throw new UnsupportedError("Cannot modify an unmodifiable Set");
173 }
174
175 bool containsAll(Iterable<E> other) => _source.containsAll(other);
176
177 Set<E> intersection(Set<E> other) => _source.intersection(other);
178
179 Set<E> union(Set<E> other) => _source.union(other);
180
181 Set<E> difference(Set<E> other) => _source.difference(other);
182
183
184 void add(E value) => _throw();
185
186 void addAll(Iterable<E> elements) => _throw();
187
188 bool remove(Object value) => _throw();
189
190 void removeAll(Iterable elements) => _throw();
191
192 void retainAll(Iterable elements) => _throw();
193
194 void removeWhere(bool test(E element)) => _throw();
195
196 void retainWhere(bool test(E element)) => _throw();
197
198 void clear() => _throw();
199 }
200
201 /**
202 * A [Map] wrapper that acts as an unmodifiable map.
203 */
204 class UnmodifiableMapView<K, V> implements Map<K, V> {
205 Map<K, V> _source;
206 UnmodifiableMapView(Map<K, V> source) : _source = source;
207
208 static void _throw() {
209 throw new UnsupportedError("Cannot modify an unmodifiable Map");
210 }
211
212 int get length => _source.length;
213
214 bool get isEmpty => _source.isEmpty;
215
216 bool get isNotEmpty => _source.isNotEmpty;
217
218 V operator [](K key) => _source[key];
219
220 bool containsKey(K key) => _source.containsKey(key);
221
222 bool containsValue(V value) => _source.containsValue(value);
223
224 void forEach(void f(K key, V value)) => _source.forEach(f);
225
226 Iterable<K> get keys => _source.keys;
227
228 Iterable<V> get values => _source.values;
229
230
231 void operator []=(K key, V value) => _throw();
232
233 V putIfAbsent(K key, V ifAbsent()) => _throw();
234
235 void addAll(Map<K, V> other) => _throw();
236
237 V remove(K key) => _throw();
238
239 void clear() => _throw();
240 }
241
242 abstract class _IterableView<E> {
243 Iterable<E> get _source;
244
245 bool any(bool test(E element)) => _source.any(test);
246
247 bool contains(E element) => _source.contains(element);
248
249 E elementAt(int index) => _source.elementAt(index);
250
251 bool every(bool test(E element)) => _source.every(test);
252
253 Iterable expand(Iterable f(E element)) => _source.expand(f);
254
255 E get first => _source.first;
256
257 E firstWhere(bool test(E element), { E orElse() })
258 => _source.firstWhere(test, orElse: orElse);
259
260 dynamic fold(var initialValue,
261 dynamic combine(var previousValue, E element))
262 => _source.fold(initialValue, combine);
263
264 void forEach(void f(E element)) => _source.forEach(f);
265
266 bool get isEmpty => _source.isEmpty;
267
268 bool get isNotEmpty => _source.isNotEmpty;
269
270 Iterator<E> get iterator => _source.iterator;
271
272 String join([String separator = ""]) => _source.join(separator);
273
274 E get last => _source.last;
275
276 E lastWhere(bool test(E element), {E orElse()})
277 => _source.lastWhere(test, orElse: orElse);
278
279 int get length => _source.length;
280
281 Iterable map(f(E element)) => _source.map(f);
282
283 E reduce(E combine(E value, E element)) => _source.reduce(combine);
284
285 E get single => _source.single;
286
287 E singleWhere(bool test(E element)) => _source.singleWhere(test);
288
289 Iterable<E> skip(int n) => _source.skip(n);
290
291 Iterable<E> skipWhile(bool test(E value)) => _source.skipWhile(test);
292
293 Iterable<E> take(int n) => _source.take(n);
294
295 Iterable<E> takeWhile(bool test(E value)) => _source.takeWhile(test);
296
297 List<E> toList({ bool growable: true }) => _source.toList(growable: growable);
298
299 Set<E> toSet() => _source.toSet();
300
301 Iterable<E> where(bool test(E element)) => _source.where(test);
302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698