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