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