1 |
|
/* |
2 |
< |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
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 |
7 |
> |
* published by the Free Software Foundation. Oracle designates this |
8 |
|
* particular file as subject to the "Classpath" exception as provided |
9 |
< |
* by Sun in the LICENSE file that accompanied this code. |
9 |
> |
* by Oracle 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 |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.util.function.Consumer; |
29 |
+ |
import java.util.function.Predicate; |
30 |
+ |
import java.util.function.UnaryOperator; |
31 |
+ |
|
32 |
|
/** |
33 |
< |
* Resizable-array implementation of the <tt>List</tt> interface. Implements |
33 |
> |
* Resizable-array implementation of the {@code List} interface. Implements |
34 |
|
* all optional list operations, and permits all elements, including |
35 |
< |
* <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, |
35 |
> |
* {@code null}. In addition to implementing the {@code List} interface, |
36 |
|
* this class provides methods to manipulate the size of the array that is |
37 |
|
* used internally to store the list. (This class is roughly equivalent to |
38 |
< |
* <tt>Vector</tt>, except that it is unsynchronized.) |
38 |
> |
* {@code Vector}, except that it is unsynchronized.) |
39 |
|
* |
40 |
< |
* <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>, |
41 |
< |
* <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant |
42 |
< |
* time. The <tt>add</tt> operation runs in <i>amortized constant time</i>, |
40 |
> |
* <p>The {@code size}, {@code isEmpty}, {@code get}, {@code set}, |
41 |
> |
* {@code iterator}, and {@code listIterator} operations run in constant |
42 |
> |
* time. The {@code add} operation runs in <i>amortized constant time</i>, |
43 |
|
* that is, adding n elements requires O(n) time. All of the other operations |
44 |
|
* run in linear time (roughly speaking). The constant factor is low compared |
45 |
< |
* to that for the <tt>LinkedList</tt> implementation. |
45 |
> |
* to that for the {@code LinkedList} implementation. |
46 |
|
* |
47 |
< |
* <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is |
47 |
> |
* <p>Each {@code ArrayList} instance has a <i>capacity</i>. The capacity is |
48 |
|
* the size of the array used to store the elements in the list. It is always |
49 |
|
* at least as large as the list size. As elements are added to an ArrayList, |
50 |
|
* its capacity grows automatically. The details of the growth policy are not |
51 |
|
* specified beyond the fact that adding an element has constant amortized |
52 |
|
* time cost. |
53 |
|
* |
54 |
< |
* <p>An application can increase the capacity of an <tt>ArrayList</tt> instance |
55 |
< |
* before adding a large number of elements using the <tt>ensureCapacity</tt> |
54 |
> |
* <p>An application can increase the capacity of an {@code ArrayList} instance |
55 |
> |
* before adding a large number of elements using the {@code ensureCapacity} |
56 |
|
* operation. This may reduce the amount of incremental reallocation. |
57 |
|
* |
58 |
|
* <p><strong>Note that this implementation is not synchronized.</strong> |
59 |
< |
* If multiple threads access an <tt>ArrayList</tt> instance concurrently, |
59 |
> |
* If multiple threads access an {@code ArrayList} instance concurrently, |
60 |
|
* and at least one of the threads modifies the list structurally, it |
61 |
|
* <i>must</i> be synchronized externally. (A structural modification is |
62 |
|
* any operation that adds or deletes one or more elements, or explicitly |
70 |
|
* unsynchronized access to the list:<pre> |
71 |
|
* List list = Collections.synchronizedList(new ArrayList(...));</pre> |
72 |
|
* |
73 |
< |
* <p><a name="fail-fast"/> |
73 |
> |
* <p id="fail-fast"> |
74 |
|
* The iterators returned by this class's {@link #iterator() iterator} and |
75 |
|
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
76 |
|
* if the list is structurally modified at any time after the iterator is |
94 |
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
95 |
|
* Java Collections Framework</a>. |
96 |
|
* |
97 |
+ |
* @param <E> the type of elements in this list |
98 |
+ |
* |
99 |
|
* @author Josh Bloch |
100 |
|
* @author Neal Gafter |
101 |
|
* @see Collection |
104 |
|
* @see Vector |
105 |
|
* @since 1.2 |
106 |
|
*/ |
101 |
– |
|
107 |
|
public class ArrayList<E> extends AbstractList<E> |
108 |
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
109 |
|
{ |
110 |
|
private static final long serialVersionUID = 8683452581122892189L; |
111 |
|
|
112 |
|
/** |
113 |
+ |
* Default initial capacity. |
114 |
+ |
*/ |
115 |
+ |
private static final int DEFAULT_CAPACITY = 10; |
116 |
+ |
|
117 |
+ |
/** |
118 |
+ |
* Shared empty array instance used for empty instances. |
119 |
+ |
*/ |
120 |
+ |
private static final Object[] EMPTY_ELEMENTDATA = {}; |
121 |
+ |
|
122 |
+ |
/** |
123 |
+ |
* Shared empty array instance used for default sized empty instances. We |
124 |
+ |
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when |
125 |
+ |
* first element is added. |
126 |
+ |
*/ |
127 |
+ |
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; |
128 |
+ |
|
129 |
+ |
/** |
130 |
|
* The array buffer into which the elements of the ArrayList are stored. |
131 |
< |
* The capacity of the ArrayList is the length of this array buffer. |
131 |
> |
* The capacity of the ArrayList is the length of this array buffer. Any |
132 |
> |
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA |
133 |
> |
* will be expanded to DEFAULT_CAPACITY when the first element is added. |
134 |
|
*/ |
135 |
< |
private transient Object[] elementData; |
135 |
> |
transient Object[] elementData; // non-private to simplify nested class access |
136 |
|
|
137 |
|
/** |
138 |
|
* The size of the ArrayList (the number of elements it contains). |
144 |
|
/** |
145 |
|
* Constructs an empty list with the specified initial capacity. |
146 |
|
* |
147 |
< |
* @param initialCapacity the initial capacity of the list |
148 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
149 |
< |
* is negative |
147 |
> |
* @param initialCapacity the initial capacity of the list |
148 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
149 |
> |
* is negative |
150 |
|
*/ |
151 |
|
public ArrayList(int initialCapacity) { |
152 |
< |
super(); |
153 |
< |
if (initialCapacity < 0) |
152 |
> |
if (initialCapacity > 0) { |
153 |
> |
this.elementData = new Object[initialCapacity]; |
154 |
> |
} else if (initialCapacity == 0) { |
155 |
> |
this.elementData = EMPTY_ELEMENTDATA; |
156 |
> |
} else { |
157 |
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
158 |
|
initialCapacity); |
159 |
< |
this.elementData = new Object[initialCapacity]; |
159 |
> |
} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* Constructs an empty list with an initial capacity of ten. |
164 |
|
*/ |
165 |
|
public ArrayList() { |
166 |
< |
this(10); |
166 |
> |
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; |
167 |
|
} |
168 |
|
|
169 |
|
/** |
176 |
|
*/ |
177 |
|
public ArrayList(Collection<? extends E> c) { |
178 |
|
elementData = c.toArray(); |
179 |
< |
size = elementData.length; |
180 |
< |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
181 |
< |
if (elementData.getClass() != Object[].class) |
182 |
< |
elementData = Arrays.copyOf(elementData, size, Object[].class); |
179 |
> |
if ((size = elementData.length) != 0) { |
180 |
> |
// defend against c.toArray (incorrectly) not returning Object[] |
181 |
> |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
182 |
> |
if (elementData.getClass() != Object[].class) |
183 |
> |
elementData = Arrays.copyOf(elementData, size, Object[].class); |
184 |
> |
} else { |
185 |
> |
// replace with empty array. |
186 |
> |
this.elementData = EMPTY_ELEMENTDATA; |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
191 |
> |
* Trims the capacity of this {@code ArrayList} instance to be the |
192 |
|
* list's current size. An application can use this operation to minimize |
193 |
< |
* the storage of an <tt>ArrayList</tt> instance. |
193 |
> |
* the storage of an {@code ArrayList} instance. |
194 |
|
*/ |
195 |
|
public void trimToSize() { |
196 |
|
modCount++; |
197 |
< |
int oldCapacity = elementData.length; |
198 |
< |
if (size < oldCapacity) { |
199 |
< |
elementData = Arrays.copyOf(elementData, size); |
197 |
> |
if (size < elementData.length) { |
198 |
> |
elementData = (size == 0) |
199 |
> |
? EMPTY_ELEMENTDATA |
200 |
> |
: Arrays.copyOf(elementData, size); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
205 |
> |
* Increases the capacity of this {@code ArrayList} instance, if |
206 |
|
* necessary, to ensure that it can hold at least the number of elements |
207 |
|
* specified by the minimum capacity argument. |
208 |
|
* |
209 |
< |
* @param minCapacity the desired minimum capacity |
209 |
> |
* @param minCapacity the desired minimum capacity |
210 |
|
*/ |
211 |
|
public void ensureCapacity(int minCapacity) { |
212 |
< |
modCount++; |
213 |
< |
int oldCapacity = elementData.length; |
214 |
< |
if (minCapacity > oldCapacity) { |
215 |
< |
int newCapacity = (oldCapacity * 3)/2 + 1; |
216 |
< |
if (newCapacity < minCapacity) |
184 |
< |
newCapacity = minCapacity; |
185 |
< |
// minCapacity is usually close to size, so this is a win: |
186 |
< |
elementData = Arrays.copyOf(elementData, newCapacity); |
212 |
> |
if (minCapacity > elementData.length |
213 |
> |
&& !(elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA |
214 |
> |
&& minCapacity <= DEFAULT_CAPACITY)) { |
215 |
> |
modCount++; |
216 |
> |
grow(minCapacity); |
217 |
|
} |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
+ |
* The maximum size of array to allocate (unless necessary). |
222 |
+ |
* Some VMs reserve some header words in an array. |
223 |
+ |
* Attempts to allocate larger arrays may result in |
224 |
+ |
* OutOfMemoryError: Requested array size exceeds VM limit |
225 |
+ |
*/ |
226 |
+ |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
227 |
+ |
|
228 |
+ |
/** |
229 |
+ |
* Increases the capacity to ensure that it can hold at least the |
230 |
+ |
* number of elements specified by the minimum capacity argument. |
231 |
+ |
* |
232 |
+ |
* @param minCapacity the desired minimum capacity |
233 |
+ |
* @throws OutOfMemoryError if minCapacity is less than zero |
234 |
+ |
*/ |
235 |
+ |
private Object[] grow(int minCapacity) { |
236 |
+ |
return elementData = Arrays.copyOf(elementData, |
237 |
+ |
newCapacity(minCapacity)); |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
private Object[] grow() { |
241 |
+ |
return grow(size + 1); |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
/** |
245 |
+ |
* Returns a capacity at least as large as the given minimum capacity. |
246 |
+ |
* Returns the current capacity increased by 50% if that suffices. |
247 |
+ |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
248 |
+ |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
249 |
+ |
* |
250 |
+ |
* @param minCapacity the desired minimum capacity |
251 |
+ |
* @throws OutOfMemoryError if minCapacity is less than zero |
252 |
+ |
*/ |
253 |
+ |
private int newCapacity(int minCapacity) { |
254 |
+ |
// overflow-conscious code |
255 |
+ |
int oldCapacity = elementData.length; |
256 |
+ |
int newCapacity = oldCapacity + (oldCapacity >> 1); |
257 |
+ |
if (newCapacity - minCapacity <= 0) { |
258 |
+ |
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) |
259 |
+ |
return Math.max(DEFAULT_CAPACITY, minCapacity); |
260 |
+ |
if (minCapacity < 0) // overflow |
261 |
+ |
throw new OutOfMemoryError(); |
262 |
+ |
return minCapacity; |
263 |
+ |
} |
264 |
+ |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
265 |
+ |
? newCapacity |
266 |
+ |
: hugeCapacity(minCapacity); |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
private static int hugeCapacity(int minCapacity) { |
270 |
+ |
if (minCapacity < 0) // overflow |
271 |
+ |
throw new OutOfMemoryError(); |
272 |
+ |
return (minCapacity > MAX_ARRAY_SIZE) |
273 |
+ |
? Integer.MAX_VALUE |
274 |
+ |
: MAX_ARRAY_SIZE; |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
/** |
278 |
|
* Returns the number of elements in this list. |
279 |
|
* |
280 |
|
* @return the number of elements in this list |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* Returns <tt>true</tt> if this list contains no elements. |
287 |
> |
* Returns {@code true} if this list contains no elements. |
288 |
|
* |
289 |
< |
* @return <tt>true</tt> if this list contains no elements |
289 |
> |
* @return {@code true} if this list contains no elements |
290 |
|
*/ |
291 |
|
public boolean isEmpty() { |
292 |
|
return size == 0; |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* Returns <tt>true</tt> if this list contains the specified element. |
297 |
< |
* More formally, returns <tt>true</tt> if and only if this list contains |
298 |
< |
* at least one element <tt>e</tt> such that |
299 |
< |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
296 |
> |
* Returns {@code true} if this list contains the specified element. |
297 |
> |
* More formally, returns {@code true} if and only if this list contains |
298 |
> |
* at least one element {@code e} such that |
299 |
> |
* {@code Objects.equals(o, e)}. |
300 |
|
* |
301 |
|
* @param o element whose presence in this list is to be tested |
302 |
< |
* @return <tt>true</tt> if this list contains the specified element |
302 |
> |
* @return {@code true} if this list contains the specified element |
303 |
|
*/ |
304 |
|
public boolean contains(Object o) { |
305 |
|
return indexOf(o) >= 0; |
308 |
|
/** |
309 |
|
* Returns the index of the first occurrence of the specified element |
310 |
|
* in this list, or -1 if this list does not contain the element. |
311 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
312 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
311 |
> |
* More formally, returns the lowest index {@code i} such that |
312 |
> |
* {@code Objects.equals(o, get(i))}, |
313 |
|
* or -1 if there is no such index. |
314 |
|
*/ |
315 |
|
public int indexOf(Object o) { |
328 |
|
/** |
329 |
|
* Returns the index of the last occurrence of the specified element |
330 |
|
* in this list, or -1 if this list does not contain the element. |
331 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
332 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
331 |
> |
* More formally, returns the highest index {@code i} such that |
332 |
> |
* {@code Objects.equals(o, get(i))}, |
333 |
|
* or -1 if there is no such index. |
334 |
|
*/ |
335 |
|
public int lastIndexOf(Object o) { |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The |
349 |
> |
* Returns a shallow copy of this {@code ArrayList} instance. (The |
350 |
|
* elements themselves are not copied.) |
351 |
|
* |
352 |
< |
* @return a clone of this <tt>ArrayList</tt> instance |
352 |
> |
* @return a clone of this {@code ArrayList} instance |
353 |
|
*/ |
354 |
|
public Object clone() { |
355 |
|
try { |
356 |
< |
@SuppressWarnings("unchecked") |
270 |
< |
ArrayList<E> v = (ArrayList<E>) super.clone(); |
356 |
> |
ArrayList<?> v = (ArrayList<?>) super.clone(); |
357 |
|
v.elementData = Arrays.copyOf(elementData, size); |
358 |
|
v.modCount = 0; |
359 |
|
return v; |
360 |
|
} catch (CloneNotSupportedException e) { |
361 |
|
// this shouldn't happen, since we are Cloneable |
362 |
< |
throw new InternalError(); |
362 |
> |
throw new InternalError(e); |
363 |
|
} |
364 |
|
} |
365 |
|
|
392 |
|
* <p>If the list fits in the specified array with room to spare |
393 |
|
* (i.e., the array has more elements than the list), the element in |
394 |
|
* the array immediately following the end of the collection is set to |
395 |
< |
* <tt>null</tt>. (This is useful in determining the length of the |
395 |
> |
* {@code null}. (This is useful in determining the length of the |
396 |
|
* list <i>only</i> if the caller knows that the list does not contain |
397 |
|
* any null elements.) |
398 |
|
* |
423 |
|
return (E) elementData[index]; |
424 |
|
} |
425 |
|
|
426 |
+ |
@SuppressWarnings("unchecked") |
427 |
+ |
static <E> E elementAt(Object[] es, int index) { |
428 |
+ |
return (E) es[index]; |
429 |
+ |
} |
430 |
+ |
|
431 |
|
/** |
432 |
|
* Returns the element at the specified position in this list. |
433 |
|
* |
436 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
437 |
|
*/ |
438 |
|
public E get(int index) { |
439 |
< |
rangeCheck(index); |
349 |
< |
|
439 |
> |
Objects.checkIndex(index, size); |
440 |
|
return elementData(index); |
441 |
|
} |
442 |
|
|
450 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
451 |
|
*/ |
452 |
|
public E set(int index, E element) { |
453 |
< |
rangeCheck(index); |
364 |
< |
|
453 |
> |
Objects.checkIndex(index, size); |
454 |
|
E oldValue = elementData(index); |
455 |
|
elementData[index] = element; |
456 |
|
return oldValue; |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
+ |
* This helper method split out from add(E) to keep method |
461 |
+ |
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
462 |
+ |
* which helps when add(E) is called in a C1-compiled loop. |
463 |
+ |
*/ |
464 |
+ |
private void add(E e, Object[] elementData, int s) { |
465 |
+ |
if (s == elementData.length) |
466 |
+ |
elementData = grow(); |
467 |
+ |
elementData[s] = e; |
468 |
+ |
size = s + 1; |
469 |
+ |
} |
470 |
+ |
|
471 |
+ |
/** |
472 |
|
* Appends the specified element to the end of this list. |
473 |
|
* |
474 |
|
* @param e element to be appended to this list |
475 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
475 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
476 |
|
*/ |
477 |
|
public boolean add(E e) { |
478 |
< |
ensureCapacity(size + 1); // Increments modCount!! |
479 |
< |
elementData[size++] = e; |
478 |
> |
modCount++; |
479 |
> |
add(e, elementData, size); |
480 |
|
return true; |
481 |
|
} |
482 |
|
|
491 |
|
*/ |
492 |
|
public void add(int index, E element) { |
493 |
|
rangeCheckForAdd(index); |
494 |
< |
|
495 |
< |
ensureCapacity(size+1); // Increments modCount!! |
496 |
< |
System.arraycopy(elementData, index, elementData, index + 1, |
497 |
< |
size - index); |
494 |
> |
modCount++; |
495 |
> |
final int s; |
496 |
> |
Object[] elementData; |
497 |
> |
if ((s = size) == (elementData = this.elementData).length) |
498 |
> |
elementData = grow(); |
499 |
> |
System.arraycopy(elementData, index, |
500 |
> |
elementData, index + 1, |
501 |
> |
s - index); |
502 |
|
elementData[index] = element; |
503 |
< |
size++; |
503 |
> |
size = s + 1; |
504 |
|
} |
505 |
|
|
506 |
|
/** |
513 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
514 |
|
*/ |
515 |
|
public E remove(int index) { |
516 |
< |
rangeCheck(index); |
516 |
> |
Objects.checkIndex(index, size); |
517 |
|
|
518 |
|
modCount++; |
519 |
|
E oldValue = elementData(index); |
522 |
|
if (numMoved > 0) |
523 |
|
System.arraycopy(elementData, index+1, elementData, index, |
524 |
|
numMoved); |
525 |
< |
elementData[--size] = null; // Let gc do its work |
525 |
> |
elementData[--size] = null; // clear to let GC do its work |
526 |
|
|
527 |
|
return oldValue; |
528 |
|
} |
531 |
|
* Removes the first occurrence of the specified element from this list, |
532 |
|
* if it is present. If the list does not contain the element, it is |
533 |
|
* unchanged. More formally, removes the element with the lowest index |
534 |
< |
* <tt>i</tt> such that |
535 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
536 |
< |
* (if such an element exists). Returns <tt>true</tt> if this list |
534 |
> |
* {@code i} such that |
535 |
> |
* {@code Objects.equals(o, get(i))} |
536 |
> |
* (if such an element exists). Returns {@code true} if this list |
537 |
|
* contained the specified element (or equivalently, if this list |
538 |
|
* changed as a result of the call). |
539 |
|
* |
540 |
|
* @param o element to be removed from this list, if present |
541 |
< |
* @return <tt>true</tt> if this list contained the specified element |
541 |
> |
* @return {@code true} if this list contained the specified element |
542 |
|
*/ |
543 |
|
public boolean remove(Object o) { |
544 |
|
if (o == null) { |
567 |
|
if (numMoved > 0) |
568 |
|
System.arraycopy(elementData, index+1, elementData, index, |
569 |
|
numMoved); |
570 |
< |
elementData[--size] = null; // Let gc do its work |
570 |
> |
elementData[--size] = null; // clear to let GC do its work |
571 |
|
} |
572 |
|
|
573 |
|
/** |
577 |
|
public void clear() { |
578 |
|
modCount++; |
579 |
|
|
580 |
< |
// Let gc do its work |
580 |
> |
// clear to let GC do its work |
581 |
|
for (int i = 0; i < size; i++) |
582 |
|
elementData[i] = null; |
583 |
|
|
594 |
|
* list is nonempty.) |
595 |
|
* |
596 |
|
* @param c collection containing elements to be added to this list |
597 |
< |
* @return <tt>true</tt> if this list changed as a result of the call |
597 |
> |
* @return {@code true} if this list changed as a result of the call |
598 |
|
* @throws NullPointerException if the specified collection is null |
599 |
|
*/ |
600 |
|
public boolean addAll(Collection<? extends E> c) { |
601 |
|
Object[] a = c.toArray(); |
602 |
+ |
modCount++; |
603 |
|
int numNew = a.length; |
604 |
< |
ensureCapacity(size + numNew); // Increments modCount |
605 |
< |
System.arraycopy(a, 0, elementData, size, numNew); |
606 |
< |
size += numNew; |
607 |
< |
return numNew != 0; |
604 |
> |
if (numNew == 0) |
605 |
> |
return false; |
606 |
> |
Object[] elementData; |
607 |
> |
final int s; |
608 |
> |
if (numNew > (elementData = this.elementData).length - (s = size)) |
609 |
> |
elementData = grow(s + numNew); |
610 |
> |
System.arraycopy(a, 0, elementData, s, numNew); |
611 |
> |
size = s + numNew; |
612 |
> |
return true; |
613 |
|
} |
614 |
|
|
615 |
|
/** |
623 |
|
* @param index index at which to insert the first element from the |
624 |
|
* specified collection |
625 |
|
* @param c collection containing elements to be added to this list |
626 |
< |
* @return <tt>true</tt> if this list changed as a result of the call |
626 |
> |
* @return {@code true} if this list changed as a result of the call |
627 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
628 |
|
* @throws NullPointerException if the specified collection is null |
629 |
|
*/ |
631 |
|
rangeCheckForAdd(index); |
632 |
|
|
633 |
|
Object[] a = c.toArray(); |
634 |
+ |
modCount++; |
635 |
|
int numNew = a.length; |
636 |
< |
ensureCapacity(size + numNew); // Increments modCount |
636 |
> |
if (numNew == 0) |
637 |
> |
return false; |
638 |
> |
Object[] elementData; |
639 |
> |
final int s; |
640 |
> |
if (numNew > (elementData = this.elementData).length - (s = size)) |
641 |
> |
elementData = grow(s + numNew); |
642 |
|
|
643 |
< |
int numMoved = size - index; |
643 |
> |
int numMoved = s - index; |
644 |
|
if (numMoved > 0) |
645 |
< |
System.arraycopy(elementData, index, elementData, index + numNew, |
645 |
> |
System.arraycopy(elementData, index, |
646 |
> |
elementData, index + numNew, |
647 |
|
numMoved); |
530 |
– |
|
648 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
649 |
< |
size += numNew; |
650 |
< |
return numNew != 0; |
649 |
> |
size = s + numNew; |
650 |
> |
return true; |
651 |
|
} |
652 |
|
|
653 |
|
/** |
660 |
|
* @throws IndexOutOfBoundsException if {@code fromIndex} or |
661 |
|
* {@code toIndex} is out of range |
662 |
|
* ({@code fromIndex < 0 || |
546 |
– |
* fromIndex >= size() || |
663 |
|
* toIndex > size() || |
664 |
|
* toIndex < fromIndex}) |
665 |
|
*/ |
666 |
|
protected void removeRange(int fromIndex, int toIndex) { |
667 |
+ |
if (fromIndex > toIndex) { |
668 |
+ |
throw new IndexOutOfBoundsException( |
669 |
+ |
outOfBoundsMsg(fromIndex, toIndex)); |
670 |
+ |
} |
671 |
|
modCount++; |
672 |
|
int numMoved = size - toIndex; |
673 |
|
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
674 |
|
numMoved); |
675 |
|
|
676 |
< |
// Let gc do its work |
676 |
> |
// clear to let GC do its work |
677 |
|
int newSize = size - (toIndex-fromIndex); |
678 |
< |
while (size != newSize) |
679 |
< |
elementData[--size] = null; |
680 |
< |
} |
681 |
< |
|
562 |
< |
/** |
563 |
< |
* Checks if the given index is in range. If not, throws an appropriate |
564 |
< |
* runtime exception. This method does *not* check if the index is |
565 |
< |
* negative: It is always used immediately prior to an array access, |
566 |
< |
* which throws an ArrayIndexOutOfBoundsException if index is negative. |
567 |
< |
*/ |
568 |
< |
private void rangeCheck(int index) { |
569 |
< |
if (index >= size) |
570 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
678 |
> |
for (int i = newSize; i < size; i++) { |
679 |
> |
elementData[i] = null; |
680 |
> |
} |
681 |
> |
size = newSize; |
682 |
|
} |
683 |
|
|
684 |
|
/** |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
+ |
* A version used in checking (fromIndex > toIndex) condition |
703 |
+ |
*/ |
704 |
+ |
private static String outOfBoundsMsg(int fromIndex, int toIndex) { |
705 |
+ |
return "From Index: " + fromIndex + " > To Index: " + toIndex; |
706 |
+ |
} |
707 |
+ |
|
708 |
+ |
/** |
709 |
|
* Removes from this list all of its elements that are contained in the |
710 |
|
* specified collection. |
711 |
|
* |
712 |
|
* @param c collection containing elements to be removed from this list |
713 |
|
* @return {@code true} if this list changed as a result of the call |
714 |
|
* @throws ClassCastException if the class of an element of this list |
715 |
< |
* is incompatible with the specified collection (optional) |
715 |
> |
* is incompatible with the specified collection |
716 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
717 |
|
* @throws NullPointerException if this list contains a null element and the |
718 |
< |
* specified collection does not permit null elements (optional), |
718 |
> |
* specified collection does not permit null elements |
719 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
720 |
|
* or if the specified collection is null |
721 |
|
* @see Collection#contains(Object) |
722 |
|
*/ |
732 |
|
* @param c collection containing elements to be retained in this list |
733 |
|
* @return {@code true} if this list changed as a result of the call |
734 |
|
* @throws ClassCastException if the class of an element of this list |
735 |
< |
* is incompatible with the specified collection (optional) |
735 |
> |
* is incompatible with the specified collection |
736 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
737 |
|
* @throws NullPointerException if this list contains a null element and the |
738 |
< |
* specified collection does not permit null elements (optional), |
738 |
> |
* specified collection does not permit null elements |
739 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
740 |
|
* or if the specified collection is null |
741 |
|
* @see Collection#contains(Object) |
742 |
|
*/ |
745 |
|
} |
746 |
|
|
747 |
|
private boolean batchRemove(Collection<?> c, boolean complement) { |
748 |
< |
final Object[] elementData = this.elementData; |
749 |
< |
int r = 0, w = 0; |
750 |
< |
boolean modified = false; |
751 |
< |
try { |
752 |
< |
for (; r < size; r++) |
753 |
< |
if (c.contains(elementData[r]) == complement) |
754 |
< |
elementData[w++] = elementData[r]; |
755 |
< |
} finally { |
756 |
< |
// Preserve behavioral compatibility with AbstractCollection, |
757 |
< |
// even if c.contains() throws. |
758 |
< |
if (r != size) { |
759 |
< |
System.arraycopy(elementData, r, |
760 |
< |
elementData, w, |
761 |
< |
size - r); |
762 |
< |
w += size - r; |
763 |
< |
} |
764 |
< |
if (w != size) { |
765 |
< |
for (int i = w; i < size; i++) |
766 |
< |
elementData[i] = null; |
767 |
< |
modCount += size - w; |
768 |
< |
size = w; |
769 |
< |
modified = true; |
748 |
> |
Objects.requireNonNull(c); |
749 |
> |
final Object[] es = elementData; |
750 |
> |
final int end = size; |
751 |
> |
final boolean modified; |
752 |
> |
int r; |
753 |
> |
// Optimize for initial run of survivors |
754 |
> |
for (r = 0; r < end && c.contains(es[r]) == complement; r++) |
755 |
> |
; |
756 |
> |
if (modified = (r < end)) { |
757 |
> |
int w = r++; |
758 |
> |
try { |
759 |
> |
for (Object e; r < end; r++) |
760 |
> |
if (c.contains(e = es[r]) == complement) |
761 |
> |
es[w++] = e; |
762 |
> |
} catch (Throwable ex) { |
763 |
> |
// Preserve behavioral compatibility with AbstractCollection, |
764 |
> |
// even if c.contains() throws. |
765 |
> |
System.arraycopy(es, r, es, w, end - r); |
766 |
> |
w += end - r; |
767 |
> |
throw ex; |
768 |
> |
} finally { |
769 |
> |
modCount += end - w; |
770 |
> |
Arrays.fill(es, size = w, end, null); |
771 |
|
} |
772 |
|
} |
773 |
|
return modified; |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
< |
* Save the state of the <tt>ArrayList</tt> instance to a stream (that |
777 |
> |
* Save the state of the {@code ArrayList} instance to a stream (that |
778 |
|
* is, serialize it). |
779 |
|
* |
780 |
< |
* @serialData The length of the array backing the <tt>ArrayList</tt> |
780 |
> |
* @serialData The length of the array backing the {@code ArrayList} |
781 |
|
* instance is emitted (int), followed by all of its elements |
782 |
< |
* (each an <tt>Object</tt>) in the proper order. |
782 |
> |
* (each an {@code Object}) in the proper order. |
783 |
|
*/ |
784 |
|
private void writeObject(java.io.ObjectOutputStream s) |
785 |
|
throws java.io.IOException{ |
787 |
|
int expectedModCount = modCount; |
788 |
|
s.defaultWriteObject(); |
789 |
|
|
790 |
< |
// Write out array length |
791 |
< |
s.writeInt(elementData.length); |
790 |
> |
// Write out size as capacity for behavioural compatibility with clone() |
791 |
> |
s.writeInt(size); |
792 |
|
|
793 |
|
// Write out all elements in the proper order. |
794 |
< |
for (int i=0; i<size; i++) |
794 |
> |
for (int i=0; i<size; i++) { |
795 |
|
s.writeObject(elementData[i]); |
796 |
+ |
} |
797 |
|
|
798 |
|
if (modCount != expectedModCount) { |
799 |
|
throw new ConcurrentModificationException(); |
800 |
|
} |
677 |
– |
|
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
< |
* Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, |
804 |
> |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
805 |
|
* deserialize it). |
806 |
|
*/ |
807 |
|
private void readObject(java.io.ObjectInputStream s) |
808 |
|
throws java.io.IOException, ClassNotFoundException { |
809 |
+ |
|
810 |
|
// Read in size, and any hidden stuff |
811 |
|
s.defaultReadObject(); |
812 |
|
|
813 |
< |
// Read in array length and allocate array |
814 |
< |
int arrayLength = s.readInt(); |
815 |
< |
Object[] a = elementData = new Object[arrayLength]; |
816 |
< |
|
817 |
< |
// Read in all elements in the proper order. |
818 |
< |
for (int i=0; i<size; i++) |
819 |
< |
a[i] = s.readObject(); |
813 |
> |
// Read in capacity |
814 |
> |
s.readInt(); // ignored |
815 |
> |
|
816 |
> |
if (size > 0) { |
817 |
> |
// like clone(), allocate array based upon size not capacity |
818 |
> |
Object[] elements = new Object[size]; |
819 |
> |
|
820 |
> |
// Read in all elements in the proper order. |
821 |
> |
for (int i = 0; i < size; i++) { |
822 |
> |
elements[i] = s.readObject(); |
823 |
> |
} |
824 |
> |
|
825 |
> |
elementData = elements; |
826 |
> |
} else if (size == 0) { |
827 |
> |
elementData = EMPTY_ELEMENTDATA; |
828 |
> |
} else { |
829 |
> |
throw new java.io.InvalidObjectException("Invalid size: " + size); |
830 |
> |
} |
831 |
|
} |
832 |
|
|
833 |
|
/** |
843 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
844 |
|
*/ |
845 |
|
public ListIterator<E> listIterator(int index) { |
846 |
< |
if (index < 0 || index > size) |
712 |
< |
throw new IndexOutOfBoundsException("Index: "+index); |
846 |
> |
rangeCheckForAdd(index); |
847 |
|
return new ListItr(index); |
848 |
|
} |
849 |
|
|
878 |
|
int lastRet = -1; // index of last element returned; -1 if no such |
879 |
|
int expectedModCount = modCount; |
880 |
|
|
881 |
+ |
// prevent creating a synthetic constructor |
882 |
+ |
Itr() {} |
883 |
+ |
|
884 |
|
public boolean hasNext() { |
885 |
|
return cursor != size; |
886 |
|
} |
913 |
|
} |
914 |
|
} |
915 |
|
|
916 |
+ |
@Override |
917 |
+ |
@SuppressWarnings("unchecked") |
918 |
+ |
public void forEachRemaining(Consumer<? super E> consumer) { |
919 |
+ |
Objects.requireNonNull(consumer); |
920 |
+ |
final int size = ArrayList.this.size; |
921 |
+ |
int i = cursor; |
922 |
+ |
if (i >= size) { |
923 |
+ |
return; |
924 |
+ |
} |
925 |
+ |
final Object[] elementData = ArrayList.this.elementData; |
926 |
+ |
if (i >= elementData.length) { |
927 |
+ |
throw new ConcurrentModificationException(); |
928 |
+ |
} |
929 |
+ |
while (i != size && modCount == expectedModCount) { |
930 |
+ |
consumer.accept((E) elementData[i++]); |
931 |
+ |
} |
932 |
+ |
// update once at end of iteration to reduce heap write traffic |
933 |
+ |
cursor = i; |
934 |
+ |
lastRet = i - 1; |
935 |
+ |
checkForComodification(); |
936 |
+ |
} |
937 |
+ |
|
938 |
|
final void checkForComodification() { |
939 |
|
if (modCount != expectedModCount) |
940 |
|
throw new ConcurrentModificationException(); |
1033 |
|
*/ |
1034 |
|
public List<E> subList(int fromIndex, int toIndex) { |
1035 |
|
subListRangeCheck(fromIndex, toIndex, size); |
1036 |
< |
return new SubList(this, 0, fromIndex, toIndex); |
1036 |
> |
return new SubList<>(this, fromIndex, toIndex); |
1037 |
|
} |
1038 |
|
|
1039 |
< |
static void subListRangeCheck(int fromIndex, int toIndex, int size) { |
1040 |
< |
if (fromIndex < 0) |
1041 |
< |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
883 |
< |
if (toIndex > size) |
884 |
< |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
885 |
< |
if (fromIndex > toIndex) |
886 |
< |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
887 |
< |
") > toIndex(" + toIndex + ")"); |
888 |
< |
} |
889 |
< |
|
890 |
< |
private class SubList extends AbstractList<E> implements RandomAccess { |
891 |
< |
private final AbstractList<E> parent; |
892 |
< |
private final int parentOffset; |
1039 |
> |
private static class SubList<E> extends AbstractList<E> implements RandomAccess { |
1040 |
> |
private final ArrayList<E> root; |
1041 |
> |
private final SubList<E> parent; |
1042 |
|
private final int offset; |
1043 |
< |
int size; |
1043 |
> |
private int size; |
1044 |
|
|
1045 |
< |
SubList(AbstractList<E> parent, |
1046 |
< |
int offset, int fromIndex, int toIndex) { |
1045 |
> |
/** |
1046 |
> |
* Constructs a sublist of an arbitrary ArrayList. |
1047 |
> |
*/ |
1048 |
> |
public SubList(ArrayList<E> root, int fromIndex, int toIndex) { |
1049 |
> |
this.root = root; |
1050 |
> |
this.parent = null; |
1051 |
> |
this.offset = fromIndex; |
1052 |
> |
this.size = toIndex - fromIndex; |
1053 |
> |
this.modCount = root.modCount; |
1054 |
> |
} |
1055 |
> |
|
1056 |
> |
/** |
1057 |
> |
* Constructs a sublist of another SubList. |
1058 |
> |
*/ |
1059 |
> |
private SubList(SubList<E> parent, int fromIndex, int toIndex) { |
1060 |
> |
this.root = parent.root; |
1061 |
|
this.parent = parent; |
1062 |
< |
this.parentOffset = fromIndex; |
900 |
< |
this.offset = offset + fromIndex; |
1062 |
> |
this.offset = parent.offset + fromIndex; |
1063 |
|
this.size = toIndex - fromIndex; |
1064 |
< |
this.modCount = ArrayList.this.modCount; |
1064 |
> |
this.modCount = root.modCount; |
1065 |
|
} |
1066 |
|
|
1067 |
< |
public E set(int index, E e) { |
1068 |
< |
rangeCheck(index); |
1067 |
> |
public E set(int index, E element) { |
1068 |
> |
Objects.checkIndex(index, size); |
1069 |
|
checkForComodification(); |
1070 |
< |
E oldValue = ArrayList.this.elementData(offset + index); |
1071 |
< |
ArrayList.this.elementData[offset + index] = e; |
1070 |
> |
E oldValue = root.elementData(offset + index); |
1071 |
> |
root.elementData[offset + index] = element; |
1072 |
|
return oldValue; |
1073 |
|
} |
1074 |
|
|
1075 |
|
public E get(int index) { |
1076 |
< |
rangeCheck(index); |
1076 |
> |
Objects.checkIndex(index, size); |
1077 |
|
checkForComodification(); |
1078 |
< |
return ArrayList.this.elementData(offset + index); |
1078 |
> |
return root.elementData(offset + index); |
1079 |
|
} |
1080 |
|
|
1081 |
|
public int size() { |
1082 |
|
checkForComodification(); |
1083 |
< |
return this.size; |
1083 |
> |
return size; |
1084 |
|
} |
1085 |
|
|
1086 |
< |
public void add(int index, E e) { |
1086 |
> |
public void add(int index, E element) { |
1087 |
|
rangeCheckForAdd(index); |
1088 |
|
checkForComodification(); |
1089 |
< |
parent.add(parentOffset + index, e); |
1090 |
< |
this.modCount = parent.modCount; |
929 |
< |
this.size++; |
1089 |
> |
root.add(offset + index, element); |
1090 |
> |
updateSizeAndModCount(1); |
1091 |
|
} |
1092 |
|
|
1093 |
|
public E remove(int index) { |
1094 |
< |
rangeCheck(index); |
1094 |
> |
Objects.checkIndex(index, size); |
1095 |
|
checkForComodification(); |
1096 |
< |
E result = parent.remove(parentOffset + index); |
1097 |
< |
this.modCount = parent.modCount; |
937 |
< |
this.size--; |
1096 |
> |
E result = root.remove(offset + index); |
1097 |
> |
updateSizeAndModCount(-1); |
1098 |
|
return result; |
1099 |
|
} |
1100 |
|
|
1101 |
|
protected void removeRange(int fromIndex, int toIndex) { |
1102 |
|
checkForComodification(); |
1103 |
< |
parent.removeRange(parentOffset + fromIndex, |
1104 |
< |
parentOffset + toIndex); |
945 |
< |
this.modCount = parent.modCount; |
946 |
< |
this.size -= toIndex - fromIndex; |
1103 |
> |
root.removeRange(offset + fromIndex, offset + toIndex); |
1104 |
> |
updateSizeAndModCount(fromIndex - toIndex); |
1105 |
|
} |
1106 |
|
|
1107 |
|
public boolean addAll(Collection<? extends E> c) { |
1113 |
|
int cSize = c.size(); |
1114 |
|
if (cSize==0) |
1115 |
|
return false; |
958 |
– |
|
1116 |
|
checkForComodification(); |
1117 |
< |
parent.addAll(parentOffset + index, c); |
1118 |
< |
this.modCount = parent.modCount; |
962 |
< |
this.size += cSize; |
1117 |
> |
root.addAll(offset + index, c); |
1118 |
> |
updateSizeAndModCount(cSize); |
1119 |
|
return true; |
1120 |
|
} |
1121 |
|
|
1123 |
|
return listIterator(); |
1124 |
|
} |
1125 |
|
|
1126 |
< |
public ListIterator<E> listIterator(final int index) { |
1126 |
> |
public ListIterator<E> listIterator(int index) { |
1127 |
|
checkForComodification(); |
1128 |
|
rangeCheckForAdd(index); |
973 |
– |
final int offset = this.offset; |
1129 |
|
|
1130 |
|
return new ListIterator<E>() { |
1131 |
|
int cursor = index; |
1132 |
|
int lastRet = -1; |
1133 |
< |
int expectedModCount = ArrayList.this.modCount; |
1133 |
> |
int expectedModCount = root.modCount; |
1134 |
|
|
1135 |
|
public boolean hasNext() { |
1136 |
|
return cursor != SubList.this.size; |
1142 |
|
int i = cursor; |
1143 |
|
if (i >= SubList.this.size) |
1144 |
|
throw new NoSuchElementException(); |
1145 |
< |
Object[] elementData = ArrayList.this.elementData; |
1145 |
> |
Object[] elementData = root.elementData; |
1146 |
|
if (offset + i >= elementData.length) |
1147 |
|
throw new ConcurrentModificationException(); |
1148 |
|
cursor = i + 1; |
1159 |
|
int i = cursor - 1; |
1160 |
|
if (i < 0) |
1161 |
|
throw new NoSuchElementException(); |
1162 |
< |
Object[] elementData = ArrayList.this.elementData; |
1162 |
> |
Object[] elementData = root.elementData; |
1163 |
|
if (offset + i >= elementData.length) |
1164 |
|
throw new ConcurrentModificationException(); |
1165 |
|
cursor = i; |
1166 |
|
return (E) elementData[offset + (lastRet = i)]; |
1167 |
|
} |
1168 |
|
|
1169 |
+ |
@SuppressWarnings("unchecked") |
1170 |
+ |
public void forEachRemaining(Consumer<? super E> consumer) { |
1171 |
+ |
Objects.requireNonNull(consumer); |
1172 |
+ |
final int size = SubList.this.size; |
1173 |
+ |
int i = cursor; |
1174 |
+ |
if (i >= size) { |
1175 |
+ |
return; |
1176 |
+ |
} |
1177 |
+ |
final Object[] elementData = root.elementData; |
1178 |
+ |
if (offset + i >= elementData.length) { |
1179 |
+ |
throw new ConcurrentModificationException(); |
1180 |
+ |
} |
1181 |
+ |
while (i != size && modCount == expectedModCount) { |
1182 |
+ |
consumer.accept((E) elementData[offset + (i++)]); |
1183 |
+ |
} |
1184 |
+ |
// update once at end of iteration to reduce heap write traffic |
1185 |
+ |
lastRet = cursor = i; |
1186 |
+ |
checkForComodification(); |
1187 |
+ |
} |
1188 |
+ |
|
1189 |
|
public int nextIndex() { |
1190 |
|
return cursor; |
1191 |
|
} |
1203 |
|
SubList.this.remove(lastRet); |
1204 |
|
cursor = lastRet; |
1205 |
|
lastRet = -1; |
1206 |
< |
expectedModCount = ArrayList.this.modCount; |
1206 |
> |
expectedModCount = root.modCount; |
1207 |
|
} catch (IndexOutOfBoundsException ex) { |
1208 |
|
throw new ConcurrentModificationException(); |
1209 |
|
} |
1215 |
|
checkForComodification(); |
1216 |
|
|
1217 |
|
try { |
1218 |
< |
ArrayList.this.set(offset + lastRet, e); |
1218 |
> |
root.set(offset + lastRet, e); |
1219 |
|
} catch (IndexOutOfBoundsException ex) { |
1220 |
|
throw new ConcurrentModificationException(); |
1221 |
|
} |
1229 |
|
SubList.this.add(i, e); |
1230 |
|
cursor = i + 1; |
1231 |
|
lastRet = -1; |
1232 |
< |
expectedModCount = ArrayList.this.modCount; |
1232 |
> |
expectedModCount = root.modCount; |
1233 |
|
} catch (IndexOutOfBoundsException ex) { |
1234 |
|
throw new ConcurrentModificationException(); |
1235 |
|
} |
1236 |
|
} |
1237 |
|
|
1238 |
|
final void checkForComodification() { |
1239 |
< |
if (expectedModCount != ArrayList.this.modCount) |
1239 |
> |
if (root.modCount != expectedModCount) |
1240 |
|
throw new ConcurrentModificationException(); |
1241 |
|
} |
1242 |
|
}; |
1244 |
|
|
1245 |
|
public List<E> subList(int fromIndex, int toIndex) { |
1246 |
|
subListRangeCheck(fromIndex, toIndex, size); |
1247 |
< |
return new SubList(this, offset, fromIndex, toIndex); |
1073 |
< |
} |
1074 |
< |
|
1075 |
< |
private void rangeCheck(int index) { |
1076 |
< |
if (index < 0 || index >= this.size) |
1077 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
1247 |
> |
return new SubList<>(this, fromIndex, toIndex); |
1248 |
|
} |
1249 |
|
|
1250 |
|
private void rangeCheckForAdd(int index) { |
1257 |
|
} |
1258 |
|
|
1259 |
|
private void checkForComodification() { |
1260 |
< |
if (ArrayList.this.modCount != this.modCount) |
1260 |
> |
if (root.modCount != modCount) |
1261 |
|
throw new ConcurrentModificationException(); |
1262 |
|
} |
1263 |
+ |
|
1264 |
+ |
private void updateSizeAndModCount(int sizeChange) { |
1265 |
+ |
SubList<E> slist = this; |
1266 |
+ |
do { |
1267 |
+ |
slist.size += sizeChange; |
1268 |
+ |
slist.modCount = root.modCount; |
1269 |
+ |
slist = slist.parent; |
1270 |
+ |
} while (slist != null); |
1271 |
+ |
} |
1272 |
+ |
|
1273 |
+ |
public Spliterator<E> spliterator() { |
1274 |
+ |
checkForComodification(); |
1275 |
+ |
|
1276 |
+ |
// ArrayListSpliterator is not used because late-binding logic |
1277 |
+ |
// is different here |
1278 |
+ |
return new Spliterator<>() { |
1279 |
+ |
private int index = offset; // current index, modified on advance/split |
1280 |
+ |
private int fence = -1; // -1 until used; then one past last index |
1281 |
+ |
private int expectedModCount; // initialized when fence set |
1282 |
+ |
|
1283 |
+ |
private int getFence() { // initialize fence to size on first use |
1284 |
+ |
int hi; // (a specialized variant appears in method forEach) |
1285 |
+ |
if ((hi = fence) < 0) { |
1286 |
+ |
expectedModCount = modCount; |
1287 |
+ |
hi = fence = offset + size; |
1288 |
+ |
} |
1289 |
+ |
return hi; |
1290 |
+ |
} |
1291 |
+ |
|
1292 |
+ |
public ArrayListSpliterator<E> trySplit() { |
1293 |
+ |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1294 |
+ |
// ArrayListSpliterator could be used here as the source is already bound |
1295 |
+ |
return (lo >= mid) ? null : // divide range in half unless too small |
1296 |
+ |
new ArrayListSpliterator<>(root, lo, index = mid, |
1297 |
+ |
expectedModCount); |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
public boolean tryAdvance(Consumer<? super E> action) { |
1301 |
+ |
Objects.requireNonNull(action); |
1302 |
+ |
int hi = getFence(), i = index; |
1303 |
+ |
if (i < hi) { |
1304 |
+ |
index = i + 1; |
1305 |
+ |
@SuppressWarnings("unchecked") E e = (E)root.elementData[i]; |
1306 |
+ |
action.accept(e); |
1307 |
+ |
if (root.modCount != expectedModCount) |
1308 |
+ |
throw new ConcurrentModificationException(); |
1309 |
+ |
return true; |
1310 |
+ |
} |
1311 |
+ |
return false; |
1312 |
+ |
} |
1313 |
+ |
|
1314 |
+ |
public void forEachRemaining(Consumer<? super E> action) { |
1315 |
+ |
Objects.requireNonNull(action); |
1316 |
+ |
int i, hi, mc; // hoist accesses and checks from loop |
1317 |
+ |
ArrayList<E> lst = root; |
1318 |
+ |
Object[] a; |
1319 |
+ |
if ((a = lst.elementData) != null) { |
1320 |
+ |
if ((hi = fence) < 0) { |
1321 |
+ |
mc = modCount; |
1322 |
+ |
hi = offset + size; |
1323 |
+ |
} |
1324 |
+ |
else |
1325 |
+ |
mc = expectedModCount; |
1326 |
+ |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
1327 |
+ |
for (; i < hi; ++i) { |
1328 |
+ |
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1329 |
+ |
action.accept(e); |
1330 |
+ |
} |
1331 |
+ |
if (lst.modCount == mc) |
1332 |
+ |
return; |
1333 |
+ |
} |
1334 |
+ |
} |
1335 |
+ |
throw new ConcurrentModificationException(); |
1336 |
+ |
} |
1337 |
+ |
|
1338 |
+ |
public long estimateSize() { |
1339 |
+ |
return (long) (getFence() - index); |
1340 |
+ |
} |
1341 |
+ |
|
1342 |
+ |
public int characteristics() { |
1343 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1344 |
+ |
} |
1345 |
+ |
}; |
1346 |
+ |
} |
1347 |
+ |
} |
1348 |
+ |
|
1349 |
+ |
@Override |
1350 |
+ |
public void forEach(Consumer<? super E> action) { |
1351 |
+ |
Objects.requireNonNull(action); |
1352 |
+ |
final int expectedModCount = modCount; |
1353 |
+ |
final Object[] es = elementData; |
1354 |
+ |
final int size = this.size; |
1355 |
+ |
for (int i = 0; modCount == expectedModCount && i < size; i++) { |
1356 |
+ |
action.accept(elementAt(es, i)); |
1357 |
+ |
} |
1358 |
+ |
if (modCount != expectedModCount) { |
1359 |
+ |
throw new ConcurrentModificationException(); |
1360 |
+ |
} |
1361 |
+ |
} |
1362 |
+ |
|
1363 |
+ |
/** |
1364 |
+ |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
1365 |
+ |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
1366 |
+ |
* list. |
1367 |
+ |
* |
1368 |
+ |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
1369 |
+ |
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
1370 |
+ |
* Overriding implementations should document the reporting of additional |
1371 |
+ |
* characteristic values. |
1372 |
+ |
* |
1373 |
+ |
* @return a {@code Spliterator} over the elements in this list |
1374 |
+ |
* @since 1.8 |
1375 |
+ |
*/ |
1376 |
+ |
@Override |
1377 |
+ |
public Spliterator<E> spliterator() { |
1378 |
+ |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1379 |
+ |
} |
1380 |
+ |
|
1381 |
+ |
/** Index-based split-by-two, lazily initialized Spliterator */ |
1382 |
+ |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1383 |
+ |
|
1384 |
+ |
/* |
1385 |
+ |
* If ArrayLists were immutable, or structurally immutable (no |
1386 |
+ |
* adds, removes, etc), we could implement their spliterators |
1387 |
+ |
* with Arrays.spliterator. Instead we detect as much |
1388 |
+ |
* interference during traversal as practical without |
1389 |
+ |
* sacrificing much performance. We rely primarily on |
1390 |
+ |
* modCounts. These are not guaranteed to detect concurrency |
1391 |
+ |
* violations, and are sometimes overly conservative about |
1392 |
+ |
* within-thread interference, but detect enough problems to |
1393 |
+ |
* be worthwhile in practice. To carry this out, we (1) lazily |
1394 |
+ |
* initialize fence and expectedModCount until the latest |
1395 |
+ |
* point that we need to commit to the state we are checking |
1396 |
+ |
* against; thus improving precision. (This doesn't apply to |
1397 |
+ |
* SubLists, that create spliterators with current non-lazy |
1398 |
+ |
* values). (2) We perform only a single |
1399 |
+ |
* ConcurrentModificationException check at the end of forEach |
1400 |
+ |
* (the most performance-sensitive method). When using forEach |
1401 |
+ |
* (as opposed to iterators), we can normally only detect |
1402 |
+ |
* interference after actions, not before. Further |
1403 |
+ |
* CME-triggering checks apply to all other possible |
1404 |
+ |
* violations of assumptions for example null or too-small |
1405 |
+ |
* elementData array given its size(), that could only have |
1406 |
+ |
* occurred due to interference. This allows the inner loop |
1407 |
+ |
* of forEach to run without any further checks, and |
1408 |
+ |
* simplifies lambda-resolution. While this does entail a |
1409 |
+ |
* number of checks, note that in the common case of |
1410 |
+ |
* list.stream().forEach(a), no checks or other computation |
1411 |
+ |
* occur anywhere other than inside forEach itself. The other |
1412 |
+ |
* less-often-used methods cannot take advantage of most of |
1413 |
+ |
* these streamlinings. |
1414 |
+ |
*/ |
1415 |
+ |
|
1416 |
+ |
private final ArrayList<E> list; |
1417 |
+ |
private int index; // current index, modified on advance/split |
1418 |
+ |
private int fence; // -1 until used; then one past last index |
1419 |
+ |
private int expectedModCount; // initialized when fence set |
1420 |
+ |
|
1421 |
+ |
/** Create new spliterator covering the given range */ |
1422 |
+ |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1423 |
+ |
int expectedModCount) { |
1424 |
+ |
this.list = list; // OK if null unless traversed |
1425 |
+ |
this.index = origin; |
1426 |
+ |
this.fence = fence; |
1427 |
+ |
this.expectedModCount = expectedModCount; |
1428 |
+ |
} |
1429 |
+ |
|
1430 |
+ |
private int getFence() { // initialize fence to size on first use |
1431 |
+ |
int hi; // (a specialized variant appears in method forEach) |
1432 |
+ |
ArrayList<E> lst; |
1433 |
+ |
if ((hi = fence) < 0) { |
1434 |
+ |
if ((lst = list) == null) |
1435 |
+ |
hi = fence = 0; |
1436 |
+ |
else { |
1437 |
+ |
expectedModCount = lst.modCount; |
1438 |
+ |
hi = fence = lst.size; |
1439 |
+ |
} |
1440 |
+ |
} |
1441 |
+ |
return hi; |
1442 |
+ |
} |
1443 |
+ |
|
1444 |
+ |
public ArrayListSpliterator<E> trySplit() { |
1445 |
+ |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1446 |
+ |
return (lo >= mid) ? null : // divide range in half unless too small |
1447 |
+ |
new ArrayListSpliterator<>(list, lo, index = mid, |
1448 |
+ |
expectedModCount); |
1449 |
+ |
} |
1450 |
+ |
|
1451 |
+ |
public boolean tryAdvance(Consumer<? super E> action) { |
1452 |
+ |
if (action == null) |
1453 |
+ |
throw new NullPointerException(); |
1454 |
+ |
int hi = getFence(), i = index; |
1455 |
+ |
if (i < hi) { |
1456 |
+ |
index = i + 1; |
1457 |
+ |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1458 |
+ |
action.accept(e); |
1459 |
+ |
if (list.modCount != expectedModCount) |
1460 |
+ |
throw new ConcurrentModificationException(); |
1461 |
+ |
return true; |
1462 |
+ |
} |
1463 |
+ |
return false; |
1464 |
+ |
} |
1465 |
+ |
|
1466 |
+ |
public void forEachRemaining(Consumer<? super E> action) { |
1467 |
+ |
int i, hi, mc; // hoist accesses and checks from loop |
1468 |
+ |
ArrayList<E> lst; Object[] a; |
1469 |
+ |
if (action == null) |
1470 |
+ |
throw new NullPointerException(); |
1471 |
+ |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1472 |
+ |
if ((hi = fence) < 0) { |
1473 |
+ |
mc = lst.modCount; |
1474 |
+ |
hi = lst.size; |
1475 |
+ |
} |
1476 |
+ |
else |
1477 |
+ |
mc = expectedModCount; |
1478 |
+ |
if ((i = index) >= 0 && (index = hi) <= a.length) { |
1479 |
+ |
for (; i < hi; ++i) { |
1480 |
+ |
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1481 |
+ |
action.accept(e); |
1482 |
+ |
} |
1483 |
+ |
if (lst.modCount == mc) |
1484 |
+ |
return; |
1485 |
+ |
} |
1486 |
+ |
} |
1487 |
+ |
throw new ConcurrentModificationException(); |
1488 |
+ |
} |
1489 |
+ |
|
1490 |
+ |
public long estimateSize() { |
1491 |
+ |
return (long) (getFence() - index); |
1492 |
+ |
} |
1493 |
+ |
|
1494 |
+ |
public int characteristics() { |
1495 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1496 |
+ |
} |
1497 |
+ |
} |
1498 |
+ |
|
1499 |
+ |
// A tiny bit set implementation |
1500 |
+ |
|
1501 |
+ |
private static long[] nBits(int n) { |
1502 |
+ |
return new long[((n - 1) >> 6) + 1]; |
1503 |
+ |
} |
1504 |
+ |
private static void setBit(long[] bits, int i) { |
1505 |
+ |
bits[i >> 6] |= 1L << i; |
1506 |
+ |
} |
1507 |
+ |
private static boolean isClear(long[] bits, int i) { |
1508 |
+ |
return (bits[i >> 6] & (1L << i)) == 0; |
1509 |
+ |
} |
1510 |
+ |
|
1511 |
+ |
@Override |
1512 |
+ |
public boolean removeIf(Predicate<? super E> filter) { |
1513 |
+ |
Objects.requireNonNull(filter); |
1514 |
+ |
int expectedModCount = modCount; |
1515 |
+ |
final Object[] es = elementData; |
1516 |
+ |
final int end = size; |
1517 |
+ |
final boolean modified; |
1518 |
+ |
int i; |
1519 |
+ |
// Optimize for initial run of survivors |
1520 |
+ |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
1521 |
+ |
; |
1522 |
+ |
// Tolerate predicates that reentrantly access the collection for |
1523 |
+ |
// read (but writers still get CME), so traverse once to find |
1524 |
+ |
// elements to delete, a second pass to physically expunge. |
1525 |
+ |
if (modified = (i < end)) { |
1526 |
+ |
expectedModCount++; |
1527 |
+ |
modCount++; |
1528 |
+ |
final int beg = i; |
1529 |
+ |
final long[] deathRow = nBits(end - beg); |
1530 |
+ |
deathRow[0] = 1L; // set bit 0 |
1531 |
+ |
for (i = beg + 1; i < end; i++) |
1532 |
+ |
if (filter.test(elementAt(es, i))) |
1533 |
+ |
setBit(deathRow, i - beg); |
1534 |
+ |
int w = beg; |
1535 |
+ |
for (i = beg; i < end; i++) |
1536 |
+ |
if (isClear(deathRow, i - beg)) |
1537 |
+ |
es[w++] = es[i]; |
1538 |
+ |
Arrays.fill(es, size = w, end, null); |
1539 |
+ |
} |
1540 |
+ |
if (modCount != expectedModCount) |
1541 |
+ |
throw new ConcurrentModificationException(); |
1542 |
+ |
return modified; |
1543 |
+ |
} |
1544 |
+ |
|
1545 |
+ |
@Override |
1546 |
+ |
public void replaceAll(UnaryOperator<E> operator) { |
1547 |
+ |
Objects.requireNonNull(operator); |
1548 |
+ |
final int expectedModCount = modCount; |
1549 |
+ |
final Object[] es = elementData; |
1550 |
+ |
final int size = this.size; |
1551 |
+ |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1552 |
+ |
es[i] = operator.apply(elementAt(es, i)); |
1553 |
+ |
} |
1554 |
+ |
if (modCount != expectedModCount) { |
1555 |
+ |
throw new ConcurrentModificationException(); |
1556 |
+ |
} |
1557 |
+ |
modCount++; |
1558 |
+ |
} |
1559 |
+ |
|
1560 |
+ |
@Override |
1561 |
+ |
@SuppressWarnings("unchecked") |
1562 |
+ |
public void sort(Comparator<? super E> c) { |
1563 |
+ |
final int expectedModCount = modCount; |
1564 |
+ |
Arrays.sort((E[]) elementData, 0, size, c); |
1565 |
+ |
if (modCount != expectedModCount) { |
1566 |
+ |
throw new ConcurrentModificationException(); |
1567 |
+ |
} |
1568 |
+ |
modCount++; |
1569 |
|
} |
1570 |
|
} |