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