1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2006 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 {@code List} |
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), {@code AbstractSequentialList} should |
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 this |
36 |
< |
* class and provide implementations for the {@code get(int index)} and |
37 |
< |
* {@code size()} methods. |
38 |
< |
* |
39 |
< |
* <p>To implement a modifiable list, the programmer must additionally override |
40 |
< |
* the {@code set(int index, Object element)} method (which otherwise throws |
41 |
< |
* an {@code UnsupportedOperationException}. If the list is variable-size |
42 |
< |
* the programmer must additionally override the {@code add(int index, Object |
43 |
< |
* element)} and {@code remove(int index)} methods. |
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 |
> |
* <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 |
|
* <p>The programmer should generally provide a void (no argument) and collection |
46 |
< |
* constructor, as per the recommendation in the {@code Collection} interface |
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: {@code get(int index)}, {@code set(int index, E element)}, |
53 |
< |
* {@code add(int index, E element)} and {@code remove(int index)}. |
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 |
< |
* <p>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. |
61 |
|
* |
66 |
|
* @author Josh Bloch |
67 |
|
* @author Neal Gafter |
68 |
|
* @version %I%, %G% |
48 |
– |
* @see Collection |
49 |
– |
* @see List |
50 |
– |
* @see AbstractSequentialList |
51 |
– |
* @see AbstractCollection |
69 |
|
* @since 1.2 |
70 |
|
*/ |
71 |
|
|
91 |
|
* <p>This implementation calls {@code add(size(), e)}. |
92 |
|
* |
93 |
|
* <p>Note that this implementation throws an |
94 |
< |
* {@code UnsupportedOperationException} unless {@code add(int, Object)} |
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 {@code true} (as specified by {@link Collection#add}) |
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 |
< |
* {@code add(int, Object)}. 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 |
< |
* {@code UnsupportedOperationException} unless {@code add(int, Object)} |
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} |
268 |
|
// Iterators |
269 |
|
|
270 |
|
/** |
271 |
< |
* Returns an iterator over the elements in this list in proper |
255 |
< |
* sequence. <p> |
271 |
> |
* Returns an iterator over the elements in this list in proper sequence. |
272 |
|
* |
273 |
< |
* This implementation returns a straightforward implementation of the |
273 |
> |
* <p>This implementation returns a straightforward implementation of the |
274 |
|
* iterator interface, relying on the backing list's {@code size()}, |
275 |
< |
* {@code get(int)}, and {@code remove(int)} methods.<p> |
275 |
> |
* {@code get(int)}, and {@code remove(int)} methods. |
276 |
|
* |
277 |
< |
* Note that the iterator returned by this method will throw an |
277 |
> |
* <p>Note that the iterator returned by this method will throw an |
278 |
|
* {@code UnsupportedOperationException} in response to its |
279 |
|
* {@code remove} method unless the list's {@code remove(int)} method is |
280 |
< |
* overridden.<p> |
280 |
> |
* overridden. |
281 |
|
* |
282 |
< |
* This implementation can be made to throw runtime exceptions in the face |
283 |
< |
* of concurrent modification, as described in the specification for the |
284 |
< |
* (protected) {@code modCount} field. |
282 |
> |
* <p>This implementation can be made to throw runtime exceptions in the |
283 |
> |
* face of concurrent modification, as described in the specification |
284 |
> |
* for the (protected) {@code modCount} field. |
285 |
|
* |
286 |
|
* @return an iterator over the elements in this list in proper sequence |
287 |
|
* |
309 |
|
* {@code ListIterator} interface that extends the implementation of the |
310 |
|
* {@code Iterator} interface returned by the {@code iterator()} method. |
311 |
|
* The {@code ListIterator} implementation relies on the backing list's |
312 |
< |
* {@code get(int)}, {@code set(int, Object)}, {@code add(int, Object)} |
312 |
> |
* {@code get(int)}, {@code set(int, E)}, {@code add(int, E)} |
313 |
|
* and {@code remove(int)} methods. |
314 |
|
* |
315 |
|
* <p>Note that the list iterator returned by this implementation will |
316 |
|
* throw an {@code UnsupportedOperationException} in response to its |
317 |
|
* {@code remove}, {@code set} and {@code add} methods unless the |
318 |
< |
* list's {@code remove(int)}, {@code set(int, Object)}, and |
319 |
< |
* {@code add(int, Object)} methods are overridden. |
318 |
> |
* list's {@code remove(int)}, {@code set(int, E)}, and |
319 |
> |
* {@code add(int, E)} methods are overridden. |
320 |
|
* |
321 |
|
* <p>This implementation can be made to throw runtime exceptions in the |
322 |
|
* face of concurrent modification, as described in the specification for |
461 |
|
* If this list implements {@code RandomAccess} the returned list will |
462 |
|
* be an instance of the subclass that implements {@code RandomAccess}. |
463 |
|
* |
464 |
< |
* <p>The subclass's {@code set(int, Object)}, {@code get(int)}, |
465 |
< |
* {@code add(int, Object)}, {@code remove(int)}, {@code addAll(int, |
464 |
> |
* <p>The subclass's {@code set(int, E)}, {@code get(int)}, |
465 |
> |
* {@code add(int, E)}, {@code remove(int)}, {@code addAll(int, |
466 |
|
* Collection)} and {@code removeRange(int, int)} methods all |
467 |
|
* delegate to the corresponding methods on the backing abstract list, |
468 |
|
* after bounds-checking the index and adjusting for the offset. The |
480 |
|
* {@code ConcurrentModificationException} if it is not. |
481 |
|
* |
482 |
|
* @throws IndexOutOfBoundsException endpoint index value out of range |
483 |
< |
* {@code (fromIndex < 0 || toIndex > size)} |
483 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
484 |
|
* @throws IllegalArgumentException if the endpoint indices are out of order |
485 |
< |
* {@code (fromIndex > toIndex)} |
485 |
> |
* {@code (fromIndex > toIndex)} |
486 |
|
*/ |
487 |
|
public List<E> subList(int fromIndex, int toIndex) { |
488 |
|
return (this instanceof RandomAccess ? |
499 |
|
* the two lists are <i>equal</i>. (Two elements {@code e1} and |
500 |
|
* {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null : |
501 |
|
* e1.equals(e2))}.) In other words, two lists are defined to be |
502 |
< |
* equal if they contain the same elements in the same order.<p> |
502 |
> |
* equal if they contain the same elements in the same order. |
503 |
|
* |
504 |
< |
* This implementation first checks if the specified object is this |
504 |
> |
* <p>This implementation first checks if the specified object is this |
505 |
|
* list. If so, it returns {@code true}; if not, it checks if the |
506 |
|
* specified object is a list. If not, it returns {@code false}; if so, |
507 |
|
* it iterates over both lists, comparing corresponding pairs of elements. |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* Returns the hash code value for this list. <p> |
534 |
> |
* Returns the hash code value for this list. |
535 |
|
* |
536 |
< |
* This implementation uses exactly the code that is used to define the |
536 |
> |
* <p>This implementation uses exactly the code that is used to define the |
537 |
|
* list hash function in the documentation for the {@link List#hashCode} |
538 |
|
* method. |
539 |
|
* |
555 |
|
* Shifts any succeeding elements to the left (reduces their index). |
556 |
|
* This call shortens the ArrayList by {@code (toIndex - fromIndex)} |
557 |
|
* elements. (If {@code toIndex==fromIndex}, this operation has no |
558 |
< |
* effect.)<p> |
558 |
> |
* effect.) |
559 |
|
* |
560 |
< |
* This method is called by the {@code clear} operation on this list |
560 |
> |
* <p>This method is called by the {@code clear} operation on this list |
561 |
|
* and its subLists. Overriding this method to take advantage of |
562 |
|
* the internals of the list implementation can <i>substantially</i> |
563 |
|
* improve the performance of the {@code clear} operation on this list |
564 |
< |
* and its subLists.<p> |
564 |
> |
* and its subLists. |
565 |
|
* |
566 |
< |
* This implementation gets a list iterator positioned before |
566 |
> |
* <p>This implementation gets a list iterator positioned before |
567 |
|
* {@code fromIndex}, and repeatedly calls {@code ListIterator.next} |
568 |
|
* followed by {@code ListIterator.remove} until the entire range has |
569 |
|
* been removed. <b>Note: if {@code ListIterator.remove} requires linear |
584 |
|
* The number of times this list has been <i>structurally modified</i>. |
585 |
|
* Structural modifications are those that change the size of the |
586 |
|
* list, or otherwise perturb it in such a fashion that iterations in |
587 |
< |
* progress may yield incorrect results.<p> |
587 |
> |
* progress may yield incorrect results. |
588 |
|
* |
589 |
< |
* This field is used by the iterator and list iterator implementation |
589 |
> |
* <p>This field is used by the iterator and list iterator implementation |
590 |
|
* returned by the {@code iterator} and {@code listIterator} methods. |
591 |
|
* If the value of this field changes unexpectedly, the iterator (or list |
592 |
|
* iterator) will throw a {@code ConcurrentModificationException} in |
593 |
|
* response to the {@code next}, {@code remove}, {@code previous}, |
594 |
|
* {@code set} or {@code add} operations. This provides |
595 |
|
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in |
596 |
< |
* the face of concurrent modification during iteration.<p> |
596 |
> |
* the face of concurrent modification during iteration. |
597 |
|
* |
598 |
< |
* <b>Use of this field by subclasses is optional.</b> If a subclass |
598 |
> |
* <p><b>Use of this field by subclasses is optional.</b> If a subclass |
599 |
|
* wishes to provide fail-fast iterators (and list iterators), then it |
600 |
< |
* merely has to increment this field in its {@code add(int, Object)} and |
600 |
> |
* merely has to increment this field in its {@code add(int, E)} and |
601 |
|
* {@code remove(int)} methods (and any other methods that it overrides |
602 |
|
* that result in structural modifications to the list). A single call to |
603 |
< |
* {@code add(int, Object)} or {@code remove(int)} must add no more than |
603 |
> |
* {@code add(int, E)} or {@code remove(int)} must add no more than |
604 |
|
* one to this field, or the iterators (and list iterators) will throw |
605 |
|
* bogus {@code ConcurrentModificationExceptions}. If an implementation |
606 |
|
* does not wish to provide fail-fast iterators, this field may be |
887 |
|
fromIndex, toIndex); |
888 |
|
} |
889 |
|
} |
874 |
– |
|