OLD | NEW |
| (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 * Wrappers that prevent a List, Set, or Map object from being modified. | |
7 * | |
8 * The [Set] and [Map] wrappers allow reading from the wrapped collection, | |
9 * but prohibit writing. | |
10 * | |
11 * The [List] wrapper prevents changes to the length of the wrapped list, | |
12 * but allows changes to the contents. | |
13 */ | |
14 library collection.unmodifiable_wrappers; | |
15 | |
16 import '../wrappers.dart'; | |
17 | |
18 export "dart:collection" show UnmodifiableListView, UnmodifiableMapView; | |
19 | |
20 /** | |
21 * A fixed-length list. | |
22 * | |
23 * A `NonGrowableListView` contains a [List] object and ensures that | |
24 * its length does not change. | |
25 * Methods that would change the length of the list, | |
26 * such as [add] and [remove], throw an [UnsupportedError]. | |
27 * All other methods work directly on the underlying list. | |
28 * | |
29 * This class _does_ allow changes to the contents of the wrapped list. | |
30 * You can, for example, [sort] the list. | |
31 * Permitted operations defer to the wrapped list. | |
32 */ | |
33 class NonGrowableListView<E> extends DelegatingList<E> | |
34 with NonGrowableListMixin<E> { | |
35 NonGrowableListView(List<E> listBase) : super(listBase); | |
36 } | |
37 | |
38 /** | |
39 * Mixin class that implements a throwing version of all list operations that | |
40 * change the List's length. | |
41 */ | |
42 abstract class NonGrowableListMixin<E> implements List<E> { | |
43 static _throw() { | |
44 throw new UnsupportedError( | |
45 "Cannot change the length of a fixed-length list"); | |
46 } | |
47 | |
48 /** | |
49 * Throws an [UnsupportedError]; | |
50 * operations that change the length of the list are disallowed. | |
51 */ | |
52 void set length(int newLength) => _throw(); | |
53 | |
54 /** | |
55 * Throws an [UnsupportedError]; | |
56 * operations that change the length of the list are disallowed. | |
57 */ | |
58 bool add(E value) => _throw(); | |
59 | |
60 /** | |
61 * Throws an [UnsupportedError]; | |
62 * operations that change the length of the list are disallowed. | |
63 */ | |
64 void addAll(Iterable<E> iterable) => _throw(); | |
65 | |
66 /** | |
67 * Throws an [UnsupportedError]; | |
68 * operations that change the length of the list are disallowed. | |
69 */ | |
70 void insert(int index, E element) => _throw(); | |
71 | |
72 /** | |
73 * Throws an [UnsupportedError]; | |
74 * operations that change the length of the list are disallowed. | |
75 */ | |
76 void insertAll(int index, Iterable<E> iterable) => _throw(); | |
77 | |
78 /** | |
79 * Throws an [UnsupportedError]; | |
80 * operations that change the length of the list are disallowed. | |
81 */ | |
82 bool remove(Object value) => _throw(); | |
83 | |
84 /** | |
85 * Throws an [UnsupportedError]; | |
86 * operations that change the length of the list are disallowed. | |
87 */ | |
88 E removeAt(int index) => _throw(); | |
89 | |
90 /** | |
91 * Throws an [UnsupportedError]; | |
92 * operations that change the length of the list are disallowed. | |
93 */ | |
94 E removeLast() => _throw(); | |
95 | |
96 /** | |
97 * Throws an [UnsupportedError]; | |
98 * operations that change the length of the list are disallowed. | |
99 */ | |
100 void removeWhere(bool test(E element)) => _throw(); | |
101 | |
102 /** | |
103 * Throws an [UnsupportedError]; | |
104 * operations that change the length of the list are disallowed. | |
105 */ | |
106 void retainWhere(bool test(E element)) => _throw(); | |
107 | |
108 /** | |
109 * Throws an [UnsupportedError]; | |
110 * operations that change the length of the list are disallowed. | |
111 */ | |
112 void removeRange(int start, int end) => _throw(); | |
113 | |
114 /** | |
115 * Throws an [UnsupportedError]; | |
116 * operations that change the length of the list are disallowed. | |
117 */ | |
118 void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); | |
119 | |
120 /** | |
121 * Throws an [UnsupportedError]; | |
122 * operations that change the length of the list are disallowed. | |
123 */ | |
124 void clear() => _throw(); | |
125 } | |
126 | |
127 /** | |
128 * An unmodifiable set. | |
129 * | |
130 * An UnmodifiableSetView contains a [Set] object and ensures | |
131 * that it does not change. | |
132 * Methods that would change the set, | |
133 * such as [add] and [remove], throw an [UnsupportedError]. | |
134 * Permitted operations defer to the wrapped set. | |
135 */ | |
136 class UnmodifiableSetView<E> extends DelegatingSet<E> | |
137 with UnmodifiableSetMixin<E> { | |
138 UnmodifiableSetView(Set<E> setBase) : super(setBase); | |
139 } | |
140 | |
141 /** | |
142 * Mixin class that implements a throwing version of all set operations that | |
143 * change the Set. | |
144 */ | |
145 abstract class UnmodifiableSetMixin<E> implements Set<E> { | |
146 _throw() { | |
147 throw new UnsupportedError("Cannot modify an unmodifiable Set"); | |
148 } | |
149 | |
150 /** | |
151 * Throws an [UnsupportedError]; | |
152 * operations that change the set are disallowed. | |
153 */ | |
154 bool add(E value) => _throw(); | |
155 | |
156 /** | |
157 * Throws an [UnsupportedError]; | |
158 * operations that change the set are disallowed. | |
159 */ | |
160 void addAll(Iterable<E> elements) => _throw(); | |
161 | |
162 /** | |
163 * Throws an [UnsupportedError]; | |
164 * operations that change the set are disallowed. | |
165 */ | |
166 bool remove(Object value) => _throw(); | |
167 | |
168 /** | |
169 * Throws an [UnsupportedError]; | |
170 * operations that change the set are disallowed. | |
171 */ | |
172 void removeAll(Iterable elements) => _throw(); | |
173 | |
174 /** | |
175 * Throws an [UnsupportedError]; | |
176 * operations that change the set are disallowed. | |
177 */ | |
178 void retainAll(Iterable elements) => _throw(); | |
179 | |
180 /** | |
181 * Throws an [UnsupportedError]; | |
182 * operations that change the set are disallowed. | |
183 */ | |
184 void removeWhere(bool test(E element)) => _throw(); | |
185 | |
186 /** | |
187 * Throws an [UnsupportedError]; | |
188 * operations that change the set are disallowed. | |
189 */ | |
190 void retainWhere(bool test(E element)) => _throw(); | |
191 | |
192 /** | |
193 * Throws an [UnsupportedError]; | |
194 * operations that change the set are disallowed. | |
195 */ | |
196 void clear() => _throw(); | |
197 } | |
198 | |
199 /** | |
200 * Mixin class that implements a throwing version of all map operations that | |
201 * change the Map. | |
202 */ | |
203 abstract class UnmodifiableMapMixin<K, V> implements Map<K, V> { | |
204 static _throw() { | |
205 throw new UnsupportedError("Cannot modify an unmodifiable Map"); | |
206 } | |
207 | |
208 /** | |
209 * Throws an [UnsupportedError]; | |
210 * operations that change the map are disallowed. | |
211 */ | |
212 void operator []=(K key, V value) => _throw(); | |
213 | |
214 /** | |
215 * Throws an [UnsupportedError]; | |
216 * operations that change the map are disallowed. | |
217 */ | |
218 V putIfAbsent(K key, V ifAbsent()) => _throw(); | |
219 | |
220 /** | |
221 * Throws an [UnsupportedError]; | |
222 * operations that change the map are disallowed. | |
223 */ | |
224 void addAll(Map<K, V> other) => _throw(); | |
225 | |
226 /** | |
227 * Throws an [UnsupportedError]; | |
228 * operations that change the map are disallowed. | |
229 */ | |
230 V remove(Object key) => _throw(); | |
231 | |
232 /** | |
233 * Throws an [UnsupportedError]; | |
234 * operations that change the map are disallowed. | |
235 */ | |
236 void clear() => _throw(); | |
237 } | |
OLD | NEW |