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