1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
|
/** |
29 |
< |
* This class provides a skeletal implementation of the <tt>List</tt> |
29 |
> |
* This class provides a skeletal implementation of the {@link List} |
30 |
|
* interface to minimize the effort required to implement this interface |
31 |
|
* backed by a "random access" data store (such as an array). For sequential |
32 |
< |
* access data (such as a linked list), <tt>AbstractSequentialList</tt> should |
33 |
< |
* be used in preference to this class.<p> |
32 |
> |
* access data (such as a linked list), {@link AbstractSequentialList} should |
33 |
> |
* be used in preference to this class. |
34 |
> |
* |
35 |
> |
* <p>To implement an unmodifiable list, the programmer needs only to extend |
36 |
> |
* this class and provide implementations for the {@link #get(int)} and |
37 |
> |
* {@link List#size() size()} methods. |
38 |
|
* |
39 |
< |
* To implement an unmodifiable list, the programmer needs only to extend this |
40 |
< |
* class and provide implementations for the <tt>get(int index)</tt> and |
41 |
< |
* <tt>size()</tt> methods.<p> |
42 |
< |
* |
43 |
< |
* To implement a modifiable list, the programmer must additionally override |
22 |
< |
* the <tt>set(int index, Object element)</tt> method (which otherwise throws |
23 |
< |
* an <tt>UnsupportedOperationException</tt>. If the list is variable-size |
24 |
< |
* the programmer must additionally override the <tt>add(int index, Object |
25 |
< |
* element)</tt> and <tt>remove(int index)</tt> methods.<p> |
26 |
< |
* |
27 |
< |
* The programmer should generally provide a void (no argument) and collection |
28 |
< |
* constructor, as per the recommendation in the <tt>Collection</tt> interface |
29 |
< |
* specification.<p> |
39 |
> |
* <p>To implement a modifiable list, the programmer must additionally |
40 |
> |
* override the {@link #set(int, Object) set(int, E)} method (which otherwise |
41 |
> |
* throws an {@code UnsupportedOperationException}). If the list is |
42 |
> |
* variable-size the programmer must additionally override the |
43 |
> |
* {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods. |
44 |
|
* |
45 |
< |
* Unlike the other abstract collection implementations, the programmer does |
45 |
> |
* <p>The programmer should generally provide a void (no argument) and collection |
46 |
> |
* constructor, as per the recommendation in the {@link Collection} interface |
47 |
> |
* specification. |
48 |
> |
* |
49 |
> |
* <p>Unlike the other abstract collection implementations, the programmer does |
50 |
|
* <i>not</i> have to provide an iterator implementation; the iterator and |
51 |
|
* list iterator are implemented by this class, on top of the "random access" |
52 |
< |
* methods: <tt>get(int index)</tt>, <tt>set(int index, E element)</tt>, |
53 |
< |
* <tt>add(int index, E element)</tt> and <tt>remove(int index)</tt>.<p> |
52 |
> |
* methods: |
53 |
> |
* {@link #get(int)}, |
54 |
> |
* {@link #set(int, Object) set(int, E)}, |
55 |
> |
* {@link #add(int, Object) add(int, E)} and |
56 |
> |
* {@link #remove(int)}. |
57 |
|
* |
58 |
< |
* The documentation for each non-abstract methods in this class describes its |
58 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
59 |
|
* implementation in detail. Each of these methods may be overridden if the |
60 |
< |
* collection being implemented admits a more efficient implementation.<p> |
60 |
> |
* collection being implemented admits a more efficient implementation. |
61 |
|
* |
62 |
< |
* This class is a member of the |
63 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
62 |
> |
* <p>This class is a member of the |
63 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
64 |
|
* Java Collections Framework</a>. |
65 |
|
* |
66 |
|
* @author Josh Bloch |
67 |
|
* @author Neal Gafter |
68 |
< |
* @version 1.37, 01/18/03 |
48 |
< |
* @see Collection |
49 |
< |
* @see List |
50 |
< |
* @see AbstractSequentialList |
51 |
< |
* @see AbstractCollection |
68 |
> |
* @version %I%, %G% |
69 |
|
* @since 1.2 |
70 |
|
*/ |
71 |
|
|
88 |
|
* classes should clearly specify in their documentation any restrictions |
89 |
|
* on what elements may be added. |
90 |
|
* |
91 |
< |
* <p>This implementation calls <tt>add(size(), e)</tt>. |
91 |
> |
* <p>This implementation calls {@code add(size(), e)}. |
92 |
|
* |
93 |
|
* <p>Note that this implementation throws an |
94 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> |
95 |
< |
* is overridden. |
94 |
> |
* {@code UnsupportedOperationException} unless |
95 |
> |
* {@link #add(int, Object) add(int, E)} is overridden. |
96 |
|
* |
97 |
|
* @param e element to be appended to this list |
98 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
99 |
< |
* @throws UnsupportedOperationException if the <tt>add</tt> operation |
98 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
99 |
> |
* @throws UnsupportedOperationException if the {@code add} operation |
100 |
|
* is not supported by this list |
101 |
|
* @throws ClassCastException if the class of the specified element |
102 |
|
* prevents it from being added to this list |
121 |
|
* {@inheritDoc} |
122 |
|
* |
123 |
|
* <p>This implementation always throws an |
124 |
< |
* <tt>UnsupportedOperationException</tt>. |
124 |
> |
* {@code UnsupportedOperationException}. |
125 |
|
* |
126 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
127 |
|
* @throws ClassCastException {@inheritDoc} |
137 |
|
* {@inheritDoc} |
138 |
|
* |
139 |
|
* <p>This implementation always throws an |
140 |
< |
* <tt>UnsupportedOperationException</tt>. |
140 |
> |
* {@code UnsupportedOperationException}. |
141 |
|
* |
142 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
143 |
|
* @throws ClassCastException {@inheritDoc} |
153 |
|
* {@inheritDoc} |
154 |
|
* |
155 |
|
* <p>This implementation always throws an |
156 |
< |
* <tt>UnsupportedOperationException</tt>. |
156 |
> |
* {@code UnsupportedOperationException}. |
157 |
|
* |
158 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
159 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
169 |
|
* {@inheritDoc} |
170 |
|
* |
171 |
|
* <p>This implementation first gets a list iterator (with |
172 |
< |
* <tt>listIterator()</tt>). Then, it iterates over the list until the |
172 |
> |
* {@code listIterator()}). Then, it iterates over the list until the |
173 |
|
* specified element is found or the end of the list is reached. |
174 |
|
* |
175 |
|
* @throws ClassCastException {@inheritDoc} |
193 |
|
* {@inheritDoc} |
194 |
|
* |
195 |
|
* <p>This implementation first gets a list iterator that points to the end |
196 |
< |
* of the list (with <tt>listIterator(size())</tt>). Then, it iterates |
196 |
> |
* of the list (with {@code listIterator(size())}). Then, it iterates |
197 |
|
* backwards over the list until the specified element is found, or the |
198 |
|
* beginning of the list is reached. |
199 |
|
* |
221 |
|
* Removes all of the elements from this list (optional operation). |
222 |
|
* The list will be empty after this call returns. |
223 |
|
* |
224 |
< |
* <p>This implementation calls <tt>removeRange(0, size())</tt>. |
224 |
> |
* <p>This implementation calls {@code removeRange(0, size())}. |
225 |
|
* |
226 |
|
* <p>Note that this implementation throws an |
227 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>remove(int |
228 |
< |
* index)</tt> or <tt>removeRange(int fromIndex, int toIndex)</tt> is |
227 |
> |
* {@code UnsupportedOperationException} unless {@code remove(int |
228 |
> |
* index)} or {@code removeRange(int fromIndex, int toIndex)} is |
229 |
|
* overridden. |
230 |
|
* |
231 |
< |
* @throws UnsupportedOperationException if the <tt>clear</tt> operation |
231 |
> |
* @throws UnsupportedOperationException if the {@code clear} operation |
232 |
|
* is not supported by this list |
233 |
|
*/ |
234 |
|
public void clear() { |
238 |
|
/** |
239 |
|
* {@inheritDoc} |
240 |
|
* |
241 |
< |
* <p>This implementation gets an iterator over the specified collection and |
242 |
< |
* iterates over it, inserting the elements obtained from the iterator |
243 |
< |
* into this list at the appropriate position, one at a time, using |
244 |
< |
* <tt>add(int, Object)</tt>. Many implementations will override this |
245 |
< |
* method for efficiency. |
241 |
> |
* <p>This implementation gets an iterator over the specified collection |
242 |
> |
* and iterates over it, inserting the elements obtained from the |
243 |
> |
* iterator into this list at the appropriate position, one at a time, |
244 |
> |
* using {@code add(int, E)}. |
245 |
> |
* Many implementations will override this method for efficiency. |
246 |
|
* |
247 |
|
* <p>Note that this implementation throws an |
248 |
< |
* <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt> |
249 |
< |
* is overridden. |
248 |
> |
* {@code UnsupportedOperationException} unless |
249 |
> |
* {@link #add(int, Object) add(int, E)} is overridden. |
250 |
|
* |
251 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
252 |
|
* @throws ClassCastException {@inheritDoc} |
255 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
256 |
|
*/ |
257 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
258 |
+ |
rangeCheckForAdd(index); |
259 |
|
boolean modified = false; |
260 |
|
Iterator<? extends E> e = c.iterator(); |
261 |
|
while (e.hasNext()) { |
269 |
|
// Iterators |
270 |
|
|
271 |
|
/** |
272 |
< |
* Returns an iterator over the elements in this list in proper |
255 |
< |
* sequence. <p> |
272 |
> |
* Returns an iterator over the elements in this list in proper sequence. |
273 |
|
* |
274 |
< |
* This implementation returns a straightforward implementation of the |
275 |
< |
* iterator interface, relying on the backing list's <tt>size()</tt>, |
276 |
< |
* <tt>get(int)</tt>, and <tt>remove(int)</tt> methods.<p> |
260 |
< |
* |
261 |
< |
* Note that the iterator returned by this method will throw an |
262 |
< |
* <tt>UnsupportedOperationException</tt> in response to its |
263 |
< |
* <tt>remove</tt> method unless the list's <tt>remove(int)</tt> method is |
264 |
< |
* overridden.<p> |
265 |
< |
* |
266 |
< |
* This implementation can be made to throw runtime exceptions in the face |
267 |
< |
* of concurrent modification, as described in the specification for the |
268 |
< |
* (protected) <tt>modCount</tt> field. |
274 |
> |
* <p>This implementation returns a straightforward implementation of the |
275 |
> |
* iterator interface, relying on the backing list's {@code size()}, |
276 |
> |
* {@code get(int)}, and {@code remove(int)} methods. |
277 |
|
* |
278 |
< |
* @return an iterator over the elements in this list in proper sequence |
278 |
> |
* <p>Note that the iterator returned by this method will throw an |
279 |
> |
* {@link UnsupportedOperationException} in response to its |
280 |
> |
* {@code remove} method unless the list's {@code remove(int)} method is |
281 |
> |
* overridden. |
282 |
> |
* |
283 |
> |
* <p>This implementation can be made to throw runtime exceptions in the |
284 |
> |
* face of concurrent modification, as described in the specification |
285 |
> |
* for the (protected) {@link #modCount} field. |
286 |
|
* |
287 |
< |
* @see #modCount |
287 |
> |
* @return an iterator over the elements in this list in proper sequence |
288 |
|
*/ |
289 |
|
public Iterator<E> iterator() { |
290 |
|
return new Itr(); |
293 |
|
/** |
294 |
|
* {@inheritDoc} |
295 |
|
* |
296 |
< |
* <p>This implementation returns <tt>listIterator(0)</tt>. |
296 |
> |
* <p>This implementation returns {@code listIterator(0)}. |
297 |
|
* |
298 |
|
* @see #listIterator(int) |
299 |
|
*/ |
305 |
|
* {@inheritDoc} |
306 |
|
* |
307 |
|
* <p>This implementation returns a straightforward implementation of the |
308 |
< |
* <tt>ListIterator</tt> interface that extends the implementation of the |
309 |
< |
* <tt>Iterator</tt> interface returned by the <tt>iterator()</tt> method. |
310 |
< |
* The <tt>ListIterator</tt> implementation relies on the backing list's |
311 |
< |
* <tt>get(int)</tt>, <tt>set(int, Object)</tt>, <tt>add(int, Object)</tt> |
312 |
< |
* and <tt>remove(int)</tt> methods. |
308 |
> |
* {@code ListIterator} interface that extends the implementation of the |
309 |
> |
* {@code Iterator} interface returned by the {@code iterator()} method. |
310 |
> |
* The {@code ListIterator} implementation relies on the backing list's |
311 |
> |
* {@code get(int)}, {@code set(int, E)}, {@code add(int, E)} |
312 |
> |
* and {@code remove(int)} methods. |
313 |
|
* |
314 |
|
* <p>Note that the list iterator returned by this implementation will |
315 |
< |
* throw an <tt>UnsupportedOperationException</tt> in response to its |
316 |
< |
* <tt>remove</tt>, <tt>set</tt> and <tt>add</tt> methods unless the |
317 |
< |
* list's <tt>remove(int)</tt>, <tt>set(int, Object)</tt>, and |
318 |
< |
* <tt>add(int, Object)</tt> methods are overridden. |
315 |
> |
* throw an {@link UnsupportedOperationException} in response to its |
316 |
> |
* {@code remove}, {@code set} and {@code add} methods unless the |
317 |
> |
* list's {@code remove(int)}, {@code set(int, E)}, and |
318 |
> |
* {@code add(int, E)} methods are overridden. |
319 |
|
* |
320 |
|
* <p>This implementation can be made to throw runtime exceptions in the |
321 |
|
* face of concurrent modification, as described in the specification for |
322 |
< |
* the (protected) <tt>modCount</tt> field. |
322 |
> |
* the (protected) {@link #modCount} field. |
323 |
|
* |
324 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
310 |
– |
* |
311 |
– |
* @see #modCount |
325 |
|
*/ |
326 |
|
public ListIterator<E> listIterator(final int index) { |
327 |
< |
if (index<0 || index>size()) |
315 |
< |
throw new IndexOutOfBoundsException("Index: "+index); |
327 |
> |
rangeCheckForAdd(index); |
328 |
|
|
329 |
|
return new ListItr(index); |
330 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
public E next() { |
357 |
< |
try { |
358 |
< |
int i = cursor; |
359 |
< |
E next = get(i); |
360 |
< |
lastRet = i; |
361 |
< |
cursor = i + 1; |
362 |
< |
return next; |
363 |
< |
} catch (IndexOutOfBoundsException ex) { |
364 |
< |
throw new NoSuchElementException(); |
365 |
< |
} finally { |
366 |
< |
if (expectedModCount != modCount) |
367 |
< |
throw new ConcurrentModificationException(); |
356 |
< |
} |
357 |
> |
checkForComodification(); |
358 |
> |
try { |
359 |
> |
int i = cursor; |
360 |
> |
E next = get(i); |
361 |
> |
lastRet = i; |
362 |
> |
cursor = i + 1; |
363 |
> |
return next; |
364 |
> |
} catch (IndexOutOfBoundsException e) { |
365 |
> |
checkForComodification(); |
366 |
> |
throw new NoSuchElementException(); |
367 |
> |
} |
368 |
|
} |
369 |
|
|
370 |
|
public void remove() { |
371 |
< |
if (lastRet == -1) |
371 |
> |
if (lastRet < 0) |
372 |
|
throw new IllegalStateException(); |
373 |
< |
if (expectedModCount != modCount) |
374 |
< |
throw new ConcurrentModificationException(); |
373 |
> |
checkForComodification(); |
374 |
> |
|
375 |
|
try { |
376 |
|
AbstractList.this.remove(lastRet); |
377 |
|
if (lastRet < cursor) |
382 |
|
throw new ConcurrentModificationException(); |
383 |
|
} |
384 |
|
} |
385 |
+ |
|
386 |
+ |
final void checkForComodification() { |
387 |
+ |
if (modCount != expectedModCount) |
388 |
+ |
throw new ConcurrentModificationException(); |
389 |
+ |
} |
390 |
|
} |
391 |
< |
|
391 |
> |
|
392 |
|
private class ListItr extends Itr implements ListIterator<E> { |
393 |
|
ListItr(int index) { |
394 |
|
cursor = index; |
398 |
|
return cursor != 0; |
399 |
|
} |
400 |
|
|
385 |
– |
public int nextIndex() { |
386 |
– |
return cursor; |
387 |
– |
} |
388 |
– |
|
389 |
– |
public int previousIndex() { |
390 |
– |
return cursor - 1; |
391 |
– |
} |
392 |
– |
|
401 |
|
public E previous() { |
402 |
+ |
checkForComodification(); |
403 |
|
try { |
404 |
|
int i = cursor - 1; |
405 |
< |
E prev = get(i); |
406 |
< |
lastRet = i; |
407 |
< |
cursor = i; |
408 |
< |
return prev; |
409 |
< |
} catch (IndexOutOfBoundsException ex) { |
405 |
> |
E previous = get(i); |
406 |
> |
lastRet = cursor = i; |
407 |
> |
return previous; |
408 |
> |
} catch (IndexOutOfBoundsException e) { |
409 |
> |
checkForComodification(); |
410 |
|
throw new NoSuchElementException(); |
402 |
– |
} finally { |
403 |
– |
if (expectedModCount != modCount) |
404 |
– |
throw new ConcurrentModificationException(); |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
+ |
public int nextIndex() { |
415 |
+ |
return cursor; |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
public int previousIndex() { |
419 |
+ |
return cursor-1; |
420 |
+ |
} |
421 |
+ |
|
422 |
|
public void set(E e) { |
423 |
< |
if (lastRet == -1) |
423 |
> |
if (lastRet < 0) |
424 |
|
throw new IllegalStateException(); |
425 |
< |
if (expectedModCount != modCount) |
426 |
< |
throw new ConcurrentModificationException(); |
425 |
> |
checkForComodification(); |
426 |
> |
|
427 |
|
try { |
428 |
|
AbstractList.this.set(lastRet, e); |
429 |
|
expectedModCount = modCount; |
433 |
|
} |
434 |
|
|
435 |
|
public void add(E e) { |
436 |
< |
if (expectedModCount != modCount) |
437 |
< |
throw new ConcurrentModificationException(); |
436 |
> |
checkForComodification(); |
437 |
> |
|
438 |
|
try { |
439 |
< |
int i = cursor; |
439 |
> |
int i = cursor; |
440 |
|
AbstractList.this.add(i, e); |
427 |
– |
cursor = i + 1; |
441 |
|
lastRet = -1; |
442 |
+ |
cursor = i + 1; |
443 |
|
expectedModCount = modCount; |
444 |
|
} catch (IndexOutOfBoundsException ex) { |
445 |
|
throw new ConcurrentModificationException(); |
451 |
|
* {@inheritDoc} |
452 |
|
* |
453 |
|
* <p>This implementation returns a list that subclasses |
454 |
< |
* <tt>AbstractList</tt>. The subclass stores, in private fields, the |
454 |
> |
* {@code AbstractList}. The subclass stores, in private fields, the |
455 |
|
* offset of the subList within the backing list, the size of the subList |
456 |
|
* (which can change over its lifetime), and the expected |
457 |
< |
* <tt>modCount</tt> value of the backing list. There are two variants |
458 |
< |
* of the subclass, one of which implements <tt>RandomAccess</tt>. |
459 |
< |
* If this list implements <tt>RandomAccess</tt> the returned list will |
460 |
< |
* be an instance of the subclass that implements <tt>RandomAccess</tt>. |
461 |
< |
* |
462 |
< |
* <p>The subclass's <tt>set(int, Object)</tt>, <tt>get(int)</tt>, |
463 |
< |
* <tt>add(int, Object)</tt>, <tt>remove(int)</tt>, <tt>addAll(int, |
464 |
< |
* Collection)</tt> and <tt>removeRange(int, int)</tt> methods all |
457 |
> |
* {@code modCount} value of the backing list. There are two variants |
458 |
> |
* of the subclass, one of which implements {@code RandomAccess}. |
459 |
> |
* If this list implements {@code RandomAccess} the returned list will |
460 |
> |
* be an instance of the subclass that implements {@code RandomAccess}. |
461 |
> |
* |
462 |
> |
* <p>The subclass's {@code set(int, E)}, {@code get(int)}, |
463 |
> |
* {@code add(int, E)}, {@code remove(int)}, {@code addAll(int, |
464 |
> |
* Collection)} and {@code removeRange(int, int)} methods all |
465 |
|
* delegate to the corresponding methods on the backing abstract list, |
466 |
|
* after bounds-checking the index and adjusting for the offset. The |
467 |
< |
* <tt>addAll(Collection c)</tt> method merely returns <tt>addAll(size, |
468 |
< |
* c)</tt>. |
467 |
> |
* {@code addAll(Collection c)} method merely returns {@code addAll(size, |
468 |
> |
* c)}. |
469 |
|
* |
470 |
< |
* <p>The <tt>listIterator(int)</tt> method returns a "wrapper object" |
470 |
> |
* <p>The {@code listIterator(int)} method returns a "wrapper object" |
471 |
|
* over a list iterator on the backing list, which is created with the |
472 |
< |
* corresponding method on the backing list. The <tt>iterator</tt> method |
473 |
< |
* merely returns <tt>listIterator()</tt>, and the <tt>size</tt> method |
474 |
< |
* merely returns the subclass's <tt>size</tt> field. |
472 |
> |
* corresponding method on the backing list. The {@code iterator} method |
473 |
> |
* merely returns {@code listIterator()}, and the {@code size} method |
474 |
> |
* merely returns the subclass's {@code size} field. |
475 |
|
* |
476 |
< |
* <p>All methods first check to see if the actual <tt>modCount</tt> of |
476 |
> |
* <p>All methods first check to see if the actual {@code modCount} of |
477 |
|
* the backing list is equal to its expected value, and throw a |
478 |
< |
* <tt>ConcurrentModificationException</tt> if it is not. |
478 |
> |
* {@code ConcurrentModificationException} if it is not. |
479 |
|
* |
480 |
< |
* @throws IndexOutOfBoundsException endpoint index value out of range |
481 |
< |
* <tt>(fromIndex < 0 || toIndex > size)</tt> |
480 |
> |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
481 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
482 |
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
483 |
< |
* <tt>(fromIndex > toIndex)</tt> |
483 |
> |
* {@code (fromIndex > toIndex)} |
484 |
|
*/ |
485 |
|
public List<E> subList(int fromIndex, int toIndex) { |
486 |
|
return (this instanceof RandomAccess ? |
492 |
|
|
493 |
|
/** |
494 |
|
* Compares the specified object with this list for equality. Returns |
495 |
< |
* <tt>true</tt> if and only if the specified object is also a list, both |
495 |
> |
* {@code true} if and only if the specified object is also a list, both |
496 |
|
* lists have the same size, and all corresponding pairs of elements in |
497 |
< |
* the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and |
498 |
< |
* <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null : |
499 |
< |
* e1.equals(e2))</tt>.) In other words, two lists are defined to be |
497 |
> |
* the two lists are <i>equal</i>. (Two elements {@code e1} and |
498 |
> |
* {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null : |
499 |
> |
* e1.equals(e2))}.) In other words, two lists are defined to be |
500 |
|
* equal if they contain the same elements in the same order.<p> |
501 |
|
* |
502 |
|
* This implementation first checks if the specified object is this |
503 |
< |
* list. If so, it returns <tt>true</tt>; if not, it checks if the |
504 |
< |
* specified object is a list. If not, it returns <tt>false</tt>; if so, |
503 |
> |
* list. If so, it returns {@code true}; if not, it checks if the |
504 |
> |
* specified object is a list. If not, it returns {@code false}; if so, |
505 |
|
* it iterates over both lists, comparing corresponding pairs of elements. |
506 |
< |
* If any comparison returns <tt>false</tt>, this method returns |
507 |
< |
* <tt>false</tt>. If either iterator runs out of elements before the |
508 |
< |
* other it returns <tt>false</tt> (as the lists are of unequal length); |
509 |
< |
* otherwise it returns <tt>true</tt> when the iterations complete. |
506 |
> |
* If any comparison returns {@code false}, this method returns |
507 |
> |
* {@code false}. If either iterator runs out of elements before the |
508 |
> |
* other it returns {@code false} (as the lists are of unequal length); |
509 |
> |
* otherwise it returns {@code true} when the iterations complete. |
510 |
|
* |
511 |
|
* @param o the object to be compared for equality with this list |
512 |
< |
* @return <tt>true</tt> if the specified object is equal to this list |
512 |
> |
* @return {@code true} if the specified object is equal to this list |
513 |
|
*/ |
514 |
|
public boolean equals(Object o) { |
515 |
|
if (o == this) |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* Returns the hash code value for this list. <p> |
532 |
> |
* Returns the hash code value for this list. |
533 |
|
* |
534 |
< |
* This implementation uses exactly the code that is used to define the |
534 |
> |
* <p>This implementation uses exactly the code that is used to define the |
535 |
|
* list hash function in the documentation for the {@link List#hashCode} |
536 |
|
* method. |
537 |
|
* |
539 |
|
*/ |
540 |
|
public int hashCode() { |
541 |
|
int hashCode = 1; |
542 |
< |
Iterator<E> i = iterator(); |
543 |
< |
while (i.hasNext()) { |
530 |
< |
E obj = i.next(); |
531 |
< |
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); |
532 |
< |
} |
542 |
> |
for (E e : this) |
543 |
> |
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
544 |
|
return hashCode; |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
|
* Removes from this list all of the elements whose index is between |
549 |
< |
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. |
549 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
550 |
|
* Shifts any succeeding elements to the left (reduces their index). |
551 |
< |
* This call shortens the ArrayList by <tt>(toIndex - fromIndex)</tt> |
552 |
< |
* elements. (If <tt>toIndex==fromIndex</tt>, this operation has no |
542 |
< |
* effect.)<p> |
551 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
552 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
553 |
|
* |
554 |
< |
* This method is called by the <tt>clear</tt> operation on this list |
554 |
> |
* <p>This method is called by the {@code clear} operation on this list |
555 |
|
* and its subLists. Overriding this method to take advantage of |
556 |
|
* the internals of the list implementation can <i>substantially</i> |
557 |
< |
* improve the performance of the <tt>clear</tt> operation on this list |
558 |
< |
* and its subLists.<p> |
557 |
> |
* improve the performance of the {@code clear} operation on this list |
558 |
> |
* and its subLists. |
559 |
|
* |
560 |
< |
* This implementation gets a list iterator positioned before |
561 |
< |
* <tt>fromIndex</tt>, and repeatedly calls <tt>ListIterator.next</tt> |
562 |
< |
* followed by <tt>ListIterator.remove</tt> until the entire range has |
563 |
< |
* been removed. <b>Note: if <tt>ListIterator.remove</tt> requires linear |
560 |
> |
* <p>This implementation gets a list iterator positioned before |
561 |
> |
* {@code fromIndex}, and repeatedly calls {@code ListIterator.next} |
562 |
> |
* followed by {@code ListIterator.remove} until the entire range has |
563 |
> |
* been removed. <b>Note: if {@code ListIterator.remove} requires linear |
564 |
|
* time, this implementation requires quadratic time.</b> |
565 |
|
* |
566 |
|
* @param fromIndex index of first element to be removed |
578 |
|
* The number of times this list has been <i>structurally modified</i>. |
579 |
|
* Structural modifications are those that change the size of the |
580 |
|
* list, or otherwise perturb it in such a fashion that iterations in |
581 |
< |
* progress may yield incorrect results.<p> |
581 |
> |
* progress may yield incorrect results. |
582 |
|
* |
583 |
< |
* This field is used by the iterator and list iterator implementation |
584 |
< |
* returned by the <tt>iterator</tt> and <tt>listIterator</tt> methods. |
583 |
> |
* <p>This field is used by the iterator and list iterator implementation |
584 |
> |
* returned by the {@code iterator} and {@code listIterator} methods. |
585 |
|
* If the value of this field changes unexpectedly, the iterator (or list |
586 |
< |
* iterator) will throw a <tt>ConcurrentModificationException</tt> in |
587 |
< |
* response to the <tt>next</tt>, <tt>remove</tt>, <tt>previous</tt>, |
588 |
< |
* <tt>set</tt> or <tt>add</tt> operations. This provides |
586 |
> |
* iterator) will throw a {@code ConcurrentModificationException} in |
587 |
> |
* response to the {@code next}, {@code remove}, {@code previous}, |
588 |
> |
* {@code set} or {@code add} operations. This provides |
589 |
|
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in |
590 |
< |
* the face of concurrent modification during iteration.<p> |
590 |
> |
* the face of concurrent modification during iteration. |
591 |
|
* |
592 |
< |
* <b>Use of this field by subclasses is optional.</b> If a subclass |
592 |
> |
* <p><b>Use of this field by subclasses is optional.</b> If a subclass |
593 |
|
* wishes to provide fail-fast iterators (and list iterators), then it |
594 |
< |
* merely has to increment this field in its <tt>add(int, Object)</tt> and |
595 |
< |
* <tt>remove(int)</tt> methods (and any other methods that it overrides |
594 |
> |
* merely has to increment this field in its {@code add(int, E)} and |
595 |
> |
* {@code remove(int)} methods (and any other methods that it overrides |
596 |
|
* that result in structural modifications to the list). A single call to |
597 |
< |
* <tt>add(int, Object)</tt> or <tt>remove(int)</tt> must add no more than |
597 |
> |
* {@code add(int, E)} or {@code remove(int)} must add no more than |
598 |
|
* one to this field, or the iterators (and list iterators) will throw |
599 |
< |
* bogus <tt>ConcurrentModificationExceptions</tt>. If an implementation |
599 |
> |
* bogus {@code ConcurrentModificationExceptions}. If an implementation |
600 |
|
* does not wish to provide fail-fast iterators, this field may be |
601 |
|
* ignored. |
602 |
|
*/ |
603 |
|
protected transient int modCount = 0; |
604 |
+ |
|
605 |
+ |
private void rangeCheckForAdd(int index) { |
606 |
+ |
if (index < 0 || index > size()) |
607 |
+ |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
608 |
+ |
} |
609 |
+ |
|
610 |
+ |
private String outOfBoundsMsg(int index) { |
611 |
+ |
return "Index: "+index+", Size: "+size(); |
612 |
+ |
} |
613 |
|
} |
614 |
|
|
615 |
|
class SubList<E> extends AbstractList<E> { |
616 |
< |
private AbstractList<E> l; |
617 |
< |
private int offset; |
616 |
> |
private final AbstractList<E> l; |
617 |
> |
private final int offset; |
618 |
|
private int size; |
600 |
– |
private int expectedModCount; |
619 |
|
|
620 |
|
SubList(AbstractList<E> list, int fromIndex, int toIndex) { |
621 |
|
if (fromIndex < 0) |
628 |
|
l = list; |
629 |
|
offset = fromIndex; |
630 |
|
size = toIndex - fromIndex; |
631 |
< |
expectedModCount = l.modCount; |
631 |
> |
this.modCount = l.modCount; |
632 |
|
} |
633 |
|
|
634 |
|
public E set(int index, E element) { |
649 |
|
} |
650 |
|
|
651 |
|
public void add(int index, E element) { |
652 |
< |
if (index<0 || index>size) |
635 |
< |
throw new IndexOutOfBoundsException(); |
652 |
> |
rangeCheckForAdd(index); |
653 |
|
checkForComodification(); |
654 |
|
l.add(index+offset, element); |
655 |
< |
expectedModCount = l.modCount; |
655 |
> |
this.modCount = l.modCount; |
656 |
|
size++; |
640 |
– |
modCount++; |
657 |
|
} |
658 |
|
|
659 |
|
public E remove(int index) { |
660 |
|
rangeCheck(index); |
661 |
|
checkForComodification(); |
662 |
|
E result = l.remove(index+offset); |
663 |
< |
expectedModCount = l.modCount; |
663 |
> |
this.modCount = l.modCount; |
664 |
|
size--; |
649 |
– |
modCount++; |
665 |
|
return result; |
666 |
|
} |
667 |
|
|
668 |
|
protected void removeRange(int fromIndex, int toIndex) { |
669 |
|
checkForComodification(); |
670 |
|
l.removeRange(fromIndex+offset, toIndex+offset); |
671 |
< |
expectedModCount = l.modCount; |
671 |
> |
this.modCount = l.modCount; |
672 |
|
size -= (toIndex-fromIndex); |
658 |
– |
modCount++; |
673 |
|
} |
674 |
|
|
675 |
|
public boolean addAll(Collection<? extends E> c) { |
677 |
|
} |
678 |
|
|
679 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
680 |
< |
if (index<0 || index>size) |
667 |
< |
throw new IndexOutOfBoundsException( |
668 |
< |
"Index: "+index+", Size: "+size); |
680 |
> |
rangeCheckForAdd(index); |
681 |
|
int cSize = c.size(); |
682 |
|
if (cSize==0) |
683 |
|
return false; |
684 |
|
|
685 |
|
checkForComodification(); |
686 |
|
l.addAll(offset+index, c); |
687 |
< |
expectedModCount = l.modCount; |
687 |
> |
this.modCount = l.modCount; |
688 |
|
size += cSize; |
677 |
– |
modCount++; |
689 |
|
return true; |
690 |
|
} |
691 |
|
|
695 |
|
|
696 |
|
public ListIterator<E> listIterator(final int index) { |
697 |
|
checkForComodification(); |
698 |
< |
if (index<0 || index>size) |
688 |
< |
throw new IndexOutOfBoundsException( |
689 |
< |
"Index: "+index+", Size: "+size); |
698 |
> |
rangeCheckForAdd(index); |
699 |
|
|
700 |
|
return new ListIterator<E>() { |
701 |
< |
private ListIterator<E> i = l.listIterator(index+offset); |
701 |
> |
private final ListIterator<E> i = l.listIterator(index+offset); |
702 |
|
|
703 |
|
public boolean hasNext() { |
704 |
|
return nextIndex() < size; |
732 |
|
|
733 |
|
public void remove() { |
734 |
|
i.remove(); |
735 |
< |
expectedModCount = l.modCount; |
735 |
> |
SubList.this.modCount = l.modCount; |
736 |
|
size--; |
728 |
– |
modCount++; |
737 |
|
} |
738 |
|
|
739 |
|
public void set(E e) { |
742 |
|
|
743 |
|
public void add(E e) { |
744 |
|
i.add(e); |
745 |
< |
expectedModCount = l.modCount; |
745 |
> |
SubList.this.modCount = l.modCount; |
746 |
|
size++; |
739 |
– |
modCount++; |
747 |
|
} |
748 |
|
}; |
749 |
|
} |
753 |
|
} |
754 |
|
|
755 |
|
private void rangeCheck(int index) { |
756 |
< |
if (index<0 || index>=size) |
757 |
< |
throw new IndexOutOfBoundsException("Index: "+index+ |
758 |
< |
",Size: "+size); |
756 |
> |
if (index < 0 || index >= size) |
757 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
758 |
> |
} |
759 |
> |
|
760 |
> |
private void rangeCheckForAdd(int index) { |
761 |
> |
if (index < 0 || index > size) |
762 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
763 |
> |
} |
764 |
> |
|
765 |
> |
private String outOfBoundsMsg(int index) { |
766 |
> |
return "Index: "+index+", Size: "+size; |
767 |
|
} |
768 |
|
|
769 |
|
private void checkForComodification() { |
770 |
< |
if (l.modCount != expectedModCount) |
770 |
> |
if (this.modCount != l.modCount) |
771 |
|
throw new ConcurrentModificationException(); |
772 |
|
} |
773 |
|
} |