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