OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 import "dart:collection"; | |
6 import "dart:math" as math; | |
7 | |
8 import "wrappers.dart"; | |
9 | |
10 typedef F _UnaryFunction<E, F>(E argument); | |
11 | |
12 /// The base class for delegating, type-asserting iterables. | |
13 /// | |
14 /// Subclasses can provide a [_base] that should be delegated to. Unlike | |
15 /// [TypedIterable], this allows the base to be created on demand. | |
16 abstract class _TypedIterableBase<E> implements Iterable<E> { | |
17 /// The base iterable to which operations are delegated. | |
18 Iterable get _base; | |
19 | |
20 const _TypedIterableBase(); | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Don't make a const constructor unless you intend t
nweiz
2016/03/29 20:18:05
Done.
| |
21 | |
22 bool any(bool test(E element)) => _base.any(_assert(test)); | |
23 | |
24 bool contains(Object element) => _base.contains(element); | |
25 | |
26 E elementAt(int index) => _base.elementAt(index) as E; | |
27 | |
28 bool every(bool test(E element)) => _base.every(_assert(test)); | |
29 | |
30 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => _base.expand(_a ssert(f)); | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Long line
nweiz
2016/03/29 20:18:05
Done.
| |
31 | |
32 E get first => _base.first as E; | |
33 | |
34 E firstWhere(bool test(E element), {E orElse()}) => | |
35 _base.firstWhere(_assert(test), orElse: orElse) as E; | |
36 | |
37 /*=T*/ fold/*<T>*/( | |
38 /*=T*/ initialValue, | |
39 /*=T*/ combine(/*=T*/ previousValue, E element)) => | |
40 _base.fold(initialValue, | |
41 (previousValue, element) => combine(previousValue, element as E)); | |
42 | |
43 void forEach(void f(E element)) => _base.forEach(_assert(f)); | |
44 | |
45 bool get isEmpty => _base.isEmpty; | |
46 | |
47 bool get isNotEmpty => _base.isNotEmpty; | |
48 | |
49 Iterator<E> get iterator => _base.map((element) => element as E).iterator; | |
50 | |
51 String join([String separator = ""]) => _base.join(separator); | |
52 | |
53 E get last => _base.last as E; | |
54 | |
55 E lastWhere(bool test(E element), {E orElse()}) => | |
56 _base.lastWhere(_assert(test), orElse: orElse) as E; | |
57 | |
58 int get length => _base.length; | |
59 | |
60 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => _base.map(_assert(f)); | |
61 | |
62 E reduce(E combine(E value, E element)) => | |
63 _base.reduce((value, element) => combine(value as E, element as E)) as E; | |
64 | |
65 E get single => _base.single as E; | |
66 | |
67 E singleWhere(bool test(E element)) => _base.singleWhere(_assert(test)) as E; | |
68 | |
69 Iterable<E> skip(int n) => new TypedIterable<E>(_base.skip(n)); | |
70 | |
71 Iterable<E> skipWhile(bool test(E value)) => | |
72 new TypedIterable<E>(_base.skipWhile(_assert(test))); | |
73 | |
74 Iterable<E> take(int n) => new TypedIterable<E>(_base.take(n)); | |
75 | |
76 Iterable<E> takeWhile(bool test(E value)) => | |
77 new TypedIterable<E>(_base.takeWhile(_assert(test))); | |
78 | |
79 List<E> toList({bool growable: true}) => | |
80 new TypedList<E>(_base.toList(growable: growable)); | |
81 | |
82 Set<E> toSet() => new TypedSet<E>(_base.toSet()); | |
83 | |
84 Iterable<E> where(bool test(E element)) => | |
85 new TypedIterable<E>(_base.where(_assert(test))); | |
86 | |
87 String toString() => _base.toString(); | |
88 | |
89 /// Returns a version of [function] that asserts that its argument is an | |
90 /// instance of `E`. | |
91 _UnaryFunction/*<dynamic, F>*/ _assert/*<F>*/( | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Consider renaming to _validate or _ensureType. The
nweiz
2016/03/29 20:18:05
Done.
| |
92 _UnaryFunction/*<E, F>*/ function) => | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Just (F function(E argument)), no need for the typ
nweiz
2016/03/29 20:18:05
Done.
| |
93 (value) => function(value as E); | |
94 } | |
95 | |
96 /// An [Iterable] that asserts the types of values in a base iterable. | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Consider referring to where the class is used from
nweiz
2016/03/29 20:18:05
Done.
| |
97 class TypedIterable<E> extends _TypedIterableBase<E> | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
The name TypedIterable is a little too close to th
nweiz
2016/03/29 20:18:05
Done.
| |
98 implements DelegatingIterable<E> { | |
99 final Iterable _base; | |
100 | |
101 const TypedIterable(Iterable base) : _base = base; | |
102 } | |
103 | |
104 /// A [List] that asserts the types of values in a base list. | |
105 class TypedList<E> extends TypedIterable<E> implements DelegatingList<E> { | |
106 const TypedList(List base) : super(base); | |
107 | |
108 /// A [List]-typed getter for [_base]. | |
109 List get _listBase => _base; | |
110 | |
111 E operator [](int index) => _listBase[index] as E; | |
112 | |
113 void operator []=(int index, E value) { | |
114 _listBase[index] = value; | |
115 } | |
116 | |
117 void add(E value) { | |
118 _listBase.add(value); | |
119 } | |
120 | |
121 void addAll(Iterable<E> iterable) { | |
122 _listBase.addAll(iterable); | |
123 } | |
124 | |
125 Map<int, E> asMap() => new TypedMap<int, E>(_listBase.asMap()); | |
126 | |
127 void clear() { | |
128 _listBase.clear(); | |
129 } | |
130 | |
131 void fillRange(int start, int end, [E fillValue]) { | |
132 _listBase.fillRange(start, end, fillValue); | |
133 } | |
134 | |
135 Iterable<E> getRange(int start, int end) => | |
136 new TypedIterable<E>(_listBase.getRange(start, end)); | |
137 | |
138 int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start); | |
139 | |
140 void insert(int index, E element) { | |
141 _listBase.insert(index, element); | |
142 } | |
143 | |
144 void insertAll(int index, Iterable<E> iterable) { | |
Lasse Reichstein Nielsen
2016/03/29 12:56:24
Does this mean that the input iterable isn't check
nweiz
2016/03/29 20:18:05
In strong mode, I believe that's not possible. Oth
| |
145 _listBase.insertAll(index, iterable); | |
146 } | |
147 | |
148 int lastIndexOf(E element, [int start]) => | |
149 _listBase.lastIndexOf(element, start); | |
150 | |
151 void set length(int newLength) { | |
152 _listBase.length = newLength; | |
153 } | |
154 | |
155 bool remove(Object value) => _listBase.remove(value); | |
156 | |
157 E removeAt(int index) => _listBase.removeAt(index) as E; | |
158 | |
159 E removeLast() => _listBase.removeLast() as E; | |
160 | |
161 void removeRange(int start, int end) { | |
162 _listBase.removeRange(start, end); | |
163 } | |
164 | |
165 void removeWhere(bool test(E element)) { | |
166 _listBase.removeWhere(_assert(test)); | |
167 } | |
168 | |
169 void replaceRange(int start, int end, Iterable<E> iterable) { | |
170 _listBase.replaceRange(start, end, iterable); | |
171 } | |
172 | |
173 void retainWhere(bool test(E element)) { | |
174 _listBase.retainWhere(_assert(test)); | |
175 } | |
176 | |
177 Iterable<E> get reversed => new TypedIterable<E>(_listBase.reversed); | |
178 | |
179 void setAll(int index, Iterable<E> iterable) { | |
180 _listBase.setAll(index, iterable); | |
181 } | |
182 | |
183 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { | |
184 _listBase.setRange(start, end, iterable, skipCount); | |
185 } | |
186 | |
187 void shuffle([math.Random random]) { | |
188 _listBase.shuffle(random); | |
189 } | |
190 | |
191 void sort([int compare(E a, E b)]) { | |
192 if (compare == null) { | |
193 _listBase.sort(); | |
194 } else { | |
195 _listBase.sort((a, b) => compare(a as E, b as E)); | |
196 } | |
197 } | |
198 | |
199 List<E> sublist(int start, [int end]) => | |
200 new TypedList<E>(_listBase.sublist(start, end)); | |
201 } | |
202 | |
203 /// A [Set] that asserts the types of values in a base set. | |
204 class TypedSet<E> extends TypedIterable<E> implements DelegatingSet<E> { | |
205 const TypedSet(Set base) : super(base); | |
206 | |
207 /// A [Set]-typed getter for [_base]. | |
208 Set get _setBase => _base; | |
209 | |
210 bool add(E value) => _setBase.add(value); | |
211 | |
212 void addAll(Iterable<E> elements) { | |
213 _setBase.addAll(elements); | |
214 } | |
215 | |
216 void clear() { | |
217 _setBase.clear(); | |
218 } | |
219 | |
220 bool containsAll(Iterable<Object> other) => _setBase.containsAll(other); | |
221 | |
222 Set<E> difference(Set<E> other) => | |
223 new TypedSet<E>(_setBase.difference(other)); | |
224 | |
225 Set<E> intersection(Set<Object> other) => | |
226 new TypedSet<E>(_setBase.intersection(other)); | |
227 | |
228 E lookup(Object element) => _setBase.lookup(element) as E; | |
229 | |
230 bool remove(Object value) => _setBase.remove(value); | |
231 | |
232 void removeAll(Iterable<Object> elements) { | |
233 _setBase.removeAll(elements); | |
234 } | |
235 | |
236 void removeWhere(bool test(E element)) { | |
237 _setBase.removeWhere(_assert(test)); | |
238 } | |
239 | |
240 void retainAll(Iterable<Object> elements) { | |
241 _setBase.retainAll(elements); | |
242 } | |
243 | |
244 void retainWhere(bool test(E element)) { | |
245 _setBase.retainWhere(_assert(test)); | |
246 } | |
247 | |
248 Set<E> union(Set<E> other) => new TypedSet<E>(_setBase.union(other)); | |
249 } | |
250 | |
251 /// A [Queue] that asserts the types of values in a base queue. | |
252 class TypedQueue<E> extends TypedIterable<E> implements DelegatingQueue<E> { | |
253 const TypedQueue(Queue queue) : super(queue); | |
254 | |
255 /// A [Queue]-typed getter for [_base]. | |
256 Queue get _baseQueue => _base; | |
257 | |
258 void add(E value) { | |
259 _baseQueue.add(value); | |
260 } | |
261 | |
262 void addAll(Iterable<E> iterable) { | |
263 _baseQueue.addAll(iterable); | |
264 } | |
265 | |
266 void addFirst(E value) { | |
267 _baseQueue.addFirst(value); | |
268 } | |
269 | |
270 void addLast(E value) { | |
271 _baseQueue.addLast(value); | |
272 } | |
273 | |
274 void clear() { | |
275 _baseQueue.clear(); | |
276 } | |
277 | |
278 bool remove(Object object) => _baseQueue.remove(object); | |
279 | |
280 void removeWhere(bool test(E element)) { | |
281 _baseQueue.removeWhere(_assert(test)); | |
282 } | |
283 | |
284 void retainWhere(bool test(E element)) { | |
285 _baseQueue.retainWhere(_assert(test)); | |
286 } | |
287 | |
288 E removeFirst() => _baseQueue.removeFirst() as E; | |
289 | |
290 E removeLast() => _baseQueue.removeLast() as E; | |
291 } | |
292 | |
293 /// A [Map] that asserts the types of keys and values in a base map. | |
294 class TypedMap<K, V> implements DelegatingMap<K, V> { | |
295 /// The base map to which operations are delegated. | |
296 final Map _base; | |
297 | |
298 const TypedMap(Map base) : _base = base; | |
299 | |
300 V operator [](Object key) => _base[key] as V; | |
301 | |
302 void operator []=(K key, V value) { | |
303 _base[key] = value; | |
304 } | |
305 | |
306 void addAll(Map<K, V> other) { | |
307 _base.addAll(other); | |
308 } | |
309 | |
310 void clear() { | |
311 _base.clear(); | |
312 } | |
313 | |
314 bool containsKey(Object key) => _base.containsKey(key); | |
315 | |
316 bool containsValue(Object value) => _base.containsValue(value); | |
317 | |
318 void forEach(void f(K key, V value)) { | |
319 _base.forEach((key, value) => f(key as K, value as V)); | |
320 } | |
321 | |
322 bool get isEmpty => _base.isEmpty; | |
323 | |
324 bool get isNotEmpty => _base.isNotEmpty; | |
325 | |
326 Iterable<K> get keys => new TypedIterable<K>(_base.keys); | |
327 | |
328 int get length => _base.length; | |
329 | |
330 V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent) as V; | |
331 | |
332 V remove(Object key) => _base.remove(key) as V; | |
333 | |
334 Iterable<V> get values => new TypedIterable<V>(_base.values); | |
335 | |
336 String toString() => _base.toString(); | |
337 } | |
OLD | NEW |