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