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