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 : import 'empty_unmodifiable_set.dart'; 6 : import 'wrappers.dart'; 7 : 8 : export 'dart:collection' show UnmodifiableListView, UnmodifiableMapView; 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 0 : static Never _throw() { 30 0 : throw UnsupportedError('Cannot change the length of a fixed-length list'); 31 : } 32 : 33 : /// Throws an [UnsupportedError]; 34 : /// operations that change the length of the list are disallowed. 35 0 : @override 36 0 : set length(int newLength) => _throw(); 37 : 38 : /// Throws an [UnsupportedError]; 39 : /// operations that change the length of the list are disallowed. 40 0 : @override 41 0 : bool add(E value) => _throw(); 42 : 43 : /// Throws an [UnsupportedError]; 44 : /// operations that change the length of the list are disallowed. 45 0 : @override 46 0 : void addAll(Iterable<E> iterable) => _throw(); 47 : 48 : /// Throws an [UnsupportedError]; 49 : /// operations that change the length of the list are disallowed. 50 0 : @override 51 0 : void insert(int index, E element) => _throw(); 52 : 53 : /// Throws an [UnsupportedError]; 54 : /// operations that change the length of the list are disallowed. 55 0 : @override 56 0 : void insertAll(int index, Iterable<E> iterable) => _throw(); 57 : 58 : /// Throws an [UnsupportedError]; 59 : /// operations that change the length of the list are disallowed. 60 0 : @override 61 0 : bool remove(Object? value) => _throw(); 62 : 63 : /// Throws an [UnsupportedError]; 64 : /// operations that change the length of the list are disallowed. 65 0 : @override 66 0 : E removeAt(int index) => _throw(); 67 : 68 : /// Throws an [UnsupportedError]; 69 : /// operations that change the length of the list are disallowed. 70 0 : @override 71 0 : E removeLast() => _throw(); 72 : 73 : /// Throws an [UnsupportedError]; 74 : /// operations that change the length of the list are disallowed. 75 0 : @override 76 0 : void removeWhere(bool Function(E) test) => _throw(); 77 : 78 : /// Throws an [UnsupportedError]; 79 : /// operations that change the length of the list are disallowed. 80 0 : @override 81 0 : void retainWhere(bool Function(E) test) => _throw(); 82 : 83 : /// Throws an [UnsupportedError]; 84 : /// operations that change the length of the list are disallowed. 85 0 : @override 86 0 : void removeRange(int start, int end) => _throw(); 87 : 88 : /// Throws an [UnsupportedError]; 89 : /// operations that change the length of the list are disallowed. 90 0 : @override 91 0 : void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); 92 : 93 : /// Throws an [UnsupportedError]; 94 : /// operations that change the length of the list are disallowed. 95 0 : @override 96 0 : void clear() => _throw(); 97 : } 98 : 99 : /// An unmodifiable set. 100 : /// 101 : /// An [UnmodifiableSetView] contains a [Set], 102 : /// and prevents that set from being changed through the view. 103 : /// Methods that could change the set, 104 : /// such as [add] and [remove], throw an [UnsupportedError]. 105 : /// Permitted operations defer to the wrapped set. 106 : class UnmodifiableSetView<E> extends DelegatingSet<E> 107 : with UnmodifiableSetMixin<E> { 108 22 : UnmodifiableSetView(Set<E> setBase) : super(setBase); 109 : 110 : /// An unmodifiable empty set. 111 : /// 112 : /// This is the same as `UnmodifiableSetView(Set())`, except that it 113 : /// can be used in const contexts. 114 : const factory UnmodifiableSetView.empty() = EmptyUnmodifiableSet<E>; 115 : } 116 : 117 : /// Mixin class that implements a throwing version of all set operations that 118 : /// change the Set. 119 : abstract class UnmodifiableSetMixin<E> implements Set<E> { 120 0 : static Never _throw() { 121 0 : throw UnsupportedError('Cannot modify an unmodifiable Set'); 122 : } 123 : 124 : /// Throws an [UnsupportedError]; 125 : /// operations that change the set are disallowed. 126 0 : @override 127 0 : bool add(E value) => _throw(); 128 : 129 : /// Throws an [UnsupportedError]; 130 : /// operations that change the set are disallowed. 131 0 : @override 132 0 : void addAll(Iterable<E> elements) => _throw(); 133 : 134 : /// Throws an [UnsupportedError]; 135 : /// operations that change the set are disallowed. 136 0 : @override 137 0 : bool remove(Object? value) => _throw(); 138 : 139 : /// Throws an [UnsupportedError]; 140 : /// operations that change the set are disallowed. 141 0 : @override 142 0 : void removeAll(Iterable elements) => _throw(); 143 : 144 : /// Throws an [UnsupportedError]; 145 : /// operations that change the set are disallowed. 146 0 : @override 147 0 : void retainAll(Iterable elements) => _throw(); 148 : 149 : /// Throws an [UnsupportedError]; 150 : /// operations that change the set are disallowed. 151 0 : @override 152 0 : void removeWhere(bool Function(E) test) => _throw(); 153 : 154 : /// Throws an [UnsupportedError]; 155 : /// operations that change the set are disallowed. 156 0 : @override 157 0 : void retainWhere(bool Function(E) test) => _throw(); 158 : 159 : /// Throws an [UnsupportedError]; 160 : /// operations that change the set are disallowed. 161 0 : @override 162 0 : void clear() => _throw(); 163 : } 164 : 165 : /// Mixin class that implements a throwing version of all map operations that 166 : /// change the Map. 167 : abstract class UnmodifiableMapMixin<K, V> implements Map<K, V> { 168 0 : static Never _throw() { 169 0 : throw UnsupportedError('Cannot modify an unmodifiable Map'); 170 : } 171 : 172 : /// Throws an [UnsupportedError]; 173 : /// operations that change the map are disallowed. 174 0 : @override 175 0 : void operator []=(K key, V value) => _throw(); 176 : 177 : /// Throws an [UnsupportedError]; 178 : /// operations that change the map are disallowed. 179 0 : @override 180 0 : V putIfAbsent(K key, V Function() ifAbsent) => _throw(); 181 : 182 : /// Throws an [UnsupportedError]; 183 : /// operations that change the map are disallowed. 184 0 : @override 185 0 : void addAll(Map<K, V> other) => _throw(); 186 : 187 : /// Throws an [UnsupportedError]; 188 : /// operations that change the map are disallowed. 189 0 : @override 190 0 : V remove(Object? key) => _throw(); 191 : 192 : /// Throws an [UnsupportedError]; 193 : /// operations that change the map are disallowed. 194 0 : @override 195 0 : void clear() => _throw(); 196 : 197 : /// Throws an [UnsupportedError]; 198 : /// operations that change the map are disallowed. 199 0 : set first(_) => _throw(); 200 : 201 : /// Throws an [UnsupportedError]; 202 : /// operations that change the map are disallowed. 203 0 : set last(_) => _throw(); 204 : }