1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.17 |
* Expert Group. Adapted and released, under explicit permission, |
4 |
dl |
1.15 |
* from JDK ArrayList.java which carries the following copyright: |
5 |
dl |
1.3 |
* |
6 |
|
|
* Copyright 1997 by Sun Microsystems, Inc., |
7 |
|
|
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. |
8 |
|
|
* All rights reserved. |
9 |
|
|
* |
10 |
|
|
* This software is the confidential and proprietary information |
11 |
|
|
* of Sun Microsystems, Inc. ("Confidential Information"). You |
12 |
|
|
* shall not disclose such Confidential Information and shall use |
13 |
|
|
* it only in accordance with the terms of the license agreement |
14 |
|
|
* you entered into with Sun. |
15 |
dl |
1.2 |
*/ |
16 |
|
|
|
17 |
tim |
1.1 |
package java.util.concurrent; |
18 |
|
|
import java.util.*; |
19 |
dl |
1.42 |
import java.util.concurrent.locks.*; |
20 |
jsr166 |
1.45 |
import sun.misc.Unsafe; |
21 |
tim |
1.1 |
|
22 |
|
|
/** |
23 |
dl |
1.28 |
* A thread-safe variant of {@link java.util.ArrayList} in which all mutative |
24 |
jsr166 |
1.35 |
* operations (<tt>add</tt>, <tt>set</tt>, and so on) are implemented by |
25 |
|
|
* making a fresh copy of the underlying array. |
26 |
tim |
1.1 |
* |
27 |
dl |
1.26 |
* <p> This is ordinarily too costly, but may be <em>more</em> efficient |
28 |
dl |
1.12 |
* than alternatives when traversal operations vastly outnumber |
29 |
|
|
* mutations, and is useful when you cannot or don't want to |
30 |
|
|
* synchronize traversals, yet need to preclude interference among |
31 |
dl |
1.15 |
* concurrent threads. The "snapshot" style iterator method uses a |
32 |
|
|
* reference to the state of the array at the point that the iterator |
33 |
|
|
* was created. This array never changes during the lifetime of the |
34 |
|
|
* iterator, so interference is impossible and the iterator is |
35 |
|
|
* guaranteed not to throw <tt>ConcurrentModificationException</tt>. |
36 |
|
|
* The iterator will not reflect additions, removals, or changes to |
37 |
|
|
* the list since the iterator was created. Element-changing |
38 |
jsr166 |
1.35 |
* operations on iterators themselves (<tt>remove</tt>, <tt>set</tt>, and |
39 |
|
|
* <tt>add</tt>) are not supported. These methods throw |
40 |
dl |
1.15 |
* <tt>UnsupportedOperationException</tt>. |
41 |
dl |
1.24 |
* |
42 |
jsr166 |
1.35 |
* <p>All elements are permitted, including <tt>null</tt>. |
43 |
|
|
* |
44 |
jsr166 |
1.56 |
* <p>Memory consistency effects: As with other concurrent |
45 |
|
|
* collections, actions in a thread prior to placing an object into a |
46 |
|
|
* {@code CopyOnWriteArrayList} |
47 |
|
|
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
48 |
|
|
* actions subsequent to the access or removal of that element from |
49 |
|
|
* the {@code CopyOnWriteArrayList} in another thread. |
50 |
|
|
* |
51 |
dl |
1.24 |
* <p>This class is a member of the |
52 |
jsr166 |
1.64 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
53 |
dl |
1.24 |
* Java Collections Framework</a>. |
54 |
|
|
* |
55 |
dl |
1.6 |
* @since 1.5 |
56 |
|
|
* @author Doug Lea |
57 |
dl |
1.19 |
* @param <E> the type of elements held in this collection |
58 |
dl |
1.6 |
*/ |
59 |
tim |
1.1 |
public class CopyOnWriteArrayList<E> |
60 |
dl |
1.40 |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
61 |
dl |
1.11 |
private static final long serialVersionUID = 8673264195747942595L; |
62 |
tim |
1.1 |
|
63 |
dl |
1.42 |
/** The lock protecting all mutators */ |
64 |
|
|
transient final ReentrantLock lock = new ReentrantLock(); |
65 |
|
|
|
66 |
dl |
1.40 |
/** The array, accessed only via getArray/setArray. */ |
67 |
dl |
1.41 |
private volatile transient Object[] array; |
68 |
tim |
1.1 |
|
69 |
dl |
1.57 |
/** |
70 |
jsr166 |
1.60 |
* Gets the array. Non-private so as to also be accessible |
71 |
|
|
* from CopyOnWriteArraySet class. |
72 |
dl |
1.57 |
*/ |
73 |
jsr166 |
1.63 |
final Object[] getArray() { |
74 |
jsr166 |
1.59 |
return array; |
75 |
dl |
1.57 |
} |
76 |
|
|
|
77 |
|
|
/** |
78 |
jsr166 |
1.60 |
* Sets the array. |
79 |
dl |
1.57 |
*/ |
80 |
jsr166 |
1.59 |
final void setArray(Object[] a) { |
81 |
|
|
array = a; |
82 |
dl |
1.57 |
} |
83 |
tim |
1.1 |
|
84 |
|
|
/** |
85 |
dl |
1.15 |
* Creates an empty list. |
86 |
tim |
1.1 |
*/ |
87 |
|
|
public CopyOnWriteArrayList() { |
88 |
dl |
1.41 |
setArray(new Object[0]); |
89 |
tim |
1.1 |
} |
90 |
|
|
|
91 |
|
|
/** |
92 |
dl |
1.15 |
* Creates a list containing the elements of the specified |
93 |
jsr166 |
1.32 |
* collection, in the order they are returned by the collection's |
94 |
tim |
1.1 |
* iterator. |
95 |
jsr166 |
1.32 |
* |
96 |
dl |
1.6 |
* @param c the collection of initially held elements |
97 |
jsr166 |
1.35 |
* @throws NullPointerException if the specified collection is null |
98 |
tim |
1.1 |
*/ |
99 |
tim |
1.22 |
public CopyOnWriteArrayList(Collection<? extends E> c) { |
100 |
jsr166 |
1.67 |
Object[] elements = c.toArray(); |
101 |
|
|
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
102 |
|
|
if (elements.getClass() != Object[].class) |
103 |
|
|
elements = Arrays.copyOf(elements, elements.length, Object[].class); |
104 |
|
|
setArray(elements); |
105 |
tim |
1.1 |
} |
106 |
|
|
|
107 |
|
|
/** |
108 |
jsr166 |
1.35 |
* Creates a list holding a copy of the given array. |
109 |
tim |
1.9 |
* |
110 |
|
|
* @param toCopyIn the array (a copy of this array is used as the |
111 |
|
|
* internal array) |
112 |
jsr166 |
1.38 |
* @throws NullPointerException if the specified array is null |
113 |
jsr166 |
1.30 |
*/ |
114 |
tim |
1.1 |
public CopyOnWriteArrayList(E[] toCopyIn) { |
115 |
jsr166 |
1.67 |
setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class)); |
116 |
tim |
1.1 |
} |
117 |
|
|
|
118 |
|
|
/** |
119 |
dl |
1.15 |
* Returns the number of elements in this list. |
120 |
tim |
1.1 |
* |
121 |
jsr166 |
1.35 |
* @return the number of elements in this list |
122 |
tim |
1.1 |
*/ |
123 |
|
|
public int size() { |
124 |
dl |
1.40 |
return getArray().length; |
125 |
tim |
1.1 |
} |
126 |
|
|
|
127 |
|
|
/** |
128 |
jsr166 |
1.35 |
* Returns <tt>true</tt> if this list contains no elements. |
129 |
tim |
1.1 |
* |
130 |
jsr166 |
1.35 |
* @return <tt>true</tt> if this list contains no elements |
131 |
tim |
1.1 |
*/ |
132 |
|
|
public boolean isEmpty() { |
133 |
|
|
return size() == 0; |
134 |
|
|
} |
135 |
|
|
|
136 |
dl |
1.43 |
/** |
137 |
|
|
* Test for equality, coping with nulls. |
138 |
|
|
*/ |
139 |
|
|
private static boolean eq(Object o1, Object o2) { |
140 |
|
|
return (o1 == null ? o2 == null : o1.equals(o2)); |
141 |
|
|
} |
142 |
|
|
|
143 |
dl |
1.41 |
/** |
144 |
dl |
1.40 |
* static version of indexOf, to allow repeated calls without |
145 |
dl |
1.41 |
* needing to re-acquire array each time. |
146 |
dl |
1.40 |
* @param o element to search for |
147 |
|
|
* @param elements the array |
148 |
|
|
* @param index first index to search |
149 |
|
|
* @param fence one past last index to search |
150 |
|
|
* @return index of element, or -1 if absent |
151 |
|
|
*/ |
152 |
dl |
1.41 |
private static int indexOf(Object o, Object[] elements, |
153 |
dl |
1.40 |
int index, int fence) { |
154 |
|
|
if (o == null) { |
155 |
|
|
for (int i = index; i < fence; i++) |
156 |
|
|
if (elements[i] == null) |
157 |
|
|
return i; |
158 |
|
|
} else { |
159 |
|
|
for (int i = index; i < fence; i++) |
160 |
|
|
if (o.equals(elements[i])) |
161 |
|
|
return i; |
162 |
|
|
} |
163 |
|
|
return -1; |
164 |
|
|
} |
165 |
dl |
1.41 |
|
166 |
|
|
/** |
167 |
dl |
1.40 |
* static version of lastIndexOf. |
168 |
|
|
* @param o element to search for |
169 |
|
|
* @param elements the array |
170 |
|
|
* @param index first index to search |
171 |
|
|
* @return index of element, or -1 if absent |
172 |
|
|
*/ |
173 |
|
|
private static int lastIndexOf(Object o, Object[] elements, int index) { |
174 |
|
|
if (o == null) { |
175 |
|
|
for (int i = index; i >= 0; i--) |
176 |
|
|
if (elements[i] == null) |
177 |
|
|
return i; |
178 |
|
|
} else { |
179 |
|
|
for (int i = index; i >= 0; i--) |
180 |
|
|
if (o.equals(elements[i])) |
181 |
|
|
return i; |
182 |
|
|
} |
183 |
|
|
return -1; |
184 |
|
|
} |
185 |
|
|
|
186 |
tim |
1.1 |
/** |
187 |
dl |
1.15 |
* Returns <tt>true</tt> if this list contains the specified element. |
188 |
jsr166 |
1.35 |
* More formally, returns <tt>true</tt> if and only if this list contains |
189 |
|
|
* at least one element <tt>e</tt> such that |
190 |
|
|
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
191 |
tim |
1.1 |
* |
192 |
jsr166 |
1.35 |
* @param o element whose presence in this list is to be tested |
193 |
|
|
* @return <tt>true</tt> if this list contains the specified element |
194 |
tim |
1.1 |
*/ |
195 |
jsr166 |
1.35 |
public boolean contains(Object o) { |
196 |
dl |
1.41 |
Object[] elements = getArray(); |
197 |
dl |
1.40 |
return indexOf(o, elements, 0, elements.length) >= 0; |
198 |
tim |
1.1 |
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
jsr166 |
1.35 |
* {@inheritDoc} |
202 |
tim |
1.1 |
*/ |
203 |
jsr166 |
1.35 |
public int indexOf(Object o) { |
204 |
dl |
1.41 |
Object[] elements = getArray(); |
205 |
dl |
1.40 |
return indexOf(o, elements, 0, elements.length); |
206 |
tim |
1.1 |
} |
207 |
|
|
|
208 |
|
|
/** |
209 |
jsr166 |
1.35 |
* Returns the index of the first occurrence of the specified element in |
210 |
|
|
* this list, searching forwards from <tt>index</tt>, or returns -1 if |
211 |
|
|
* the element is not found. |
212 |
|
|
* More formally, returns the lowest index <tt>i</tt> such that |
213 |
|
|
* <tt>(i >= index && (e==null ? get(i)==null : e.equals(get(i))))</tt>, |
214 |
|
|
* or -1 if there is no such index. |
215 |
|
|
* |
216 |
|
|
* @param e element to search for |
217 |
|
|
* @param index index to start searching from |
218 |
|
|
* @return the index of the first occurrence of the element in |
219 |
|
|
* this list at position <tt>index</tt> or later in the list; |
220 |
|
|
* <tt>-1</tt> if the element is not found. |
221 |
|
|
* @throws IndexOutOfBoundsException if the specified index is negative |
222 |
tim |
1.1 |
*/ |
223 |
jsr166 |
1.35 |
public int indexOf(E e, int index) { |
224 |
dl |
1.41 |
Object[] elements = getArray(); |
225 |
jsr166 |
1.67 |
return indexOf(e, elements, index, elements.length); |
226 |
tim |
1.1 |
} |
227 |
|
|
|
228 |
|
|
/** |
229 |
jsr166 |
1.35 |
* {@inheritDoc} |
230 |
tim |
1.1 |
*/ |
231 |
jsr166 |
1.35 |
public int lastIndexOf(Object o) { |
232 |
dl |
1.41 |
Object[] elements = getArray(); |
233 |
dl |
1.40 |
return lastIndexOf(o, elements, elements.length - 1); |
234 |
tim |
1.1 |
} |
235 |
|
|
|
236 |
|
|
/** |
237 |
jsr166 |
1.35 |
* Returns the index of the last occurrence of the specified element in |
238 |
|
|
* this list, searching backwards from <tt>index</tt>, or returns -1 if |
239 |
|
|
* the element is not found. |
240 |
|
|
* More formally, returns the highest index <tt>i</tt> such that |
241 |
|
|
* <tt>(i <= index && (e==null ? get(i)==null : e.equals(get(i))))</tt>, |
242 |
|
|
* or -1 if there is no such index. |
243 |
|
|
* |
244 |
|
|
* @param e element to search for |
245 |
|
|
* @param index index to start searching backwards from |
246 |
|
|
* @return the index of the last occurrence of the element at position |
247 |
|
|
* less than or equal to <tt>index</tt> in this list; |
248 |
|
|
* -1 if the element is not found. |
249 |
|
|
* @throws IndexOutOfBoundsException if the specified index is greater |
250 |
|
|
* than or equal to the current size of this list |
251 |
tim |
1.1 |
*/ |
252 |
jsr166 |
1.35 |
public int lastIndexOf(E e, int index) { |
253 |
dl |
1.41 |
Object[] elements = getArray(); |
254 |
jsr166 |
1.67 |
return lastIndexOf(e, elements, index); |
255 |
tim |
1.1 |
} |
256 |
|
|
|
257 |
|
|
/** |
258 |
|
|
* Returns a shallow copy of this list. (The elements themselves |
259 |
|
|
* are not copied.) |
260 |
|
|
* |
261 |
jsr166 |
1.35 |
* @return a clone of this list |
262 |
tim |
1.1 |
*/ |
263 |
|
|
public Object clone() { |
264 |
|
|
try { |
265 |
dl |
1.52 |
CopyOnWriteArrayList c = (CopyOnWriteArrayList)(super.clone()); |
266 |
|
|
c.resetLock(); |
267 |
|
|
return c; |
268 |
tim |
1.1 |
} catch (CloneNotSupportedException e) { |
269 |
|
|
// this shouldn't happen, since we are Cloneable |
270 |
|
|
throw new InternalError(); |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
/** |
275 |
|
|
* Returns an array containing all of the elements in this list |
276 |
jsr166 |
1.35 |
* in proper sequence (from first to last element). |
277 |
|
|
* |
278 |
|
|
* <p>The returned array will be "safe" in that no references to it are |
279 |
|
|
* maintained by this list. (In other words, this method must allocate |
280 |
|
|
* a new array). The caller is thus free to modify the returned array. |
281 |
jsr166 |
1.36 |
* |
282 |
jsr166 |
1.35 |
* <p>This method acts as bridge between array-based and collection-based |
283 |
|
|
* APIs. |
284 |
|
|
* |
285 |
|
|
* @return an array containing all the elements in this list |
286 |
tim |
1.1 |
*/ |
287 |
|
|
public Object[] toArray() { |
288 |
dl |
1.40 |
Object[] elements = getArray(); |
289 |
jsr166 |
1.67 |
return Arrays.copyOf(elements, elements.length); |
290 |
tim |
1.1 |
} |
291 |
|
|
|
292 |
|
|
/** |
293 |
jsr166 |
1.35 |
* Returns an array containing all of the elements in this list in |
294 |
|
|
* proper sequence (from first to last element); the runtime type of |
295 |
|
|
* the returned array is that of the specified array. If the list fits |
296 |
|
|
* in the specified array, it is returned therein. Otherwise, a new |
297 |
|
|
* array is allocated with the runtime type of the specified array and |
298 |
|
|
* the size of this list. |
299 |
jsr166 |
1.32 |
* |
300 |
|
|
* <p>If this list fits in the specified array with room to spare |
301 |
jsr166 |
1.35 |
* (i.e., the array has more elements than this list), the element in |
302 |
|
|
* the array immediately following the end of the list is set to |
303 |
|
|
* <tt>null</tt>. (This is useful in determining the length of this |
304 |
|
|
* list <i>only</i> if the caller knows that this list does not contain |
305 |
|
|
* any null elements.) |
306 |
|
|
* |
307 |
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
308 |
|
|
* array-based and collection-based APIs. Further, this method allows |
309 |
|
|
* precise control over the runtime type of the output array, and may, |
310 |
|
|
* under certain circumstances, be used to save allocation costs. |
311 |
|
|
* |
312 |
|
|
* <p>Suppose <tt>x</tt> is a list known to contain only strings. |
313 |
|
|
* The following code can be used to dump the list into a newly |
314 |
|
|
* allocated array of <tt>String</tt>: |
315 |
|
|
* |
316 |
|
|
* <pre> |
317 |
|
|
* String[] y = x.toArray(new String[0]);</pre> |
318 |
|
|
* |
319 |
|
|
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
320 |
|
|
* <tt>toArray()</tt>. |
321 |
tim |
1.1 |
* |
322 |
|
|
* @param a the array into which the elements of the list are to |
323 |
jsr166 |
1.35 |
* be stored, if it is big enough; otherwise, a new array of the |
324 |
|
|
* same runtime type is allocated for this purpose. |
325 |
|
|
* @return an array containing all the elements in this list |
326 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
327 |
|
|
* is not a supertype of the runtime type of every element in |
328 |
|
|
* this list |
329 |
|
|
* @throws NullPointerException if the specified array is null |
330 |
tim |
1.1 |
*/ |
331 |
jsr166 |
1.66 |
@SuppressWarnings("unchecked") |
332 |
tim |
1.1 |
public <T> T[] toArray(T a[]) { |
333 |
dl |
1.41 |
Object[] elements = getArray(); |
334 |
dl |
1.40 |
int len = elements.length; |
335 |
|
|
if (a.length < len) |
336 |
jsr166 |
1.67 |
return (T[]) Arrays.copyOf(elements, len, a.getClass()); |
337 |
|
|
else { |
338 |
|
|
System.arraycopy(elements, 0, a, 0, len); |
339 |
|
|
if (a.length > len) |
340 |
|
|
a[len] = null; |
341 |
|
|
return a; |
342 |
|
|
} |
343 |
tim |
1.1 |
} |
344 |
|
|
|
345 |
|
|
// Positional Access Operations |
346 |
|
|
|
347 |
jsr166 |
1.66 |
@SuppressWarnings("unchecked") |
348 |
|
|
private E get(Object[] a, int index) { |
349 |
jsr166 |
1.67 |
return (E) a[index]; |
350 |
jsr166 |
1.66 |
} |
351 |
|
|
|
352 |
tim |
1.1 |
/** |
353 |
jsr166 |
1.35 |
* {@inheritDoc} |
354 |
tim |
1.1 |
* |
355 |
jsr166 |
1.35 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
356 |
tim |
1.1 |
*/ |
357 |
|
|
public E get(int index) { |
358 |
jsr166 |
1.66 |
return get(getArray(), index); |
359 |
tim |
1.1 |
} |
360 |
|
|
|
361 |
|
|
/** |
362 |
jsr166 |
1.35 |
* Replaces the element at the specified position in this list with the |
363 |
|
|
* specified element. |
364 |
tim |
1.1 |
* |
365 |
jsr166 |
1.35 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
366 |
tim |
1.1 |
*/ |
367 |
dl |
1.42 |
public E set(int index, E element) { |
368 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
369 |
|
|
lock.lock(); |
370 |
|
|
try { |
371 |
|
|
Object[] elements = getArray(); |
372 |
|
|
E oldValue = get(elements, index); |
373 |
|
|
|
374 |
|
|
if (oldValue != element) { |
375 |
|
|
int len = elements.length; |
376 |
|
|
Object[] newElements = Arrays.copyOf(elements, len); |
377 |
|
|
newElements[index] = element; |
378 |
|
|
setArray(newElements); |
379 |
|
|
} else { |
380 |
|
|
// Not quite a no-op; ensures volatile write semantics |
381 |
|
|
setArray(elements); |
382 |
|
|
} |
383 |
|
|
return oldValue; |
384 |
|
|
} finally { |
385 |
|
|
lock.unlock(); |
386 |
|
|
} |
387 |
tim |
1.1 |
} |
388 |
|
|
|
389 |
|
|
/** |
390 |
|
|
* Appends the specified element to the end of this list. |
391 |
|
|
* |
392 |
dl |
1.40 |
* @param e element to be appended to this list |
393 |
jsr166 |
1.49 |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
394 |
tim |
1.1 |
*/ |
395 |
dl |
1.42 |
public boolean add(E e) { |
396 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
397 |
|
|
lock.lock(); |
398 |
|
|
try { |
399 |
|
|
Object[] elements = getArray(); |
400 |
|
|
int len = elements.length; |
401 |
|
|
Object[] newElements = Arrays.copyOf(elements, len + 1); |
402 |
|
|
newElements[len] = e; |
403 |
|
|
setArray(newElements); |
404 |
|
|
return true; |
405 |
|
|
} finally { |
406 |
|
|
lock.unlock(); |
407 |
|
|
} |
408 |
tim |
1.1 |
} |
409 |
|
|
|
410 |
|
|
/** |
411 |
|
|
* Inserts the specified element at the specified position in this |
412 |
|
|
* list. Shifts the element currently at that position (if any) and |
413 |
|
|
* any subsequent elements to the right (adds one to their indices). |
414 |
|
|
* |
415 |
jsr166 |
1.35 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
416 |
tim |
1.1 |
*/ |
417 |
dl |
1.42 |
public void add(int index, E element) { |
418 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
419 |
|
|
lock.lock(); |
420 |
|
|
try { |
421 |
|
|
Object[] elements = getArray(); |
422 |
|
|
int len = elements.length; |
423 |
|
|
if (index > len || index < 0) |
424 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
425 |
|
|
", Size: "+len); |
426 |
|
|
Object[] newElements; |
427 |
|
|
int numMoved = len - index; |
428 |
|
|
if (numMoved == 0) |
429 |
|
|
newElements = Arrays.copyOf(elements, len + 1); |
430 |
|
|
else { |
431 |
|
|
newElements = new Object[len + 1]; |
432 |
|
|
System.arraycopy(elements, 0, newElements, 0, index); |
433 |
|
|
System.arraycopy(elements, index, newElements, index + 1, |
434 |
|
|
numMoved); |
435 |
|
|
} |
436 |
|
|
newElements[index] = element; |
437 |
|
|
setArray(newElements); |
438 |
|
|
} finally { |
439 |
|
|
lock.unlock(); |
440 |
|
|
} |
441 |
tim |
1.1 |
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
|
|
* Removes the element at the specified position in this list. |
445 |
|
|
* Shifts any subsequent elements to the left (subtracts one from their |
446 |
jsr166 |
1.35 |
* indices). Returns the element that was removed from the list. |
447 |
tim |
1.1 |
* |
448 |
jsr166 |
1.35 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
449 |
tim |
1.1 |
*/ |
450 |
dl |
1.42 |
public E remove(int index) { |
451 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
452 |
|
|
lock.lock(); |
453 |
|
|
try { |
454 |
|
|
Object[] elements = getArray(); |
455 |
|
|
int len = elements.length; |
456 |
|
|
E oldValue = get(elements, index); |
457 |
|
|
int numMoved = len - index - 1; |
458 |
|
|
if (numMoved == 0) |
459 |
|
|
setArray(Arrays.copyOf(elements, len - 1)); |
460 |
|
|
else { |
461 |
|
|
Object[] newElements = new Object[len - 1]; |
462 |
|
|
System.arraycopy(elements, 0, newElements, 0, index); |
463 |
|
|
System.arraycopy(elements, index + 1, newElements, index, |
464 |
|
|
numMoved); |
465 |
|
|
setArray(newElements); |
466 |
|
|
} |
467 |
|
|
return oldValue; |
468 |
|
|
} finally { |
469 |
|
|
lock.unlock(); |
470 |
|
|
} |
471 |
tim |
1.1 |
} |
472 |
|
|
|
473 |
|
|
/** |
474 |
jsr166 |
1.35 |
* Removes the first occurrence of the specified element from this list, |
475 |
|
|
* if it is present. If this list does not contain the element, it is |
476 |
|
|
* unchanged. More formally, removes the element with the lowest index |
477 |
|
|
* <tt>i</tt> such that |
478 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
479 |
|
|
* (if such an element exists). Returns <tt>true</tt> if this list |
480 |
|
|
* contained the specified element (or equivalently, if this list |
481 |
|
|
* changed as a result of the call). |
482 |
tim |
1.1 |
* |
483 |
jsr166 |
1.35 |
* @param o element to be removed from this list, if present |
484 |
|
|
* @return <tt>true</tt> if this list contained the specified element |
485 |
tim |
1.1 |
*/ |
486 |
dl |
1.42 |
public boolean remove(Object o) { |
487 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
488 |
|
|
lock.lock(); |
489 |
|
|
try { |
490 |
|
|
Object[] elements = getArray(); |
491 |
|
|
int len = elements.length; |
492 |
|
|
if (len != 0) { |
493 |
|
|
// Copy while searching for element to remove |
494 |
|
|
// This wins in the normal case of element being present |
495 |
|
|
int newlen = len - 1; |
496 |
|
|
Object[] newElements = new Object[newlen]; |
497 |
|
|
|
498 |
|
|
for (int i = 0; i < newlen; ++i) { |
499 |
|
|
if (eq(o, elements[i])) { |
500 |
|
|
// found one; copy remaining and exit |
501 |
|
|
for (int k = i + 1; k < len; ++k) |
502 |
|
|
newElements[k-1] = elements[k]; |
503 |
|
|
setArray(newElements); |
504 |
|
|
return true; |
505 |
|
|
} else |
506 |
|
|
newElements[i] = elements[i]; |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
// special handling for last cell |
510 |
|
|
if (eq(o, elements[newlen])) { |
511 |
|
|
setArray(newElements); |
512 |
|
|
return true; |
513 |
|
|
} |
514 |
|
|
} |
515 |
|
|
return false; |
516 |
|
|
} finally { |
517 |
|
|
lock.unlock(); |
518 |
|
|
} |
519 |
tim |
1.1 |
} |
520 |
|
|
|
521 |
|
|
/** |
522 |
jsr166 |
1.35 |
* Removes from this list all of the elements whose index is between |
523 |
|
|
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. |
524 |
|
|
* Shifts any succeeding elements to the left (reduces their index). |
525 |
dl |
1.15 |
* This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements. |
526 |
|
|
* (If <tt>toIndex==fromIndex</tt>, this operation has no effect.) |
527 |
tim |
1.1 |
* |
528 |
jsr166 |
1.35 |
* @param fromIndex index of first element to be removed |
529 |
|
|
* @param toIndex index after last element to be removed |
530 |
jsr166 |
1.66 |
* @throws IndexOutOfBoundsException if fromIndex or toIndex out of range |
531 |
jsr166 |
1.69 |
* ({@code{fromIndex < 0 || toIndex > size() || toIndex < fromIndex}) |
532 |
tim |
1.1 |
*/ |
533 |
dl |
1.42 |
private void removeRange(int fromIndex, int toIndex) { |
534 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
535 |
|
|
lock.lock(); |
536 |
|
|
try { |
537 |
|
|
Object[] elements = getArray(); |
538 |
|
|
int len = elements.length; |
539 |
|
|
|
540 |
|
|
if (fromIndex < 0 || toIndex > len || toIndex < fromIndex) |
541 |
|
|
throw new IndexOutOfBoundsException(); |
542 |
|
|
int newlen = len - (toIndex - fromIndex); |
543 |
|
|
int numMoved = len - toIndex; |
544 |
|
|
if (numMoved == 0) |
545 |
|
|
setArray(Arrays.copyOf(elements, newlen)); |
546 |
|
|
else { |
547 |
|
|
Object[] newElements = new Object[newlen]; |
548 |
|
|
System.arraycopy(elements, 0, newElements, 0, fromIndex); |
549 |
|
|
System.arraycopy(elements, toIndex, newElements, |
550 |
|
|
fromIndex, numMoved); |
551 |
|
|
setArray(newElements); |
552 |
|
|
} |
553 |
|
|
} finally { |
554 |
|
|
lock.unlock(); |
555 |
|
|
} |
556 |
tim |
1.1 |
} |
557 |
|
|
|
558 |
|
|
/** |
559 |
|
|
* Append the element if not present. |
560 |
jsr166 |
1.38 |
* |
561 |
dl |
1.40 |
* @param e element to be added to this list, if absent |
562 |
jsr166 |
1.38 |
* @return <tt>true</tt> if the element was added |
563 |
jsr166 |
1.30 |
*/ |
564 |
dl |
1.42 |
public boolean addIfAbsent(E e) { |
565 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
566 |
|
|
lock.lock(); |
567 |
|
|
try { |
568 |
|
|
// Copy while checking if already present. |
569 |
|
|
// This wins in the most common case where it is not present |
570 |
|
|
Object[] elements = getArray(); |
571 |
|
|
int len = elements.length; |
572 |
|
|
Object[] newElements = new Object[len + 1]; |
573 |
|
|
for (int i = 0; i < len; ++i) { |
574 |
|
|
if (eq(e, elements[i])) |
575 |
|
|
return false; // exit, throwing away copy |
576 |
|
|
else |
577 |
|
|
newElements[i] = elements[i]; |
578 |
|
|
} |
579 |
|
|
newElements[len] = e; |
580 |
|
|
setArray(newElements); |
581 |
|
|
return true; |
582 |
|
|
} finally { |
583 |
|
|
lock.unlock(); |
584 |
|
|
} |
585 |
tim |
1.1 |
} |
586 |
|
|
|
587 |
|
|
/** |
588 |
jsr166 |
1.35 |
* Returns <tt>true</tt> if this list contains all of the elements of the |
589 |
jsr166 |
1.32 |
* specified collection. |
590 |
jsr166 |
1.34 |
* |
591 |
jsr166 |
1.36 |
* @param c collection to be checked for containment in this list |
592 |
jsr166 |
1.35 |
* @return <tt>true</tt> if this list contains all of the elements of the |
593 |
|
|
* specified collection |
594 |
|
|
* @throws NullPointerException if the specified collection is null |
595 |
jsr166 |
1.38 |
* @see #contains(Object) |
596 |
tim |
1.1 |
*/ |
597 |
tim |
1.7 |
public boolean containsAll(Collection<?> c) { |
598 |
dl |
1.41 |
Object[] elements = getArray(); |
599 |
dl |
1.40 |
int len = elements.length; |
600 |
jsr166 |
1.67 |
for (Object e : c) { |
601 |
dl |
1.41 |
if (indexOf(e, elements, 0, len) < 0) |
602 |
tim |
1.1 |
return false; |
603 |
jsr166 |
1.67 |
} |
604 |
tim |
1.1 |
return true; |
605 |
|
|
} |
606 |
|
|
|
607 |
|
|
/** |
608 |
jsr166 |
1.32 |
* Removes from this list all of its elements that are contained in |
609 |
|
|
* the specified collection. This is a particularly expensive operation |
610 |
tim |
1.1 |
* in this class because of the need for an internal temporary array. |
611 |
|
|
* |
612 |
jsr166 |
1.35 |
* @param c collection containing elements to be removed from this list |
613 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
614 |
jsr166 |
1.38 |
* @throws ClassCastException if the class of an element of this list |
615 |
|
|
* is incompatible with the specified collection (optional) |
616 |
|
|
* @throws NullPointerException if this list contains a null element and the |
617 |
|
|
* specified collection does not permit null elements (optional), |
618 |
|
|
* or if the specified collection is null |
619 |
|
|
* @see #remove(Object) |
620 |
tim |
1.1 |
*/ |
621 |
dl |
1.42 |
public boolean removeAll(Collection<?> c) { |
622 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
623 |
|
|
lock.lock(); |
624 |
|
|
try { |
625 |
|
|
Object[] elements = getArray(); |
626 |
|
|
int len = elements.length; |
627 |
|
|
if (len != 0) { |
628 |
|
|
// temp array holds those elements we know we want to keep |
629 |
|
|
int newlen = 0; |
630 |
|
|
Object[] temp = new Object[len]; |
631 |
|
|
for (int i = 0; i < len; ++i) { |
632 |
|
|
Object element = elements[i]; |
633 |
|
|
if (!c.contains(element)) |
634 |
|
|
temp[newlen++] = element; |
635 |
|
|
} |
636 |
|
|
if (newlen != len) { |
637 |
|
|
setArray(Arrays.copyOf(temp, newlen)); |
638 |
|
|
return true; |
639 |
|
|
} |
640 |
|
|
} |
641 |
|
|
return false; |
642 |
|
|
} finally { |
643 |
|
|
lock.unlock(); |
644 |
|
|
} |
645 |
tim |
1.1 |
} |
646 |
|
|
|
647 |
|
|
/** |
648 |
jsr166 |
1.32 |
* Retains only the elements in this list that are contained in the |
649 |
jsr166 |
1.35 |
* specified collection. In other words, removes from this list all of |
650 |
|
|
* its elements that are not contained in the specified collection. |
651 |
jsr166 |
1.32 |
* |
652 |
jsr166 |
1.35 |
* @param c collection containing elements to be retained in this list |
653 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
654 |
jsr166 |
1.38 |
* @throws ClassCastException if the class of an element of this list |
655 |
|
|
* is incompatible with the specified collection (optional) |
656 |
|
|
* @throws NullPointerException if this list contains a null element and the |
657 |
|
|
* specified collection does not permit null elements (optional), |
658 |
|
|
* or if the specified collection is null |
659 |
|
|
* @see #remove(Object) |
660 |
tim |
1.1 |
*/ |
661 |
dl |
1.42 |
public boolean retainAll(Collection<?> c) { |
662 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
663 |
|
|
lock.lock(); |
664 |
|
|
try { |
665 |
|
|
Object[] elements = getArray(); |
666 |
|
|
int len = elements.length; |
667 |
|
|
if (len != 0) { |
668 |
|
|
// temp array holds those elements we know we want to keep |
669 |
|
|
int newlen = 0; |
670 |
|
|
Object[] temp = new Object[len]; |
671 |
|
|
for (int i = 0; i < len; ++i) { |
672 |
|
|
Object element = elements[i]; |
673 |
|
|
if (c.contains(element)) |
674 |
|
|
temp[newlen++] = element; |
675 |
|
|
} |
676 |
|
|
if (newlen != len) { |
677 |
|
|
setArray(Arrays.copyOf(temp, newlen)); |
678 |
|
|
return true; |
679 |
|
|
} |
680 |
|
|
} |
681 |
|
|
return false; |
682 |
|
|
} finally { |
683 |
|
|
lock.unlock(); |
684 |
|
|
} |
685 |
tim |
1.1 |
} |
686 |
|
|
|
687 |
|
|
/** |
688 |
jsr166 |
1.32 |
* Appends all of the elements in the specified collection that |
689 |
tim |
1.1 |
* are not already contained in this list, to the end of |
690 |
|
|
* this list, in the order that they are returned by the |
691 |
jsr166 |
1.32 |
* specified collection's iterator. |
692 |
tim |
1.1 |
* |
693 |
jsr166 |
1.36 |
* @param c collection containing elements to be added to this list |
694 |
tim |
1.1 |
* @return the number of elements added |
695 |
jsr166 |
1.35 |
* @throws NullPointerException if the specified collection is null |
696 |
jsr166 |
1.38 |
* @see #addIfAbsent(Object) |
697 |
tim |
1.1 |
*/ |
698 |
dl |
1.42 |
public int addAllAbsent(Collection<? extends E> c) { |
699 |
jsr166 |
1.67 |
Object[] cs = c.toArray(); |
700 |
|
|
if (cs.length == 0) |
701 |
|
|
return 0; |
702 |
|
|
Object[] uniq = new Object[cs.length]; |
703 |
|
|
final ReentrantLock lock = this.lock; |
704 |
|
|
lock.lock(); |
705 |
|
|
try { |
706 |
|
|
Object[] elements = getArray(); |
707 |
|
|
int len = elements.length; |
708 |
|
|
int added = 0; |
709 |
|
|
for (int i = 0; i < cs.length; ++i) { // scan for duplicates |
710 |
|
|
Object e = cs[i]; |
711 |
|
|
if (indexOf(e, elements, 0, len) < 0 && |
712 |
|
|
indexOf(e, uniq, 0, added) < 0) |
713 |
|
|
uniq[added++] = e; |
714 |
|
|
} |
715 |
|
|
if (added > 0) { |
716 |
|
|
Object[] newElements = Arrays.copyOf(elements, len + added); |
717 |
|
|
System.arraycopy(uniq, 0, newElements, len, added); |
718 |
|
|
setArray(newElements); |
719 |
|
|
} |
720 |
|
|
return added; |
721 |
|
|
} finally { |
722 |
|
|
lock.unlock(); |
723 |
|
|
} |
724 |
tim |
1.1 |
} |
725 |
|
|
|
726 |
|
|
/** |
727 |
|
|
* Removes all of the elements from this list. |
728 |
jsr166 |
1.38 |
* The list will be empty after this call returns. |
729 |
tim |
1.1 |
*/ |
730 |
dl |
1.42 |
public void clear() { |
731 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
732 |
|
|
lock.lock(); |
733 |
|
|
try { |
734 |
|
|
setArray(new Object[0]); |
735 |
|
|
} finally { |
736 |
|
|
lock.unlock(); |
737 |
|
|
} |
738 |
tim |
1.1 |
} |
739 |
|
|
|
740 |
|
|
/** |
741 |
jsr166 |
1.32 |
* Appends all of the elements in the specified collection to the end |
742 |
|
|
* of this list, in the order that they are returned by the specified |
743 |
|
|
* collection's iterator. |
744 |
tim |
1.1 |
* |
745 |
jsr166 |
1.36 |
* @param c collection containing elements to be added to this list |
746 |
jsr166 |
1.38 |
* @return <tt>true</tt> if this list changed as a result of the call |
747 |
jsr166 |
1.35 |
* @throws NullPointerException if the specified collection is null |
748 |
jsr166 |
1.38 |
* @see #add(Object) |
749 |
tim |
1.1 |
*/ |
750 |
dl |
1.42 |
public boolean addAll(Collection<? extends E> c) { |
751 |
jsr166 |
1.67 |
Object[] cs = c.toArray(); |
752 |
|
|
if (cs.length == 0) |
753 |
|
|
return false; |
754 |
|
|
final ReentrantLock lock = this.lock; |
755 |
|
|
lock.lock(); |
756 |
|
|
try { |
757 |
|
|
Object[] elements = getArray(); |
758 |
|
|
int len = elements.length; |
759 |
|
|
Object[] newElements = Arrays.copyOf(elements, len + cs.length); |
760 |
|
|
System.arraycopy(cs, 0, newElements, len, cs.length); |
761 |
|
|
setArray(newElements); |
762 |
|
|
return true; |
763 |
|
|
} finally { |
764 |
|
|
lock.unlock(); |
765 |
|
|
} |
766 |
tim |
1.1 |
} |
767 |
|
|
|
768 |
|
|
/** |
769 |
jsr166 |
1.35 |
* Inserts all of the elements in the specified collection into this |
770 |
tim |
1.1 |
* list, starting at the specified position. Shifts the element |
771 |
|
|
* currently at that position (if any) and any subsequent elements to |
772 |
|
|
* the right (increases their indices). The new elements will appear |
773 |
jsr166 |
1.38 |
* in this list in the order that they are returned by the |
774 |
|
|
* specified collection's iterator. |
775 |
tim |
1.1 |
* |
776 |
jsr166 |
1.35 |
* @param index index at which to insert the first element |
777 |
|
|
* from the specified collection |
778 |
jsr166 |
1.36 |
* @param c collection containing elements to be added to this list |
779 |
jsr166 |
1.35 |
* @return <tt>true</tt> if this list changed as a result of the call |
780 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
781 |
|
|
* @throws NullPointerException if the specified collection is null |
782 |
jsr166 |
1.38 |
* @see #add(int,Object) |
783 |
tim |
1.1 |
*/ |
784 |
dl |
1.42 |
public boolean addAll(int index, Collection<? extends E> c) { |
785 |
jsr166 |
1.67 |
Object[] cs = c.toArray(); |
786 |
|
|
final ReentrantLock lock = this.lock; |
787 |
|
|
lock.lock(); |
788 |
|
|
try { |
789 |
|
|
Object[] elements = getArray(); |
790 |
|
|
int len = elements.length; |
791 |
|
|
if (index > len || index < 0) |
792 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
793 |
|
|
", Size: "+len); |
794 |
|
|
if (cs.length == 0) |
795 |
|
|
return false; |
796 |
|
|
int numMoved = len - index; |
797 |
|
|
Object[] newElements; |
798 |
|
|
if (numMoved == 0) |
799 |
|
|
newElements = Arrays.copyOf(elements, len + cs.length); |
800 |
|
|
else { |
801 |
|
|
newElements = new Object[len + cs.length]; |
802 |
|
|
System.arraycopy(elements, 0, newElements, 0, index); |
803 |
|
|
System.arraycopy(elements, index, |
804 |
|
|
newElements, index + cs.length, |
805 |
|
|
numMoved); |
806 |
|
|
} |
807 |
|
|
System.arraycopy(cs, 0, newElements, index, cs.length); |
808 |
|
|
setArray(newElements); |
809 |
|
|
return true; |
810 |
|
|
} finally { |
811 |
|
|
lock.unlock(); |
812 |
|
|
} |
813 |
tim |
1.1 |
} |
814 |
|
|
|
815 |
|
|
/** |
816 |
jsr166 |
1.70 |
* Saves the state of the list to a stream (that is, serializes it). |
817 |
tim |
1.1 |
* |
818 |
|
|
* @serialData The length of the array backing the list is emitted |
819 |
|
|
* (int), followed by all of its elements (each an Object) |
820 |
|
|
* in the proper order. |
821 |
dl |
1.6 |
* @param s the stream |
822 |
tim |
1.1 |
*/ |
823 |
|
|
private void writeObject(java.io.ObjectOutputStream s) |
824 |
|
|
throws java.io.IOException{ |
825 |
|
|
|
826 |
|
|
s.defaultWriteObject(); |
827 |
|
|
|
828 |
dl |
1.41 |
Object[] elements = getArray(); |
829 |
jsr166 |
1.67 |
int len = elements.length; |
830 |
tim |
1.1 |
// Write out array length |
831 |
dl |
1.40 |
s.writeInt(len); |
832 |
tim |
1.1 |
|
833 |
|
|
// Write out all elements in the proper order. |
834 |
dl |
1.40 |
for (int i = 0; i < len; i++) |
835 |
|
|
s.writeObject(elements[i]); |
836 |
tim |
1.1 |
} |
837 |
|
|
|
838 |
|
|
/** |
839 |
jsr166 |
1.70 |
* Reconstitutes the list from a stream (that is, deserializes it). |
840 |
|
|
* |
841 |
dl |
1.6 |
* @param s the stream |
842 |
tim |
1.1 |
*/ |
843 |
dl |
1.12 |
private void readObject(java.io.ObjectInputStream s) |
844 |
tim |
1.1 |
throws java.io.IOException, ClassNotFoundException { |
845 |
|
|
|
846 |
|
|
s.defaultReadObject(); |
847 |
|
|
|
848 |
dl |
1.42 |
// bind to new lock |
849 |
|
|
resetLock(); |
850 |
|
|
|
851 |
tim |
1.1 |
// Read in array length and allocate array |
852 |
dl |
1.40 |
int len = s.readInt(); |
853 |
dl |
1.41 |
Object[] elements = new Object[len]; |
854 |
tim |
1.1 |
|
855 |
|
|
// Read in all elements in the proper order. |
856 |
dl |
1.40 |
for (int i = 0; i < len; i++) |
857 |
dl |
1.41 |
elements[i] = s.readObject(); |
858 |
dl |
1.40 |
setArray(elements); |
859 |
tim |
1.1 |
} |
860 |
|
|
|
861 |
|
|
/** |
862 |
jsr166 |
1.55 |
* Returns a string representation of this list. The string |
863 |
|
|
* representation consists of the string representations of the list's |
864 |
|
|
* elements in the order they are returned by its iterator, enclosed in |
865 |
|
|
* square brackets (<tt>"[]"</tt>). Adjacent elements are separated by |
866 |
|
|
* the characters <tt>", "</tt> (comma and space). Elements are |
867 |
|
|
* converted to strings as by {@link String#valueOf(Object)}. |
868 |
|
|
* |
869 |
|
|
* @return a string representation of this list |
870 |
tim |
1.1 |
*/ |
871 |
|
|
public String toString() { |
872 |
jsr166 |
1.67 |
return Arrays.toString(getArray()); |
873 |
tim |
1.1 |
} |
874 |
|
|
|
875 |
|
|
/** |
876 |
jsr166 |
1.35 |
* Compares the specified object with this list for equality. |
877 |
jsr166 |
1.60 |
* Returns {@code true} if the specified object is the same object |
878 |
|
|
* as this object, or if it is also a {@link List} and the sequence |
879 |
|
|
* of elements returned by an {@linkplain List#iterator() iterator} |
880 |
|
|
* over the specified list is the same as the sequence returned by |
881 |
|
|
* an iterator over this list. The two sequences are considered to |
882 |
|
|
* be the same if they have the same length and corresponding |
883 |
|
|
* elements at the same position in the sequence are <em>equal</em>. |
884 |
|
|
* Two elements {@code e1} and {@code e2} are considered |
885 |
|
|
* <em>equal</em> if {@code (e1==null ? e2==null : e1.equals(e2))}. |
886 |
tim |
1.1 |
* |
887 |
jsr166 |
1.35 |
* @param o the object to be compared for equality with this list |
888 |
jsr166 |
1.60 |
* @return {@code true} if the specified object is equal to this list |
889 |
tim |
1.1 |
*/ |
890 |
|
|
public boolean equals(Object o) { |
891 |
|
|
if (o == this) |
892 |
|
|
return true; |
893 |
|
|
if (!(o instanceof List)) |
894 |
|
|
return false; |
895 |
|
|
|
896 |
dl |
1.57 |
List<?> list = (List<?>)(o); |
897 |
jsr166 |
1.67 |
Iterator<?> it = list.iterator(); |
898 |
|
|
Object[] elements = getArray(); |
899 |
|
|
int len = elements.length; |
900 |
jsr166 |
1.60 |
for (int i = 0; i < len; ++i) |
901 |
|
|
if (!it.hasNext() || !eq(elements[i], it.next())) |
902 |
dl |
1.57 |
return false; |
903 |
jsr166 |
1.60 |
if (it.hasNext()) |
904 |
tim |
1.1 |
return false; |
905 |
|
|
return true; |
906 |
|
|
} |
907 |
|
|
|
908 |
|
|
/** |
909 |
jsr166 |
1.35 |
* Returns the hash code value for this list. |
910 |
dl |
1.26 |
* |
911 |
jsr166 |
1.47 |
* <p>This implementation uses the definition in {@link List#hashCode}. |
912 |
|
|
* |
913 |
|
|
* @return the hash code value for this list |
914 |
tim |
1.1 |
*/ |
915 |
|
|
public int hashCode() { |
916 |
|
|
int hashCode = 1; |
917 |
jsr166 |
1.67 |
Object[] elements = getArray(); |
918 |
|
|
int len = elements.length; |
919 |
|
|
for (int i = 0; i < len; ++i) { |
920 |
|
|
Object obj = elements[i]; |
921 |
jsr166 |
1.45 |
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); |
922 |
tim |
1.1 |
} |
923 |
|
|
return hashCode; |
924 |
|
|
} |
925 |
|
|
|
926 |
|
|
/** |
927 |
jsr166 |
1.35 |
* Returns an iterator over the elements in this list in proper sequence. |
928 |
|
|
* |
929 |
|
|
* <p>The returned iterator provides a snapshot of the state of the list |
930 |
|
|
* when the iterator was constructed. No synchronization is needed while |
931 |
|
|
* traversing the iterator. The iterator does <em>NOT</em> support the |
932 |
|
|
* <tt>remove</tt> method. |
933 |
|
|
* |
934 |
|
|
* @return an iterator over the elements in this list in proper sequence |
935 |
tim |
1.1 |
*/ |
936 |
|
|
public Iterator<E> iterator() { |
937 |
dl |
1.40 |
return new COWIterator<E>(getArray(), 0); |
938 |
tim |
1.1 |
} |
939 |
|
|
|
940 |
|
|
/** |
941 |
jsr166 |
1.35 |
* {@inheritDoc} |
942 |
tim |
1.1 |
* |
943 |
jsr166 |
1.35 |
* <p>The returned iterator provides a snapshot of the state of the list |
944 |
|
|
* when the iterator was constructed. No synchronization is needed while |
945 |
|
|
* traversing the iterator. The iterator does <em>NOT</em> support the |
946 |
|
|
* <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods. |
947 |
tim |
1.1 |
*/ |
948 |
|
|
public ListIterator<E> listIterator() { |
949 |
dl |
1.40 |
return new COWIterator<E>(getArray(), 0); |
950 |
tim |
1.1 |
} |
951 |
|
|
|
952 |
|
|
/** |
953 |
jsr166 |
1.35 |
* {@inheritDoc} |
954 |
|
|
* |
955 |
jsr166 |
1.50 |
* <p>The returned iterator provides a snapshot of the state of the list |
956 |
|
|
* when the iterator was constructed. No synchronization is needed while |
957 |
|
|
* traversing the iterator. The iterator does <em>NOT</em> support the |
958 |
|
|
* <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods. |
959 |
jsr166 |
1.35 |
* |
960 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
961 |
tim |
1.1 |
*/ |
962 |
|
|
public ListIterator<E> listIterator(final int index) { |
963 |
dl |
1.41 |
Object[] elements = getArray(); |
964 |
dl |
1.40 |
int len = elements.length; |
965 |
jsr166 |
1.45 |
if (index<0 || index>len) |
966 |
|
|
throw new IndexOutOfBoundsException("Index: "+index); |
967 |
tim |
1.1 |
|
968 |
dl |
1.48 |
return new COWIterator<E>(elements, index); |
969 |
tim |
1.1 |
} |
970 |
|
|
|
971 |
|
|
private static class COWIterator<E> implements ListIterator<E> { |
972 |
jsr166 |
1.68 |
/** Snapshot of the array */ |
973 |
dl |
1.41 |
private final Object[] snapshot; |
974 |
dl |
1.40 |
/** Index of element to be returned by subsequent call to next. */ |
975 |
tim |
1.1 |
private int cursor; |
976 |
|
|
|
977 |
dl |
1.41 |
private COWIterator(Object[] elements, int initialCursor) { |
978 |
tim |
1.1 |
cursor = initialCursor; |
979 |
dl |
1.40 |
snapshot = elements; |
980 |
tim |
1.1 |
} |
981 |
|
|
|
982 |
|
|
public boolean hasNext() { |
983 |
dl |
1.40 |
return cursor < snapshot.length; |
984 |
tim |
1.1 |
} |
985 |
|
|
|
986 |
|
|
public boolean hasPrevious() { |
987 |
|
|
return cursor > 0; |
988 |
|
|
} |
989 |
|
|
|
990 |
jsr166 |
1.67 |
@SuppressWarnings("unchecked") |
991 |
tim |
1.1 |
public E next() { |
992 |
jsr166 |
1.67 |
if (! hasNext()) |
993 |
tim |
1.1 |
throw new NoSuchElementException(); |
994 |
jsr166 |
1.67 |
return (E) snapshot[cursor++]; |
995 |
tim |
1.1 |
} |
996 |
|
|
|
997 |
jsr166 |
1.67 |
@SuppressWarnings("unchecked") |
998 |
tim |
1.1 |
public E previous() { |
999 |
jsr166 |
1.67 |
if (! hasPrevious()) |
1000 |
tim |
1.1 |
throw new NoSuchElementException(); |
1001 |
jsr166 |
1.67 |
return (E) snapshot[--cursor]; |
1002 |
tim |
1.1 |
} |
1003 |
|
|
|
1004 |
|
|
public int nextIndex() { |
1005 |
|
|
return cursor; |
1006 |
|
|
} |
1007 |
|
|
|
1008 |
|
|
public int previousIndex() { |
1009 |
jsr166 |
1.45 |
return cursor-1; |
1010 |
tim |
1.1 |
} |
1011 |
|
|
|
1012 |
|
|
/** |
1013 |
|
|
* Not supported. Always throws UnsupportedOperationException. |
1014 |
jsr166 |
1.32 |
* @throws UnsupportedOperationException always; <tt>remove</tt> |
1015 |
|
|
* is not supported by this iterator. |
1016 |
tim |
1.1 |
*/ |
1017 |
|
|
public void remove() { |
1018 |
|
|
throw new UnsupportedOperationException(); |
1019 |
|
|
} |
1020 |
|
|
|
1021 |
|
|
/** |
1022 |
|
|
* Not supported. Always throws UnsupportedOperationException. |
1023 |
jsr166 |
1.32 |
* @throws UnsupportedOperationException always; <tt>set</tt> |
1024 |
|
|
* is not supported by this iterator. |
1025 |
tim |
1.1 |
*/ |
1026 |
jsr166 |
1.33 |
public void set(E e) { |
1027 |
tim |
1.1 |
throw new UnsupportedOperationException(); |
1028 |
|
|
} |
1029 |
|
|
|
1030 |
|
|
/** |
1031 |
|
|
* Not supported. Always throws UnsupportedOperationException. |
1032 |
jsr166 |
1.32 |
* @throws UnsupportedOperationException always; <tt>add</tt> |
1033 |
|
|
* is not supported by this iterator. |
1034 |
tim |
1.1 |
*/ |
1035 |
jsr166 |
1.33 |
public void add(E e) { |
1036 |
tim |
1.1 |
throw new UnsupportedOperationException(); |
1037 |
|
|
} |
1038 |
|
|
} |
1039 |
|
|
|
1040 |
|
|
/** |
1041 |
dl |
1.40 |
* Returns a view of the portion of this list between |
1042 |
|
|
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. |
1043 |
|
|
* The returned list is backed by this list, so changes in the |
1044 |
jsr166 |
1.66 |
* returned list are reflected in this list. |
1045 |
dl |
1.40 |
* |
1046 |
|
|
* <p>The semantics of the list returned by this method become |
1047 |
jsr166 |
1.66 |
* undefined if the backing list (i.e., this list) is modified in |
1048 |
|
|
* any way other than via the returned list. |
1049 |
tim |
1.1 |
* |
1050 |
jsr166 |
1.35 |
* @param fromIndex low endpoint (inclusive) of the subList |
1051 |
|
|
* @param toIndex high endpoint (exclusive) of the subList |
1052 |
|
|
* @return a view of the specified range within this list |
1053 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1054 |
tim |
1.1 |
*/ |
1055 |
dl |
1.42 |
public List<E> subList(int fromIndex, int toIndex) { |
1056 |
jsr166 |
1.67 |
final ReentrantLock lock = this.lock; |
1057 |
|
|
lock.lock(); |
1058 |
|
|
try { |
1059 |
|
|
Object[] elements = getArray(); |
1060 |
|
|
int len = elements.length; |
1061 |
|
|
if (fromIndex < 0 || toIndex > len || fromIndex > toIndex) |
1062 |
|
|
throw new IndexOutOfBoundsException(); |
1063 |
|
|
return new COWSubList<E>(this, fromIndex, toIndex); |
1064 |
|
|
} finally { |
1065 |
|
|
lock.unlock(); |
1066 |
|
|
} |
1067 |
tim |
1.1 |
} |
1068 |
|
|
|
1069 |
dl |
1.42 |
/** |
1070 |
|
|
* Sublist for CopyOnWriteArrayList. |
1071 |
|
|
* This class extends AbstractList merely for convenience, to |
1072 |
|
|
* avoid having to define addAll, etc. This doesn't hurt, but |
1073 |
|
|
* is wasteful. This class does not need or use modCount |
1074 |
|
|
* mechanics in AbstractList, but does need to check for |
1075 |
|
|
* concurrent modification using similar mechanics. On each |
1076 |
|
|
* operation, the array that we expect the backing list to use |
1077 |
|
|
* is checked and updated. Since we do this for all of the |
1078 |
|
|
* base operations invoked by those defined in AbstractList, |
1079 |
|
|
* all is well. While inefficient, this is not worth |
1080 |
|
|
* improving. The kinds of list operations inherited from |
1081 |
|
|
* AbstractList are already so slow on COW sublists that |
1082 |
|
|
* adding a bit more space/time doesn't seem even noticeable. |
1083 |
|
|
*/ |
1084 |
jsr166 |
1.66 |
private static class COWSubList<E> |
1085 |
jsr166 |
1.67 |
extends AbstractList<E> |
1086 |
|
|
implements RandomAccess |
1087 |
jsr166 |
1.66 |
{ |
1088 |
tim |
1.1 |
private final CopyOnWriteArrayList<E> l; |
1089 |
|
|
private final int offset; |
1090 |
|
|
private int size; |
1091 |
dl |
1.41 |
private Object[] expectedArray; |
1092 |
tim |
1.1 |
|
1093 |
jsr166 |
1.45 |
// only call this holding l's lock |
1094 |
jsr166 |
1.66 |
COWSubList(CopyOnWriteArrayList<E> list, |
1095 |
jsr166 |
1.67 |
int fromIndex, int toIndex) { |
1096 |
tim |
1.1 |
l = list; |
1097 |
dl |
1.40 |
expectedArray = l.getArray(); |
1098 |
tim |
1.1 |
offset = fromIndex; |
1099 |
|
|
size = toIndex - fromIndex; |
1100 |
|
|
} |
1101 |
|
|
|
1102 |
|
|
// only call this holding l's lock |
1103 |
|
|
private void checkForComodification() { |
1104 |
dl |
1.40 |
if (l.getArray() != expectedArray) |
1105 |
tim |
1.1 |
throw new ConcurrentModificationException(); |
1106 |
|
|
} |
1107 |
|
|
|
1108 |
|
|
// only call this holding l's lock |
1109 |
|
|
private void rangeCheck(int index) { |
1110 |
jsr166 |
1.45 |
if (index<0 || index>=size) |
1111 |
|
|
throw new IndexOutOfBoundsException("Index: "+index+ |
1112 |
jsr166 |
1.67 |
",Size: "+size); |
1113 |
tim |
1.1 |
} |
1114 |
|
|
|
1115 |
|
|
public E set(int index, E element) { |
1116 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1117 |
|
|
lock.lock(); |
1118 |
|
|
try { |
1119 |
tim |
1.1 |
rangeCheck(index); |
1120 |
|
|
checkForComodification(); |
1121 |
jsr166 |
1.45 |
E x = l.set(index+offset, element); |
1122 |
dl |
1.40 |
expectedArray = l.getArray(); |
1123 |
tim |
1.1 |
return x; |
1124 |
jsr166 |
1.67 |
} finally { |
1125 |
|
|
lock.unlock(); |
1126 |
|
|
} |
1127 |
tim |
1.1 |
} |
1128 |
|
|
|
1129 |
|
|
public E get(int index) { |
1130 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1131 |
|
|
lock.lock(); |
1132 |
|
|
try { |
1133 |
tim |
1.1 |
rangeCheck(index); |
1134 |
|
|
checkForComodification(); |
1135 |
jsr166 |
1.45 |
return l.get(index+offset); |
1136 |
jsr166 |
1.67 |
} finally { |
1137 |
|
|
lock.unlock(); |
1138 |
|
|
} |
1139 |
tim |
1.1 |
} |
1140 |
|
|
|
1141 |
|
|
public int size() { |
1142 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1143 |
|
|
lock.lock(); |
1144 |
|
|
try { |
1145 |
tim |
1.1 |
checkForComodification(); |
1146 |
|
|
return size; |
1147 |
jsr166 |
1.67 |
} finally { |
1148 |
|
|
lock.unlock(); |
1149 |
|
|
} |
1150 |
tim |
1.1 |
} |
1151 |
|
|
|
1152 |
|
|
public void add(int index, E element) { |
1153 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1154 |
|
|
lock.lock(); |
1155 |
|
|
try { |
1156 |
tim |
1.1 |
checkForComodification(); |
1157 |
|
|
if (index<0 || index>size) |
1158 |
|
|
throw new IndexOutOfBoundsException(); |
1159 |
jsr166 |
1.45 |
l.add(index+offset, element); |
1160 |
dl |
1.40 |
expectedArray = l.getArray(); |
1161 |
tim |
1.1 |
size++; |
1162 |
jsr166 |
1.67 |
} finally { |
1163 |
|
|
lock.unlock(); |
1164 |
|
|
} |
1165 |
tim |
1.1 |
} |
1166 |
|
|
|
1167 |
dl |
1.13 |
public void clear() { |
1168 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1169 |
|
|
lock.lock(); |
1170 |
|
|
try { |
1171 |
dl |
1.13 |
checkForComodification(); |
1172 |
|
|
l.removeRange(offset, offset+size); |
1173 |
dl |
1.40 |
expectedArray = l.getArray(); |
1174 |
dl |
1.13 |
size = 0; |
1175 |
jsr166 |
1.67 |
} finally { |
1176 |
|
|
lock.unlock(); |
1177 |
|
|
} |
1178 |
dl |
1.13 |
} |
1179 |
|
|
|
1180 |
tim |
1.1 |
public E remove(int index) { |
1181 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1182 |
|
|
lock.lock(); |
1183 |
|
|
try { |
1184 |
tim |
1.1 |
rangeCheck(index); |
1185 |
|
|
checkForComodification(); |
1186 |
jsr166 |
1.45 |
E result = l.remove(index+offset); |
1187 |
dl |
1.40 |
expectedArray = l.getArray(); |
1188 |
tim |
1.1 |
size--; |
1189 |
|
|
return result; |
1190 |
jsr166 |
1.67 |
} finally { |
1191 |
|
|
lock.unlock(); |
1192 |
|
|
} |
1193 |
tim |
1.1 |
} |
1194 |
|
|
|
1195 |
jsr166 |
1.66 |
public boolean remove(Object o) { |
1196 |
jsr166 |
1.67 |
int index = indexOf(o); |
1197 |
|
|
if (index == -1) |
1198 |
|
|
return false; |
1199 |
|
|
remove(index); |
1200 |
|
|
return true; |
1201 |
jsr166 |
1.66 |
} |
1202 |
|
|
|
1203 |
tim |
1.1 |
public Iterator<E> iterator() { |
1204 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1205 |
|
|
lock.lock(); |
1206 |
|
|
try { |
1207 |
tim |
1.1 |
checkForComodification(); |
1208 |
tim |
1.8 |
return new COWSubListIterator<E>(l, 0, offset, size); |
1209 |
jsr166 |
1.67 |
} finally { |
1210 |
|
|
lock.unlock(); |
1211 |
|
|
} |
1212 |
tim |
1.1 |
} |
1213 |
|
|
|
1214 |
|
|
public ListIterator<E> listIterator(final int index) { |
1215 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1216 |
|
|
lock.lock(); |
1217 |
|
|
try { |
1218 |
tim |
1.1 |
checkForComodification(); |
1219 |
|
|
if (index<0 || index>size) |
1220 |
dl |
1.40 |
throw new IndexOutOfBoundsException("Index: "+index+ |
1221 |
jsr166 |
1.67 |
", Size: "+size); |
1222 |
tim |
1.8 |
return new COWSubListIterator<E>(l, index, offset, size); |
1223 |
jsr166 |
1.67 |
} finally { |
1224 |
|
|
lock.unlock(); |
1225 |
|
|
} |
1226 |
tim |
1.1 |
} |
1227 |
|
|
|
1228 |
tim |
1.7 |
public List<E> subList(int fromIndex, int toIndex) { |
1229 |
jsr166 |
1.67 |
final ReentrantLock lock = l.lock; |
1230 |
|
|
lock.lock(); |
1231 |
|
|
try { |
1232 |
tim |
1.7 |
checkForComodification(); |
1233 |
|
|
if (fromIndex<0 || toIndex>size) |
1234 |
|
|
throw new IndexOutOfBoundsException(); |
1235 |
dl |
1.41 |
return new COWSubList<E>(l, fromIndex + offset, |
1236 |
jsr166 |
1.67 |
toIndex + offset); |
1237 |
|
|
} finally { |
1238 |
|
|
lock.unlock(); |
1239 |
|
|
} |
1240 |
tim |
1.7 |
} |
1241 |
tim |
1.1 |
|
1242 |
tim |
1.7 |
} |
1243 |
tim |
1.1 |
|
1244 |
|
|
|
1245 |
tim |
1.7 |
private static class COWSubListIterator<E> implements ListIterator<E> { |
1246 |
|
|
private final ListIterator<E> i; |
1247 |
|
|
private final int index; |
1248 |
|
|
private final int offset; |
1249 |
|
|
private final int size; |
1250 |
jsr166 |
1.66 |
|
1251 |
jsr166 |
1.67 |
COWSubListIterator(List<E> l, int index, int offset, |
1252 |
|
|
int size) { |
1253 |
tim |
1.7 |
this.index = index; |
1254 |
|
|
this.offset = offset; |
1255 |
|
|
this.size = size; |
1256 |
jsr166 |
1.45 |
i = l.listIterator(index+offset); |
1257 |
tim |
1.7 |
} |
1258 |
tim |
1.1 |
|
1259 |
tim |
1.7 |
public boolean hasNext() { |
1260 |
|
|
return nextIndex() < size; |
1261 |
|
|
} |
1262 |
tim |
1.1 |
|
1263 |
tim |
1.7 |
public E next() { |
1264 |
|
|
if (hasNext()) |
1265 |
|
|
return i.next(); |
1266 |
|
|
else |
1267 |
|
|
throw new NoSuchElementException(); |
1268 |
|
|
} |
1269 |
tim |
1.1 |
|
1270 |
tim |
1.7 |
public boolean hasPrevious() { |
1271 |
|
|
return previousIndex() >= 0; |
1272 |
|
|
} |
1273 |
tim |
1.1 |
|
1274 |
tim |
1.7 |
public E previous() { |
1275 |
|
|
if (hasPrevious()) |
1276 |
|
|
return i.previous(); |
1277 |
|
|
else |
1278 |
|
|
throw new NoSuchElementException(); |
1279 |
|
|
} |
1280 |
tim |
1.1 |
|
1281 |
tim |
1.7 |
public int nextIndex() { |
1282 |
|
|
return i.nextIndex() - offset; |
1283 |
|
|
} |
1284 |
tim |
1.1 |
|
1285 |
tim |
1.7 |
public int previousIndex() { |
1286 |
|
|
return i.previousIndex() - offset; |
1287 |
tim |
1.1 |
} |
1288 |
|
|
|
1289 |
tim |
1.7 |
public void remove() { |
1290 |
|
|
throw new UnsupportedOperationException(); |
1291 |
|
|
} |
1292 |
tim |
1.1 |
|
1293 |
jsr166 |
1.33 |
public void set(E e) { |
1294 |
tim |
1.7 |
throw new UnsupportedOperationException(); |
1295 |
tim |
1.1 |
} |
1296 |
|
|
|
1297 |
jsr166 |
1.33 |
public void add(E e) { |
1298 |
tim |
1.7 |
throw new UnsupportedOperationException(); |
1299 |
|
|
} |
1300 |
tim |
1.1 |
} |
1301 |
|
|
|
1302 |
dl |
1.42 |
// Support for resetting lock while deserializing |
1303 |
jsr166 |
1.56 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
1304 |
dl |
1.42 |
private static final long lockOffset; |
1305 |
|
|
static { |
1306 |
|
|
try { |
1307 |
|
|
lockOffset = unsafe.objectFieldOffset |
1308 |
|
|
(CopyOnWriteArrayList.class.getDeclaredField("lock")); |
1309 |
|
|
} catch (Exception ex) { throw new Error(ex); } |
1310 |
|
|
} |
1311 |
|
|
private void resetLock() { |
1312 |
|
|
unsafe.putObjectVolatile(this, lockOffset, new ReentrantLock()); |
1313 |
|
|
} |
1314 |
|
|
|
1315 |
tim |
1.1 |
} |