28 |
|
* to preclude interference among concurrent threads. The iterator |
29 |
|
* method uses a reference to the state of the array at the point that |
30 |
|
* the iterator was created. This array never changes during the |
31 |
< |
* lifetime of the iterator, so interference is impossible and the iterator |
32 |
< |
* is guaranteed not to throw <tt>ConcurrentModificationException</tt>. The |
33 |
< |
* iterator will not reflect additions, removals, or changes to the List since the |
34 |
< |
* iterator was created. |
35 |
< |
* <p> |
31 |
> |
* lifetime of the iterator, so interference is impossible and the |
32 |
> |
* iterator is guaranteed not to throw |
33 |
> |
* <tt>ConcurrentModificationException</tt>. The iterator will not |
34 |
> |
* reflect additions, removals, or changes to the List since the |
35 |
> |
* iterator was created. <p> |
36 |
|
* |
37 |
|
* Because of the copy-on-write policy, some one-by-one mutative |
38 |
|
* operations in the java.util.Arrays and java.util.Collections |
41 |
|
* operations on iterators (remove, set, and add) are not |
42 |
|
* supported. These are the only methods throwing |
43 |
|
* UnsupportedOperationException. <p> |
44 |
< |
**/ |
44 |
> |
* @since 1.5 |
45 |
> |
* @author Doug Lea |
46 |
> |
*/ |
47 |
|
public class CopyOnWriteArrayList<E> |
48 |
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
49 |
|
|
50 |
|
/** |
51 |
< |
* The held array. Directly access only within synchronized |
51 |
> |
* The held array. Directly accessed only within synchronized |
52 |
|
* methods |
53 |
|
*/ |
54 |
|
private volatile transient E[] array_; |
71 |
|
* Constructs an list containing the elements of the specified |
72 |
|
* Collection, in the order they are returned by the Collection's |
73 |
|
* iterator. |
74 |
+ |
* @param c the collection of initially held elements |
75 |
|
*/ |
76 |
|
public CopyOnWriteArrayList(Collection<E> c) { |
77 |
|
array_ = new E[c.size()]; |
129 |
|
/** |
130 |
|
* Returns true if this list contains the specified element. |
131 |
|
* |
132 |
< |
* @param o element whose presence in this List is to be tested. |
132 |
> |
* @param elem element whose presence in this List is to be tested. |
133 |
|
*/ |
134 |
|
public boolean contains(Object elem) { |
135 |
|
E[] elementData = array(); |
298 |
|
* be stored, if it is big enough; otherwise, a new array of the |
299 |
|
* same runtime type is allocated for this purpose. |
300 |
|
* @return an array containing the elements of the list. |
301 |
< |
* @exception ArrayStoreException the runtime type of a is not a supertype |
301 |
> |
* @throws ArrayStoreException the runtime type of a is not a supertype |
302 |
|
* of the runtime type of every element in this list. |
303 |
|
*/ |
304 |
|
public <T> T[] toArray(T a[]) { |
323 |
|
* Returns the element at the specified position in this list. |
324 |
|
* |
325 |
|
* @param index index of element to return. |
326 |
< |
* @exception IndexOutOfBoundsException index is out of range (index |
326 |
> |
* @return the element |
327 |
> |
* @throws IndexOutOfBoundsException index is out of range (index |
328 |
|
* < 0 || index >= size()). |
329 |
|
*/ |
330 |
|
public E get(int index) { |
340 |
|
* @param index index of element to replace. |
341 |
|
* @param element element to be stored at the specified position. |
342 |
|
* @return the element previously at the specified position. |
343 |
< |
* @exception IndexOutOfBoundsException index out of range |
343 |
> |
* @throws IndexOutOfBoundsException index out of range |
344 |
|
* (index < 0 || index >= size()). |
345 |
|
*/ |
346 |
|
public synchronized E set(int index, E element) { |
381 |
|
* |
382 |
|
* @param index index at which the specified element is to be inserted. |
383 |
|
* @param element element to be inserted. |
384 |
< |
* @exception IndexOutOfBoundsException index is out of range |
384 |
> |
* @throws IndexOutOfBoundsException index is out of range |
385 |
|
* (index < 0 || index > size()). |
386 |
|
*/ |
387 |
|
public synchronized void add(int index, E element) { |
401 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
402 |
|
* indices). Returns the element that was removed from the list. |
403 |
|
* |
404 |
< |
* @exception IndexOutOfBoundsException index out of range (index |
404 |
> |
* @throws IndexOutOfBoundsException index out of range (index |
405 |
|
* < 0 || index >= size()). |
406 |
|
* @param index the index of the element to removed. |
407 |
|
*/ |
472 |
|
* toIndex==fromIndex, this operation has no effect.) |
473 |
|
* |
474 |
|
* @param fromIndex index of first element to be removed. |
475 |
< |
* @param fromIndex index after last element to be removed. |
476 |
< |
* @exception IndexOutOfBoundsException fromIndex or toIndex out of |
475 |
> |
* @param toIndex index after last element to be removed. |
476 |
> |
* @throws IndexOutOfBoundsException fromIndex or toIndex out of |
477 |
|
* range (fromIndex < 0 || fromIndex >= size() || toIndex |
478 |
|
* > size() || toIndex < fromIndex). |
479 |
|
*/ |
525 |
|
* each element returned by the Iterator in turn to see if it's |
526 |
|
* contained in this Collection. If all elements are so contained |
527 |
|
* true is returned, otherwise false. |
528 |
< |
* |
528 |
> |
* @param c the collection |
529 |
> |
* @return true if all elements are contained |
530 |
|
*/ |
531 |
|
public <T> boolean containsAll(Collection<T> c) { |
532 |
|
E[] elementData = array(); |
533 |
|
int len = elementData.length; |
534 |
|
Iterator<T> e = c.iterator(); |
535 |
|
while (e.hasNext()) |
536 |
< |
if(indexOf(e.next(), elementData, len) < 0) |
536 |
> |
if (indexOf(e.next(), elementData, len) < 0) |
537 |
|
return false; |
538 |
|
|
539 |
|
return true; |
546 |
|
* in this class because of the need for an internal temporary array. |
547 |
|
* <p> |
548 |
|
* |
549 |
+ |
* @param c the collection |
550 |
|
* @return true if this Collection changed as a result of the call. |
551 |
|
*/ |
552 |
|
public synchronized <T> boolean removeAll(Collection<T> c) { |
578 |
|
* specified Collection (optional operation). In other words, removes from |
579 |
|
* this Collection all of its elements that are not contained in the |
580 |
|
* specified Collection. |
581 |
+ |
* @param c the collection |
582 |
|
* @return true if this Collection changed as a result of the call. |
583 |
|
*/ |
584 |
|
public synchronized <T> boolean retainAll(Collection<T> c) { |
654 |
|
* specified Collection's Iterator. |
655 |
|
* |
656 |
|
* @param c elements to be inserted into this list. |
657 |
+ |
* @return true if any elements are added |
658 |
|
*/ |
659 |
|
public synchronized <T extends E> boolean addAll(Collection<T> c) { |
660 |
|
int numNew = c.size(); |
682 |
|
* @param index index at which to insert first element |
683 |
|
* from the specified collection. |
684 |
|
* @param c elements to be inserted into this list. |
685 |
< |
* @exception IndexOutOfBoundsException index out of range (index |
685 |
> |
* @throws IndexOutOfBoundsException index out of range (index |
686 |
|
* < 0 || index > size()). |
687 |
+ |
* @return true if any elements are added |
688 |
|
*/ |
689 |
|
public synchronized <T extends E> boolean addAll(int index, Collection<T> c) { |
690 |
|
int len = array_.length; |
722 |
|
* @serialData The length of the array backing the list is emitted |
723 |
|
* (int), followed by all of its elements (each an Object) |
724 |
|
* in the proper order. |
725 |
+ |
* @param s the stream |
726 |
|
*/ |
727 |
|
private void writeObject(java.io.ObjectOutputStream s) |
728 |
|
throws java.io.IOException{ |
741 |
|
|
742 |
|
/** |
743 |
|
* Reconstitute the list from a stream (i.e., deserialize it). |
744 |
+ |
* @param s the stream |
745 |
|
*/ |
746 |
|
private synchronized void readObject(java.io.ObjectInputStream s) |
747 |
|
throws java.io.IOException, ClassNotFoundException { |
872 |
|
* |
873 |
|
* @param index index of first element to be returned from the |
874 |
|
* ListIterator (by a call to getNext). |
875 |
< |
* @exception IndexOutOfBoundsException index is out of range |
875 |
> |
* @throws IndexOutOfBoundsException index is out of range |
876 |
|
* (index < 0 || index > size()). |
877 |
|
*/ |
878 |
|
public ListIterator<E> listIterator(final int index) { |
934 |
|
|
935 |
|
/** |
936 |
|
* Not supported. Always throws UnsupportedOperationException. |
937 |
< |
* @exception UnsupportedOperationException remove is not supported |
937 |
> |
* @throws UnsupportedOperationException remove is not supported |
938 |
|
* by this Iterator. |
939 |
|
*/ |
940 |
|
|
944 |
|
|
945 |
|
/** |
946 |
|
* Not supported. Always throws UnsupportedOperationException. |
947 |
< |
* @exception UnsupportedOperationException set is not supported |
947 |
> |
* @throws UnsupportedOperationException set is not supported |
948 |
|
* by this Iterator. |
949 |
|
*/ |
950 |
|
public void set(E o) { |
953 |
|
|
954 |
|
/** |
955 |
|
* Not supported. Always throws UnsupportedOperationException. |
956 |
< |
* @exception UnsupportedOperationException add is not supported |
956 |
> |
* @throws UnsupportedOperationException add is not supported |
957 |
|
* by this Iterator. |
958 |
|
*/ |
959 |
|
public void add(E o) { |
976 |
|
* a fashion that iterations in progress may yield incorrect results.) |
977 |
|
* |
978 |
|
* @param fromIndex low endpoint (inclusive) of the subList. |
979 |
< |
* @param toKey high endpoint (exclusive) of the subList. |
979 |
> |
* @param toIndex high endpoint (exclusive) of the subList. |
980 |
|
* @return a view of the specified range within this List. |
981 |
< |
* @exception IndexOutOfBoundsException Illegal endpoint index value |
981 |
> |
* @throws IndexOutOfBoundsException Illegal endpoint index value |
982 |
|
* (fromIndex < 0 || toIndex > size || fromIndex > toIndex). |
983 |
|
*/ |
984 |
|
public synchronized List<E> subList(int fromIndex, int toIndex) { |