Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadOnlyArrayList |
|
| 0.0;0 |
1 | /* | |
2 | * Licensed under the MIT License | |
3 | * | |
4 | * Copyright 2010 (c) Flávio Silva, http://flsilva.com | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person | |
7 | * obtaining a copy of this software and associated documentation | |
8 | * files (the "Software"), to deal in the Software without | |
9 | * restriction, including without limitation the rights to use, | |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the | |
12 | * Software is furnished to do so, subject to the following | |
13 | * conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be | |
16 | * included in all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
25 | * OTHER DEALINGS IN THE SOFTWARE. | |
26 | * | |
27 | * http://www.opensource.org/licenses/mit-license.php | |
28 | */ | |
29 | ||
30 | 1 | package org.as3collections.lists |
31 | { | |
32 | import org.as3collections.AbstractList; | |
33 | import org.as3collections.ICollection; | |
34 | import org.as3collections.IIterator; | |
35 | import org.as3collections.IList; | |
36 | import org.as3collections.IListIterator; | |
37 | import org.as3collections.iterators.ReadOnlyArrayIterator; | |
38 | import org.as3collections.iterators.ReadOnlyListIterator; | |
39 | import org.as3coreaddendum.errors.UnsupportedOperationError; | |
40 | import org.as3utils.ReflectionUtil; | |
41 | ||
42 | /** | |
43 | * A list that does not allow modifications. | |
44 | * It receives all the elements by its constructor and can no longer be changed. | |
45 | * All methods that change this list will throw an <code>org.as3coreaddendum.errors.UnsupportedOperationError</code>. | |
46 | * | |
47 | * @example | |
48 | * | |
49 | * <listing version="3.0"> | |
50 | * import org.as3collections.IList; | |
51 | * import org.as3collections.lists.ArrayList; | |
52 | * import org.as3collections.lists.ReadOnlyArrayList; | |
53 | * | |
54 | * var list1:IList = new ArrayList([3, 5, 1, 7]); | |
55 | * | |
56 | * list1 // [3,5,1,7] | |
57 | * | |
58 | * var list2:IList = new ReadOnlyArrayList(list1.toArray()); | |
59 | * | |
60 | * list2 // [3,5,1,7] | |
61 | * | |
62 | * list2.add(1) // UnsupportedOperationError: ReadOnlyArrayList is a read-only list and doesn't allow modifications. | |
63 | * list2.remove(1) // UnsupportedOperationError: ReadOnlyArrayList is a read-only list and doesn't allow modifications. | |
64 | * | |
65 | * var list3:IList = list2.clone(); | |
66 | * | |
67 | * list3 // [3,5,1,7] | |
68 | * | |
69 | * list3.contains(2) // false | |
70 | * list3.contains(5) // true | |
71 | * list3.indexOf(5) // 1 | |
72 | * list3.containsAll(list1) // true | |
73 | * list3.equals(list1) // false | |
74 | * list3.getAt(2) // 1 | |
75 | * list3.subList(1, 3) // [5,1] | |
76 | * | |
77 | * list3.addAll(list2) // UnsupportedOperationError: ReadOnlyArrayList is a read-only list and doesn't allow modifications. | |
78 | * list3.removeRange(1, 3) // UnsupportedOperationError: ReadOnlyArrayList is a read-only list and doesn't allow modifications. | |
79 | * | |
80 | * var it:IIterator = list3.iterator(); | |
81 | * | |
82 | * while (it.hasNext()) | |
83 | * { | |
84 | * it.next() | |
85 | * | |
86 | * it.remove() // UnsupportedOperationError: ReadOnlyArrayIterator is a read-only iterator and doesn't allow modifications in the collection. | |
87 | * } | |
88 | * | |
89 | * var it2:IListIterator = list3.listIterator(); | |
90 | * | |
91 | * while (it2.hasNext()) | |
92 | * { | |
93 | * it2.next() | |
94 | * | |
95 | * it.add(1) // UnsupportedOperationError: ReadOnlyListIterator is a read-only iterator and doesn't allow modifications in the list. | |
96 | * } | |
97 | * </listing> | |
98 | * | |
99 | * @author Flávio Silva | |
100 | */ | |
101 | public class ReadOnlyArrayList extends AbstractList | |
102 | { | |
103 | /** | |
104 | * Constructor, creates a new <code>ReadOnlyArrayList</code> object. | |
105 | * | |
106 | * @param source an array to fill the list. | |
107 | * @throws ArgumentError if the <code>source</code> argument is <code>null</code>. | |
108 | */ | |
109 | public function ReadOnlyArrayList(source:Array) | |
110 | { | |
111 | 1 | super(source); |
112 | 1 | if (!source) throw new ArgumentError ("The 'source' argument must not be 'null'."); |
113 | 1 | } |
114 | ||
115 | /** | |
116 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
117 | * | |
118 | * @param element | |
119 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
120 | * @return | |
121 | */ | |
122 | override public function add(element:*): Boolean | |
123 | { | |
124 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
125 | } | |
126 | ||
127 | /** | |
128 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
129 | * | |
130 | * @param collection | |
131 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
132 | * @return | |
133 | */ | |
134 | override public function addAll(collection:ICollection): Boolean | |
135 | { | |
136 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
137 | } | |
138 | ||
139 | /** | |
140 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
141 | * | |
142 | * @param index | |
143 | * @param collection | |
144 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
145 | * @return | |
146 | */ | |
147 | override public function addAllAt(index:int, collection:ICollection): Boolean | |
148 | { | |
149 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
150 | } | |
151 | ||
152 | /** | |
153 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
154 | * | |
155 | * @param index | |
156 | * @param element | |
157 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
158 | * @return | |
159 | */ | |
160 | override public function addAt(index:int, element:*): Boolean | |
161 | { | |
162 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
163 | } | |
164 | ||
165 | /** | |
166 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
167 | * | |
168 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
169 | */ | |
170 | override public function clear(): void | |
171 | { | |
172 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
173 | } | |
174 | ||
175 | /** | |
176 | * Creates and return a new <code>ReadOnlyArrayList</code> object containing all elements in this list (in the same order). | |
177 | * | |
178 | * @return a new <code>ReadOnlyArrayList</code> object containing all elements in this list (in the same order). | |
179 | */ | |
180 | override public function clone(): * | |
181 | { | |
182 | 1 | return new ReadOnlyArrayList(data); |
183 | } | |
184 | ||
185 | /** | |
186 | * Returns an iterator over a set of elements. | |
187 | * <p>This implementation returns a <code>ReadOnlyArrayIterator</code> object.</p> | |
188 | * | |
189 | * @return an iterator over a set of elements. | |
190 | * @see org.as3collections.iterators.ReadOnlyArrayIterator ReadOnlyArrayIterator | |
191 | */ | |
192 | override public function iterator(): IIterator | |
193 | { | |
194 | 1 | return new ReadOnlyArrayIterator(data); |
195 | } | |
196 | ||
197 | /** | |
198 | * Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to <code>next</code>. An initial call to <code>previous</code> would return the element with the specified index minus one. | |
199 | * <p>This implementation returns a <code>ReadOnlyListIterator</code> object.</p> | |
200 | * | |
201 | * @param index index of first element to be returned from the list iterator (by a call to the <code>next</code> method) | |
202 | * @return a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
203 | * @see org.as3collections.iterators.ReadOnlyListIterator ReadOnlyListIterator | |
204 | */ | |
205 | override public function listIterator(index:int = 0): IListIterator | |
206 | { | |
207 | 1 | return new ReadOnlyListIterator(this, index); |
208 | } | |
209 | ||
210 | /** | |
211 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
212 | * | |
213 | * @param o | |
214 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
215 | * @return | |
216 | */ | |
217 | override public function remove(o:*): Boolean | |
218 | { | |
219 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
220 | } | |
221 | ||
222 | /** | |
223 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
224 | * | |
225 | * @param collection | |
226 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
227 | * @return | |
228 | */ | |
229 | override public function removeAll(collection:ICollection): Boolean | |
230 | { | |
231 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
232 | } | |
233 | ||
234 | /** | |
235 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
236 | * | |
237 | * @param index | |
238 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
239 | * @return | |
240 | */ | |
241 | override public function removeAt(index:int): * | |
242 | { | |
243 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
244 | } | |
245 | ||
246 | /** | |
247 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
248 | * | |
249 | * @param fromIndex | |
250 | * @param toIndex | |
251 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
252 | * @return | |
253 | */ | |
254 | override public function removeRange(fromIndex:int, toIndex:int): ICollection | |
255 | { | |
256 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
257 | } | |
258 | ||
259 | /** | |
260 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
261 | * | |
262 | * @param collection | |
263 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
264 | * @return | |
265 | */ | |
266 | override public function retainAll(collection:ICollection): Boolean | |
267 | { | |
268 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
269 | } | |
270 | ||
271 | /** | |
272 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
273 | * | |
274 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
275 | * @return | |
276 | */ | |
277 | override public function reverse(): void | |
278 | { | |
279 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
280 | } | |
281 | ||
282 | /** | |
283 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
284 | * | |
285 | * @param index | |
286 | * @param element | |
287 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyArrayList</code> is a read-only list and doesn't allow modifications. | |
288 | * @return | |
289 | */ | |
290 | override public function setAt(index:int, element:*): * | |
291 | { | |
292 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only list and doesn't allow modifications."); |
293 | } | |
294 | ||
295 | /** | |
296 | * Returns a new <code>ReadOnlyArrayList</code> that is a view of the portion of this <code>ReadOnlyArrayList</code> between the specified <code>fromIndex</code>, inclusive, and <code>toIndex</code>, exclusive. | |
297 | * | |
298 | * @param fromIndex the index to start retrieving elements (inclusive). | |
299 | * @param toIndex the index to stop retrieving elements (exclusive). | |
300 | * @throws org.as3collections.errors.IndexOutOfBoundsError if <code>fromIndex</code> or <code>toIndex</code> is out of range <code>(index < 0 || index > size())</code>. | |
301 | * @return a new <code>ReadOnlyArrayList</code> that is a view of the specified range within this list. | |
302 | */ | |
303 | override public function subList(fromIndex:int, toIndex:int): IList | |
304 | { | |
305 | 1 | checkIndex(fromIndex, size()); |
306 | 1 | checkIndex(toIndex, size()); |
307 | 1 | return new ReadOnlyArrayList(data.slice(fromIndex, toIndex)); |
308 | } | |
309 | ||
310 | } | |
311 | ||
312 | } |