1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group. Adapted and released under explicit permission |
4 |
< |
* from JDK1.2 ArrayList.java which carries the following copyright: |
4 |
> |
* from JDK ArrayList.java which carries the following copyright: |
5 |
|
* |
6 |
|
* Copyright 1997 by Sun Microsystems, Inc., |
7 |
|
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. |
26 |
|
* than alternatives when traversal operations vastly outnumber |
27 |
|
* mutations, and is useful when you cannot or don't want to |
28 |
|
* synchronize traversals, yet need to preclude interference among |
29 |
< |
* concurrent threads. The iterator method uses a reference to the |
30 |
< |
* state of the array at the point that the iterator was created. This |
31 |
< |
* array never changes during the lifetime of the iterator, so |
32 |
< |
* interference is impossible and the iterator is guaranteed not to |
33 |
< |
* throw <tt>ConcurrentModificationException</tt>. The iterator will |
34 |
< |
* not reflect additions, removals, or changes to the list since the |
35 |
< |
* iterator was created. Element-changing operations on iterators |
36 |
< |
* themselves (remove, set, and add) are not supported. These methods |
37 |
< |
* throw <tt>UnsupportedOperationException</tt>. |
29 |
> |
* concurrent threads. The "snapshot" style iterator method uses a |
30 |
> |
* reference to the state of the array at the point that the iterator |
31 |
> |
* was created. This array never changes during the lifetime of the |
32 |
> |
* iterator, so interference is impossible and the iterator is |
33 |
> |
* guaranteed not to throw <tt>ConcurrentModificationException</tt>. |
34 |
> |
* The iterator will not reflect additions, removals, or changes to |
35 |
> |
* the list since the iterator was created. Element-changing |
36 |
> |
* operations on iterators themselves (remove, set, and add) are not |
37 |
> |
* supported. These methods throw |
38 |
> |
* <tt>UnsupportedOperationException</tt>. |
39 |
|
* @since 1.5 |
40 |
|
* @author Doug Lea |
41 |
|
*/ |
56 |
|
private E[] array() { return array; } |
57 |
|
|
58 |
|
/** |
59 |
< |
* Constructs an empty list. |
59 |
> |
* Creates an empty list. |
60 |
|
*/ |
61 |
|
public CopyOnWriteArrayList() { |
62 |
|
array = (E[]) new Object[0]; |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* Constructs an list containing the elements of the specified |
66 |
> |
* Creates a list containing the elements of the specified |
67 |
|
* Collection, in the order they are returned by the Collection's |
68 |
|
* iterator. |
69 |
|
* @param c the collection of initially held elements |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* Returns the number of components in this list. |
108 |
> |
* Returns the number of elements in this list. |
109 |
|
* |
110 |
< |
* @return the number of components in this list. |
110 |
> |
* @return the number of elements in this list. |
111 |
|
*/ |
112 |
|
public int size() { |
113 |
|
return array().length; |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* Tests if this list has no components. |
117 |
> |
* Tests if this list has no elements. |
118 |
|
* |
119 |
< |
* @return <tt>true</tt> if this list has no components; |
119 |
> |
* @return <tt>true</tt> if this list has no elements; |
120 |
|
* <tt>false</tt> otherwise. |
121 |
|
*/ |
122 |
|
public boolean isEmpty() { |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* Returns true if this list contains the specified element. |
127 |
> |
* Returns <tt>true</tt> if this list contains the specified element. |
128 |
|
* |
129 |
|
* @param elem element whose presence in this List is to be tested. |
130 |
+ |
* @return <code>true</code> if the specified element is present; |
131 |
+ |
* <code>false</code> otherwise. |
132 |
|
*/ |
133 |
|
public boolean contains(Object elem) { |
134 |
|
E[] elementData = array(); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* Searches for the first occurence of the given argument, testing |
141 |
< |
* for equality using the <tt>equals</tt> method. |
140 |
> |
* Searches for the first occurence of the given argument, testing |
141 |
> |
* for equality using the <tt>equals</tt> method. |
142 |
|
* |
143 |
|
* @param elem an object. |
144 |
|
* @return the index of the first occurrence of the argument in this |
151 |
|
return indexOf(elem, elementData, len); |
152 |
|
} |
153 |
|
|
151 |
– |
|
154 |
|
/** |
155 |
|
* static version allows repeated call without needed |
156 |
|
* to grab lock for array each time |
200 |
|
* Returns the index of the last occurrence of the specified object in |
201 |
|
* this list. |
202 |
|
* |
203 |
< |
* @param elem the desired component. |
203 |
> |
* @param elem the desired element. |
204 |
|
* @return the index of the last occurrence of the specified object in |
205 |
|
* this list; returns -1 if the object is not found. |
206 |
|
*/ |
227 |
|
* Searches backwards for the specified object, starting from the |
228 |
|
* specified index, and returns an index to it. |
229 |
|
* |
230 |
< |
* @param elem the desired component. |
230 |
> |
* @param elem the desired element. |
231 |
|
* @param index the index to start searching from. |
232 |
|
* @return the index of the last occurrence of the specified object in this |
233 |
|
* List at position less than index in the List; |
270 |
|
/** |
271 |
|
* Returns an array containing all of the elements in this list |
272 |
|
* in the correct order. |
273 |
+ |
* @return an array containing all of the elements in this list |
274 |
+ |
* in the correct order. |
275 |
|
*/ |
276 |
|
public Object[] toArray() { |
277 |
|
Object[] elementData = array(); |
322 |
|
/** |
323 |
|
* Returns the element at the specified position in this list. |
324 |
|
* |
325 |
< |
* @param index index of element to return. |
326 |
< |
* @return the element |
327 |
< |
* @throws IndexOutOfBoundsException index is out of range (index |
328 |
< |
* < 0 || index >= size()). |
325 |
> |
* @param index index of element to return. |
326 |
> |
* @return the element at the specified position in this list. |
327 |
> |
* @throws IndexOutOfBoundsException if index is out of range <tt>(index |
328 |
> |
* < 0 || index >= size())</tt>. |
329 |
|
*/ |
330 |
|
public E get(int index) { |
331 |
|
E[] elementData = array(); |
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 |
< |
* @throws IndexOutOfBoundsException index out of range |
344 |
< |
* (index < 0 || index >= size()). |
343 |
> |
* @throws IndexOutOfBoundsException if index out of range |
344 |
> |
* <tt>(index < 0 || index >= size())</tt>. |
345 |
|
*/ |
346 |
|
public synchronized E set(int index, E element) { |
347 |
|
int len = array.length; |
381 |
|
* |
382 |
|
* @param index index at which the specified element is to be inserted. |
383 |
|
* @param element element to be inserted. |
384 |
< |
* @throws IndexOutOfBoundsException index is out of range |
385 |
< |
* (index < 0 || index > size()). |
384 |
> |
* @throws IndexOutOfBoundsException if index is out of range |
385 |
> |
* <tt>(index < 0 || index > size())</tt>. |
386 |
|
*/ |
387 |
|
public synchronized void add(int index, E element) { |
388 |
|
int len = array.length; |
399 |
|
/** |
400 |
|
* Removes the element at the specified position in this list. |
401 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
402 |
< |
* indices). Returns the element that was removed from the list. |
402 |
> |
* indices). |
403 |
|
* |
400 |
– |
* @throws IndexOutOfBoundsException index out of range (index |
401 |
– |
* < 0 || index >= size()). |
404 |
|
* @param index the index of the element to removed. |
405 |
+ |
* @return the element that was removed from the list. |
406 |
+ |
* @throws IndexOutOfBoundsException if index out of range <tt>(index |
407 |
+ |
* < 0 || index >= size())</tt>. |
408 |
|
*/ |
409 |
|
public synchronized E remove(int index) { |
410 |
|
int len = array.length; |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* Removes a single instance of the specified element from this Collection, |
424 |
< |
* if it is present (optional operation). More formally, removes an |
425 |
< |
* element <tt>e</tt> such that <tt>(o==null ? e==null : |
426 |
< |
* o.equals(e))</tt>, if the Collection contains one or more such |
427 |
< |
* elements. Returns true if the Collection contained the specified |
428 |
< |
* element (or equivalently, if the Collection changed as a result of the |
429 |
< |
* call). |
423 |
> |
* Removes a single instance of the specified element from this |
424 |
> |
* list, if it is present (optional operation). More formally, |
425 |
> |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
426 |
> |
* o.equals(e))</tt>, if the list contains one or more such |
427 |
> |
* elements. Returns <tt>true</tt> if the list contained the |
428 |
> |
* specified element (or equivalently, if the list changed as a |
429 |
> |
* result of the call).<p> |
430 |
|
* |
431 |
< |
* @param element element to be removed from this Collection, if present. |
432 |
< |
* @return true if the Collection changed as a result of the call. |
431 |
> |
* @param o element to be removed from this list, if present. |
432 |
> |
* @return <tt>true</tt> if the list contained the specified element. |
433 |
|
*/ |
434 |
< |
public synchronized boolean remove(Object element) { |
434 |
> |
public synchronized boolean remove(Object o) { |
435 |
|
int len = array.length; |
436 |
|
if (len == 0) return false; |
437 |
|
|
442 |
|
E[] newArray = (E[]) new Object[newlen]; |
443 |
|
|
444 |
|
for (int i = 0; i < newlen; ++i) { |
445 |
< |
if (element == array[i] || |
446 |
< |
(element != null && element.equals(array[i]))) { |
445 |
> |
if (o == array[i] || |
446 |
> |
(o != null && o.equals(array[i]))) { |
447 |
|
// found one; copy remaining and exit |
448 |
|
for (int k = i + 1; k < len; ++k) newArray[k-1] = array[k]; |
449 |
|
array = newArray; |
453 |
|
} |
454 |
|
// special handling for last cell |
455 |
|
|
456 |
< |
if (element == array[newlen] || |
457 |
< |
(element != null && element.equals(array[newlen]))) { |
456 |
> |
if (o == array[newlen] || |
457 |
> |
(o != null && o.equals(array[newlen]))) { |
458 |
|
array = newArray; |
459 |
|
return true; |
460 |
|
} else |
461 |
|
return false; // throw away copy |
457 |
– |
|
462 |
|
} |
463 |
|
|
464 |
|
|
466 |
|
* Removes from this List all of the elements whose index is between |
467 |
|
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
468 |
|
* elements to the left (reduces their index). |
469 |
< |
* This call shortens the List by (toIndex - fromIndex) elements. (If |
470 |
< |
* toIndex==fromIndex, this operation has no effect.) |
469 |
> |
* This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements. |
470 |
> |
* (If <tt>toIndex==fromIndex</tt>, this operation has no effect.) |
471 |
|
* |
472 |
|
* @param fromIndex index of first element to be removed. |
473 |
|
* @param toIndex index after last element to be removed. |
817 |
|
} |
818 |
|
|
819 |
|
/** |
820 |
< |
* Returns the hash code value for this List. |
821 |
< |
* <p> |
822 |
< |
* This implementation uses exactly the code that is used to define |
819 |
< |
* the List hash function in the documentation for List.hashCode. |
820 |
> |
* Returns the hash code value for this List. <p> This |
821 |
> |
* implementation uses the definition in {@link List#hashCode}. |
822 |
> |
* @return the hash code |
823 |
|
*/ |
824 |
|
public int hashCode() { |
825 |
|
int hashCode = 1; |