1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Oracle designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
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 |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
> |
* or visit www.oracle.com if you need additional information or have any |
23 |
> |
* questions. |
24 |
|
*/ |
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 |
< |
* The <code>Vector</code> class implements a growable array of |
33 |
> |
* The {@code Vector} class implements a growable array of |
34 |
|
* objects. Like an array, it contains components that can be |
35 |
|
* accessed using an integer index. However, the size of a |
36 |
< |
* <code>Vector</code> can grow or shrink as needed to accommodate |
37 |
< |
* adding and removing items after the <code>Vector</code> has been created. |
36 |
> |
* {@code Vector} can grow or shrink as needed to accommodate |
37 |
> |
* adding and removing items after the {@code Vector} has been created. |
38 |
|
* |
39 |
|
* <p>Each vector tries to optimize storage management by maintaining a |
40 |
< |
* <code>capacity</code> and a <code>capacityIncrement</code>. The |
41 |
< |
* <code>capacity</code> is always at least as large as the vector |
40 |
> |
* {@code capacity} and a {@code capacityIncrement}. The |
41 |
> |
* {@code capacity} is always at least as large as the vector |
42 |
|
* size; it is usually larger because as components are added to the |
43 |
|
* vector, the vector's storage increases in chunks the size of |
44 |
< |
* <code>capacityIncrement</code>. An application can increase the |
44 |
> |
* {@code capacityIncrement}. An application can increase the |
45 |
|
* capacity of a vector before inserting a large number of |
46 |
|
* components; this reduces the amount of incremental reallocation. |
47 |
|
* |
48 |
< |
* <p>The Iterators returned by Vector's iterator and listIterator |
49 |
< |
* methods are <em>fail-fast</em>: if the Vector is structurally modified |
50 |
< |
* at any time after the Iterator is created, in any way except through the |
51 |
< |
* Iterator's own remove or add methods, the Iterator will throw a |
52 |
< |
* ConcurrentModificationException. Thus, in the face of concurrent |
53 |
< |
* modification, the Iterator fails quickly and cleanly, rather than risking |
54 |
< |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
55 |
< |
* The Enumerations returned by Vector's elements method are <em>not</em> |
56 |
< |
* fail-fast. |
48 |
> |
* <p id="fail-fast"> |
49 |
> |
* The iterators returned by this class's {@link #iterator() iterator} and |
50 |
> |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
51 |
> |
* if the vector is structurally modified at any time after the iterator is |
52 |
> |
* created, in any way except through the iterator's own |
53 |
> |
* {@link ListIterator#remove() remove} or |
54 |
> |
* {@link ListIterator#add(Object) add} methods, the iterator will throw a |
55 |
> |
* {@link ConcurrentModificationException}. Thus, in the face of |
56 |
> |
* concurrent modification, the iterator fails quickly and cleanly, rather |
57 |
> |
* than risking arbitrary, non-deterministic behavior at an undetermined |
58 |
> |
* time in the future. The {@link Enumeration Enumerations} returned by |
59 |
> |
* the {@link #elements() elements} method are <em>not</em> fail-fast; if the |
60 |
> |
* Vector is structurally modified at any time after the enumeration is |
61 |
> |
* created then the results of enumerating are undefined. |
62 |
|
* |
63 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
64 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
65 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
66 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
66 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
67 |
|
* Therefore, it would be wrong to write a program that depended on this |
68 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
69 |
|
* should be used only to detect bugs.</i> |
70 |
|
* |
71 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
72 |
|
* implement the {@link List} interface, making it a member of the |
73 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java |
74 |
< |
* Collections Framework</a>. Unlike the new collection |
75 |
< |
* implementations, {@code Vector} is synchronized. |
73 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
74 |
> |
* Java Collections Framework</a>. Unlike the new collection |
75 |
> |
* implementations, {@code Vector} is synchronized. If a thread-safe |
76 |
> |
* implementation is not needed, it is recommended to use {@link |
77 |
> |
* ArrayList} in place of {@code Vector}. |
78 |
> |
* |
79 |
> |
* @param <E> Type of component elements |
80 |
|
* |
81 |
|
* @author Lee Boynton |
82 |
|
* @author Jonathan Payne |
52 |
– |
* @version %I%, %G% |
83 |
|
* @see Collection |
54 |
– |
* @see List |
55 |
– |
* @see ArrayList |
84 |
|
* @see LinkedList |
85 |
< |
* @since JDK1.0 |
85 |
> |
* @since 1.0 |
86 |
|
*/ |
87 |
|
public class Vector<E> |
88 |
|
extends AbstractList<E> |
91 |
|
/** |
92 |
|
* The array buffer into which the components of the vector are |
93 |
|
* stored. The capacity of the vector is the length of this array buffer, |
94 |
< |
* and is at least large enough to contain all the vector's elements.<p> |
94 |
> |
* and is at least large enough to contain all the vector's elements. |
95 |
|
* |
96 |
< |
* Any array elements following the last element in the Vector are null. |
96 |
> |
* <p>Any array elements following the last element in the Vector are null. |
97 |
|
* |
98 |
|
* @serial |
99 |
|
*/ |
100 |
|
protected Object[] elementData; |
101 |
|
|
102 |
|
/** |
103 |
< |
* The number of valid components in this <tt>Vector</tt> object. |
104 |
< |
* Components <tt>elementData[0]</tt> through |
105 |
< |
* <tt>elementData[elementCount-1]</tt> are the actual items. |
103 |
> |
* The number of valid components in this {@code Vector} object. |
104 |
> |
* Components {@code elementData[0]} through |
105 |
> |
* {@code elementData[elementCount-1]} are the actual items. |
106 |
|
* |
107 |
|
* @serial |
108 |
|
*/ |
128 |
|
* @param initialCapacity the initial capacity of the vector |
129 |
|
* @param capacityIncrement the amount by which the capacity is |
130 |
|
* increased when the vector overflows |
131 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
132 |
< |
* is negative |
131 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
132 |
> |
* is negative |
133 |
|
*/ |
134 |
|
public Vector(int initialCapacity, int capacityIncrement) { |
135 |
< |
super(); |
135 |
> |
super(); |
136 |
|
if (initialCapacity < 0) |
137 |
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
138 |
|
initialCapacity); |
139 |
< |
this.elementData = new Object[initialCapacity]; |
140 |
< |
this.capacityIncrement = capacityIncrement; |
139 |
> |
this.elementData = new Object[initialCapacity]; |
140 |
> |
this.capacityIncrement = capacityIncrement; |
141 |
|
} |
142 |
|
|
143 |
|
/** |
145 |
|
* with its capacity increment equal to zero. |
146 |
|
* |
147 |
|
* @param initialCapacity the initial capacity of the vector |
148 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
149 |
< |
* is negative |
148 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
149 |
> |
* is negative |
150 |
|
*/ |
151 |
|
public Vector(int initialCapacity) { |
152 |
< |
this(initialCapacity, 0); |
152 |
> |
this(initialCapacity, 0); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Constructs an empty vector so that its internal data array |
157 |
< |
* has size <tt>10</tt> and its standard capacity increment is |
157 |
> |
* has size {@code 10} and its standard capacity increment is |
158 |
|
* zero. |
159 |
|
*/ |
160 |
|
public Vector() { |
161 |
< |
this(10); |
161 |
> |
this(10); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
172 |
|
* @since 1.2 |
173 |
|
*/ |
174 |
|
public Vector(Collection<? extends E> c) { |
175 |
< |
elementData = c.toArray(); |
176 |
< |
elementCount = elementData.length; |
177 |
< |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
178 |
< |
if (elementData.getClass() != Object[].class) |
179 |
< |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
175 |
> |
elementData = c.toArray(); |
176 |
> |
elementCount = elementData.length; |
177 |
> |
// defend against c.toArray (incorrectly) not returning Object[] |
178 |
> |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
179 |
> |
if (elementData.getClass() != Object[].class) |
180 |
> |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
|
* Copies the components of this vector into the specified array. |
185 |
< |
* The item at index <tt>k</tt> in this vector is copied into |
186 |
< |
* component <tt>k</tt> of <tt>anArray</tt>. |
185 |
> |
* The item at index {@code k} in this vector is copied into |
186 |
> |
* component {@code k} of {@code anArray}. |
187 |
|
* |
188 |
|
* @param anArray the array into which the components get copied |
189 |
|
* @throws NullPointerException if the given array is null |
194 |
|
* @see #toArray(Object[]) |
195 |
|
*/ |
196 |
|
public synchronized void copyInto(Object[] anArray) { |
197 |
< |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
197 |
> |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
|
* Trims the capacity of this vector to be the vector's current |
202 |
|
* size. If the capacity of this vector is larger than its current |
203 |
|
* size, then the capacity is changed to equal the size by replacing |
204 |
< |
* its internal data array, kept in the field <tt>elementData</tt>, |
204 |
> |
* its internal data array, kept in the field {@code elementData}, |
205 |
|
* with a smaller one. An application can use this operation to |
206 |
|
* minimize the storage of a vector. |
207 |
|
*/ |
208 |
|
public synchronized void trimToSize() { |
209 |
< |
modCount++; |
210 |
< |
int oldCapacity = elementData.length; |
211 |
< |
if (elementCount < oldCapacity) { |
209 |
> |
modCount++; |
210 |
> |
int oldCapacity = elementData.length; |
211 |
> |
if (elementCount < oldCapacity) { |
212 |
|
elementData = Arrays.copyOf(elementData, elementCount); |
213 |
< |
} |
213 |
> |
} |
214 |
|
} |
215 |
|
|
216 |
|
/** |
219 |
|
* the minimum capacity argument. |
220 |
|
* |
221 |
|
* <p>If the current capacity of this vector is less than |
222 |
< |
* <tt>minCapacity</tt>, then its capacity is increased by replacing its |
223 |
< |
* internal data array, kept in the field <tt>elementData</tt>, with a |
222 |
> |
* {@code minCapacity}, then its capacity is increased by replacing its |
223 |
> |
* internal data array, kept in the field {@code elementData}, with a |
224 |
|
* larger one. The size of the new data array will be the old size plus |
225 |
< |
* <tt>capacityIncrement</tt>, unless the value of |
226 |
< |
* <tt>capacityIncrement</tt> is less than or equal to zero, in which case |
225 |
> |
* {@code capacityIncrement}, unless the value of |
226 |
> |
* {@code capacityIncrement} is less than or equal to zero, in which case |
227 |
|
* the new capacity will be twice the old capacity; but if this new size |
228 |
< |
* is still smaller than <tt>minCapacity</tt>, then the new capacity will |
229 |
< |
* be <tt>minCapacity</tt>. |
228 |
> |
* is still smaller than {@code minCapacity}, then the new capacity will |
229 |
> |
* be {@code minCapacity}. |
230 |
|
* |
231 |
|
* @param minCapacity the desired minimum capacity |
232 |
|
*/ |
233 |
|
public synchronized void ensureCapacity(int minCapacity) { |
234 |
< |
modCount++; |
235 |
< |
ensureCapacityHelper(minCapacity); |
234 |
> |
if (minCapacity > 0) { |
235 |
> |
modCount++; |
236 |
> |
if (minCapacity > elementData.length) |
237 |
> |
grow(minCapacity); |
238 |
> |
} |
239 |
> |
} |
240 |
> |
|
241 |
> |
/** |
242 |
> |
* The maximum size of array to allocate (unless necessary). |
243 |
> |
* Some VMs reserve some header words in an array. |
244 |
> |
* Attempts to allocate larger arrays may result in |
245 |
> |
* OutOfMemoryError: Requested array size exceeds VM limit |
246 |
> |
*/ |
247 |
> |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
248 |
> |
|
249 |
> |
/** |
250 |
> |
* Increases the capacity to ensure that it can hold at least the |
251 |
> |
* number of elements specified by the minimum capacity argument. |
252 |
> |
* |
253 |
> |
* @param minCapacity the desired minimum capacity |
254 |
> |
* @throws OutOfMemoryError if minCapacity is less than zero |
255 |
> |
*/ |
256 |
> |
private Object[] grow(int minCapacity) { |
257 |
> |
return elementData = Arrays.copyOf(elementData, |
258 |
> |
newCapacity(minCapacity)); |
259 |
> |
} |
260 |
> |
|
261 |
> |
private Object[] grow() { |
262 |
> |
return grow(elementCount + 1); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* This implements the unsynchronized semantics of ensureCapacity. |
267 |
< |
* Synchronized methods in this class can internally call this |
268 |
< |
* method for ensuring capacity without incurring the cost of an |
269 |
< |
* extra synchronization. |
270 |
< |
* |
271 |
< |
* @see java.util.Vector#ensureCapacity(int) |
272 |
< |
*/ |
273 |
< |
private void ensureCapacityHelper(int minCapacity) { |
274 |
< |
int oldCapacity = elementData.length; |
275 |
< |
if (minCapacity > oldCapacity) { |
276 |
< |
Object[] oldData = elementData; |
277 |
< |
int newCapacity = (capacityIncrement > 0) ? |
278 |
< |
(oldCapacity + capacityIncrement) : (oldCapacity * 2); |
279 |
< |
if (newCapacity < minCapacity) { |
280 |
< |
newCapacity = minCapacity; |
281 |
< |
} |
282 |
< |
elementData = Arrays.copyOf(elementData, newCapacity); |
283 |
< |
} |
266 |
> |
* Returns a capacity at least as large as the given minimum capacity. |
267 |
> |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
268 |
> |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
269 |
> |
* |
270 |
> |
* @param minCapacity the desired minimum capacity |
271 |
> |
* @throws OutOfMemoryError if minCapacity is less than zero |
272 |
> |
*/ |
273 |
> |
private int newCapacity(int minCapacity) { |
274 |
> |
// overflow-conscious code |
275 |
> |
int oldCapacity = elementData.length; |
276 |
> |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
277 |
> |
capacityIncrement : oldCapacity); |
278 |
> |
if (newCapacity - minCapacity <= 0) { |
279 |
> |
if (minCapacity < 0) // overflow |
280 |
> |
throw new OutOfMemoryError(); |
281 |
> |
return minCapacity; |
282 |
> |
} |
283 |
> |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
284 |
> |
? newCapacity |
285 |
> |
: hugeCapacity(minCapacity); |
286 |
> |
} |
287 |
> |
|
288 |
> |
private static int hugeCapacity(int minCapacity) { |
289 |
> |
if (minCapacity < 0) // overflow |
290 |
> |
throw new OutOfMemoryError(); |
291 |
> |
return (minCapacity > MAX_ARRAY_SIZE) ? |
292 |
> |
Integer.MAX_VALUE : |
293 |
> |
MAX_ARRAY_SIZE; |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
|
* Sets the size of this vector. If the new size is greater than the |
298 |
< |
* current size, new <code>null</code> items are added to the end of |
298 |
> |
* current size, new {@code null} items are added to the end of |
299 |
|
* the vector. If the new size is less than the current size, all |
300 |
< |
* components at index <code>newSize</code> and greater are discarded. |
300 |
> |
* components at index {@code newSize} and greater are discarded. |
301 |
|
* |
302 |
< |
* @param newSize the new size of this vector |
303 |
< |
* @throws ArrayIndexOutOfBoundsException if new size is negative |
302 |
> |
* @param newSize the new size of this vector |
303 |
> |
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
304 |
|
*/ |
305 |
|
public synchronized void setSize(int newSize) { |
306 |
< |
modCount++; |
307 |
< |
if (newSize > elementCount) { |
308 |
< |
ensureCapacityHelper(newSize); |
309 |
< |
} else { |
310 |
< |
for (int i = newSize ; i < elementCount ; i++) { |
311 |
< |
elementData[i] = null; |
246 |
< |
} |
247 |
< |
} |
248 |
< |
elementCount = newSize; |
306 |
> |
modCount++; |
307 |
> |
if (newSize > elementData.length) |
308 |
> |
grow(newSize); |
309 |
> |
final Object[] es = elementData; |
310 |
> |
for (int to = elementCount, i = elementCount = newSize; i < to; i++) |
311 |
> |
es[i] = null; |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
|
* Returns the current capacity of this vector. |
316 |
|
* |
317 |
|
* @return the current capacity (the length of its internal |
318 |
< |
* data array, kept in the field <tt>elementData</tt> |
318 |
> |
* data array, kept in the field {@code elementData} |
319 |
|
* of this vector) |
320 |
|
*/ |
321 |
|
public synchronized int capacity() { |
322 |
< |
return elementData.length; |
322 |
> |
return elementData.length; |
323 |
|
} |
324 |
|
|
325 |
|
/** |
328 |
|
* @return the number of components in this vector |
329 |
|
*/ |
330 |
|
public synchronized int size() { |
331 |
< |
return elementCount; |
331 |
> |
return elementCount; |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
|
* Tests if this vector has no components. |
336 |
|
* |
337 |
< |
* @return <code>true</code> if and only if this vector has |
337 |
> |
* @return {@code true} if and only if this vector has |
338 |
|
* no components, that is, its size is zero; |
339 |
< |
* <code>false</code> otherwise. |
339 |
> |
* {@code false} otherwise. |
340 |
|
*/ |
341 |
|
public synchronized boolean isEmpty() { |
342 |
< |
return elementCount == 0; |
342 |
> |
return elementCount == 0; |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
|
* Returns an enumeration of the components of this vector. The |
347 |
< |
* returned <tt>Enumeration</tt> object will generate all items in |
348 |
< |
* this vector. The first item generated is the item at index <tt>0</tt>, |
349 |
< |
* then the item at index <tt>1</tt>, and so on. |
347 |
> |
* returned {@code Enumeration} object will generate all items in |
348 |
> |
* this vector. The first item generated is the item at index {@code 0}, |
349 |
> |
* then the item at index {@code 1}, and so on. If the vector is |
350 |
> |
* structurally modified while enumerating over the elements then the |
351 |
> |
* results of enumerating are undefined. |
352 |
|
* |
353 |
|
* @return an enumeration of the components of this vector |
289 |
– |
* @see Enumeration |
354 |
|
* @see Iterator |
355 |
|
*/ |
356 |
|
public Enumeration<E> elements() { |
357 |
< |
return new Enumeration<E>() { |
358 |
< |
int count = 0; |
357 |
> |
return new Enumeration<E>() { |
358 |
> |
int count = 0; |
359 |
|
|
360 |
< |
public boolean hasMoreElements() { |
361 |
< |
return count < elementCount; |
362 |
< |
} |
363 |
< |
|
364 |
< |
public E nextElement() { |
365 |
< |
synchronized (Vector.this) { |
366 |
< |
if (count < elementCount) { |
367 |
< |
return (E)elementData[count++]; |
368 |
< |
} |
369 |
< |
} |
370 |
< |
throw new NoSuchElementException("Vector Enumeration"); |
371 |
< |
} |
372 |
< |
}; |
360 |
> |
public boolean hasMoreElements() { |
361 |
> |
return count < elementCount; |
362 |
> |
} |
363 |
> |
|
364 |
> |
public E nextElement() { |
365 |
> |
synchronized (Vector.this) { |
366 |
> |
if (count < elementCount) { |
367 |
> |
return elementData(count++); |
368 |
> |
} |
369 |
> |
} |
370 |
> |
throw new NoSuchElementException("Vector Enumeration"); |
371 |
> |
} |
372 |
> |
}; |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
< |
* Returns <tt>true</tt> if this vector contains the specified element. |
377 |
< |
* More formally, returns <tt>true</tt> if and only if this vector |
378 |
< |
* contains at least one element <tt>e</tt> such that |
379 |
< |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
376 |
> |
* Returns {@code true} if this vector contains the specified element. |
377 |
> |
* More formally, returns {@code true} if and only if this vector |
378 |
> |
* contains at least one element {@code e} such that |
379 |
> |
* {@code Objects.equals(o, e)}. |
380 |
|
* |
381 |
|
* @param o element whose presence in this vector is to be tested |
382 |
< |
* @return <tt>true</tt> if this vector contains the specified element |
382 |
> |
* @return {@code true} if this vector contains the specified element |
383 |
|
*/ |
384 |
|
public boolean contains(Object o) { |
385 |
< |
return indexOf(o, 0) >= 0; |
385 |
> |
return indexOf(o, 0) >= 0; |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* Returns the index of the first occurrence of the specified element |
390 |
|
* in this vector, or -1 if this vector does not contain the element. |
391 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
392 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
391 |
> |
* More formally, returns the lowest index {@code i} such that |
392 |
> |
* {@code Objects.equals(o, get(i))}, |
393 |
|
* or -1 if there is no such index. |
394 |
|
* |
395 |
|
* @param o element to search for |
397 |
|
* this vector, or -1 if this vector does not contain the element |
398 |
|
*/ |
399 |
|
public int indexOf(Object o) { |
400 |
< |
return indexOf(o, 0); |
400 |
> |
return indexOf(o, 0); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* Returns the index of the first occurrence of the specified element in |
405 |
< |
* this vector, searching forwards from <tt>index</tt>, or returns -1 if |
405 |
> |
* this vector, searching forwards from {@code index}, or returns -1 if |
406 |
|
* the element is not found. |
407 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
408 |
< |
* <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
407 |
> |
* More formally, returns the lowest index {@code i} such that |
408 |
> |
* {@code (i >= index && Objects.equals(o, get(i)))}, |
409 |
|
* or -1 if there is no such index. |
410 |
|
* |
411 |
|
* @param o element to search for |
412 |
|
* @param index index to start searching from |
413 |
|
* @return the index of the first occurrence of the element in |
414 |
< |
* this vector at position <tt>index</tt> or later in the vector; |
415 |
< |
* <tt>-1</tt> if the element is not found. |
414 |
> |
* this vector at position {@code index} or later in the vector; |
415 |
> |
* {@code -1} if the element is not found. |
416 |
|
* @throws IndexOutOfBoundsException if the specified index is negative |
417 |
|
* @see Object#equals(Object) |
418 |
|
*/ |
419 |
|
public synchronized int indexOf(Object o, int index) { |
420 |
< |
if (o == null) { |
421 |
< |
for (int i = index ; i < elementCount ; i++) |
422 |
< |
if (elementData[i]==null) |
423 |
< |
return i; |
424 |
< |
} else { |
425 |
< |
for (int i = index ; i < elementCount ; i++) |
426 |
< |
if (o.equals(elementData[i])) |
427 |
< |
return i; |
428 |
< |
} |
429 |
< |
return -1; |
420 |
> |
if (o == null) { |
421 |
> |
for (int i = index ; i < elementCount ; i++) |
422 |
> |
if (elementData[i]==null) |
423 |
> |
return i; |
424 |
> |
} else { |
425 |
> |
for (int i = index ; i < elementCount ; i++) |
426 |
> |
if (o.equals(elementData[i])) |
427 |
> |
return i; |
428 |
> |
} |
429 |
> |
return -1; |
430 |
|
} |
431 |
|
|
432 |
|
/** |
433 |
|
* Returns the index of the last occurrence of the specified element |
434 |
|
* in this vector, or -1 if this vector does not contain the element. |
435 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
436 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
435 |
> |
* More formally, returns the highest index {@code i} such that |
436 |
> |
* {@code Objects.equals(o, get(i))}, |
437 |
|
* or -1 if there is no such index. |
438 |
|
* |
439 |
|
* @param o element to search for |
441 |
|
* this vector, or -1 if this vector does not contain the element |
442 |
|
*/ |
443 |
|
public synchronized int lastIndexOf(Object o) { |
444 |
< |
return lastIndexOf(o, elementCount-1); |
444 |
> |
return lastIndexOf(o, elementCount-1); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* Returns the index of the last occurrence of the specified element in |
449 |
< |
* this vector, searching backwards from <tt>index</tt>, or returns -1 if |
449 |
> |
* this vector, searching backwards from {@code index}, or returns -1 if |
450 |
|
* the element is not found. |
451 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
452 |
< |
* <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
451 |
> |
* More formally, returns the highest index {@code i} such that |
452 |
> |
* {@code (i <= index && Objects.equals(o, get(i)))}, |
453 |
|
* or -1 if there is no such index. |
454 |
|
* |
455 |
|
* @param o element to search for |
456 |
|
* @param index index to start searching backwards from |
457 |
|
* @return the index of the last occurrence of the element at position |
458 |
< |
* less than or equal to <tt>index</tt> in this vector; |
458 |
> |
* less than or equal to {@code index} in this vector; |
459 |
|
* -1 if the element is not found. |
460 |
|
* @throws IndexOutOfBoundsException if the specified index is greater |
461 |
|
* than or equal to the current size of this vector |
464 |
|
if (index >= elementCount) |
465 |
|
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
466 |
|
|
467 |
< |
if (o == null) { |
468 |
< |
for (int i = index; i >= 0; i--) |
469 |
< |
if (elementData[i]==null) |
470 |
< |
return i; |
471 |
< |
} else { |
472 |
< |
for (int i = index; i >= 0; i--) |
473 |
< |
if (o.equals(elementData[i])) |
474 |
< |
return i; |
475 |
< |
} |
476 |
< |
return -1; |
467 |
> |
if (o == null) { |
468 |
> |
for (int i = index; i >= 0; i--) |
469 |
> |
if (elementData[i]==null) |
470 |
> |
return i; |
471 |
> |
} else { |
472 |
> |
for (int i = index; i >= 0; i--) |
473 |
> |
if (o.equals(elementData[i])) |
474 |
> |
return i; |
475 |
> |
} |
476 |
> |
return -1; |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Returns the component at the specified index.<p> |
480 |
> |
* Returns the component at the specified index. |
481 |
|
* |
482 |
< |
* This method is identical in functionality to the get method |
483 |
< |
* (which is part of the List interface). |
482 |
> |
* <p>This method is identical in functionality to the {@link #get(int)} |
483 |
> |
* method (which is part of the {@link List} interface). |
484 |
|
* |
485 |
|
* @param index an index into this vector |
486 |
|
* @return the component at the specified index |
487 |
< |
* @exception ArrayIndexOutOfBoundsException if the <tt>index</tt> |
488 |
< |
* is negative or not less than the current size of this |
425 |
< |
* <tt>Vector</tt> object. |
426 |
< |
* @see #get(int) |
427 |
< |
* @see List |
487 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
488 |
> |
* ({@code index < 0 || index >= size()}) |
489 |
|
*/ |
490 |
|
public synchronized E elementAt(int index) { |
491 |
< |
if (index >= elementCount) { |
492 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
493 |
< |
} |
491 |
> |
if (index >= elementCount) { |
492 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
493 |
> |
} |
494 |
|
|
495 |
< |
return (E)elementData[index]; |
495 |
> |
return elementData(index); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* Returns the first component (the item at index <tt>0</tt>) of |
499 |
> |
* Returns the first component (the item at index {@code 0}) of |
500 |
|
* this vector. |
501 |
|
* |
502 |
|
* @return the first component of this vector |
503 |
< |
* @exception NoSuchElementException if this vector has no components |
503 |
> |
* @throws NoSuchElementException if this vector has no components |
504 |
|
*/ |
505 |
|
public synchronized E firstElement() { |
506 |
< |
if (elementCount == 0) { |
507 |
< |
throw new NoSuchElementException(); |
508 |
< |
} |
509 |
< |
return (E)elementData[0]; |
506 |
> |
if (elementCount == 0) { |
507 |
> |
throw new NoSuchElementException(); |
508 |
> |
} |
509 |
> |
return elementData(0); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
|
* Returns the last component of the vector. |
514 |
|
* |
515 |
|
* @return the last component of the vector, i.e., the component at index |
516 |
< |
* <code>size() - 1</code>. |
517 |
< |
* @exception NoSuchElementException if this vector is empty |
516 |
> |
* {@code size() - 1} |
517 |
> |
* @throws NoSuchElementException if this vector is empty |
518 |
|
*/ |
519 |
|
public synchronized E lastElement() { |
520 |
< |
if (elementCount == 0) { |
521 |
< |
throw new NoSuchElementException(); |
522 |
< |
} |
523 |
< |
return (E)elementData[elementCount - 1]; |
520 |
> |
if (elementCount == 0) { |
521 |
> |
throw new NoSuchElementException(); |
522 |
> |
} |
523 |
> |
return elementData(elementCount - 1); |
524 |
|
} |
525 |
|
|
526 |
|
/** |
527 |
< |
* Sets the component at the specified <code>index</code> of this |
527 |
> |
* Sets the component at the specified {@code index} of this |
528 |
|
* vector to be the specified object. The previous component at that |
529 |
< |
* position is discarded.<p> |
529 |
> |
* position is discarded. |
530 |
|
* |
531 |
< |
* The index must be a value greater than or equal to <code>0</code> |
532 |
< |
* and less than the current size of the vector. <p> |
531 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
532 |
> |
* and less than the current size of the vector. |
533 |
|
* |
534 |
< |
* This method is identical in functionality to the set method |
535 |
< |
* (which is part of the List interface). Note that the set method reverses |
536 |
< |
* the order of the parameters, to more closely match array usage. Note |
537 |
< |
* also that the set method returns the old value that was stored at the |
538 |
< |
* specified position. |
534 |
> |
* <p>This method is identical in functionality to the |
535 |
> |
* {@link #set(int, Object) set(int, E)} |
536 |
> |
* method (which is part of the {@link List} interface). Note that the |
537 |
> |
* {@code set} method reverses the order of the parameters, to more closely |
538 |
> |
* match array usage. Note also that the {@code set} method returns the |
539 |
> |
* old value that was stored at the specified position. |
540 |
|
* |
541 |
|
* @param obj what the component is to be set to |
542 |
|
* @param index the specified index |
543 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
544 |
< |
* @see #size() |
483 |
< |
* @see List |
484 |
< |
* @see #set(int, java.lang.Object) |
543 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
544 |
> |
* ({@code index < 0 || index >= size()}) |
545 |
|
*/ |
546 |
|
public synchronized void setElementAt(E obj, int index) { |
547 |
< |
if (index >= elementCount) { |
548 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
549 |
< |
elementCount); |
550 |
< |
} |
551 |
< |
elementData[index] = obj; |
547 |
> |
if (index >= elementCount) { |
548 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
549 |
> |
elementCount); |
550 |
> |
} |
551 |
> |
elementData[index] = obj; |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
|
* Deletes the component at the specified index. Each component in |
556 |
|
* this vector with an index greater or equal to the specified |
557 |
< |
* <code>index</code> is shifted downward to have an index one |
557 |
> |
* {@code index} is shifted downward to have an index one |
558 |
|
* smaller than the value it had previously. The size of this vector |
559 |
< |
* is decreased by <tt>1</tt>.<p> |
559 |
> |
* is decreased by {@code 1}. |
560 |
|
* |
561 |
< |
* The index must be a value greater than or equal to <code>0</code> |
562 |
< |
* and less than the current size of the vector. <p> |
561 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
562 |
> |
* and less than the current size of the vector. |
563 |
|
* |
564 |
< |
* This method is identical in functionality to the remove method |
565 |
< |
* (which is part of the List interface). Note that the remove method |
566 |
< |
* returns the old value that was stored at the specified position. |
564 |
> |
* <p>This method is identical in functionality to the {@link #remove(int)} |
565 |
> |
* method (which is part of the {@link List} interface). Note that the |
566 |
> |
* {@code remove} method returns the old value that was stored at the |
567 |
> |
* specified position. |
568 |
|
* |
569 |
|
* @param index the index of the object to remove |
570 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
571 |
< |
* @see #size() |
511 |
< |
* @see #remove(int) |
512 |
< |
* @see List |
570 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
571 |
> |
* ({@code index < 0 || index >= size()}) |
572 |
|
*/ |
573 |
|
public synchronized void removeElementAt(int index) { |
574 |
< |
modCount++; |
575 |
< |
if (index >= elementCount) { |
576 |
< |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
577 |
< |
elementCount); |
578 |
< |
} |
579 |
< |
else if (index < 0) { |
580 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
581 |
< |
} |
582 |
< |
int j = elementCount - index - 1; |
583 |
< |
if (j > 0) { |
584 |
< |
System.arraycopy(elementData, index + 1, elementData, index, j); |
585 |
< |
} |
586 |
< |
elementCount--; |
587 |
< |
elementData[elementCount] = null; /* to let gc do its work */ |
574 |
> |
if (index >= elementCount) { |
575 |
> |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
576 |
> |
elementCount); |
577 |
> |
} |
578 |
> |
else if (index < 0) { |
579 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
580 |
> |
} |
581 |
> |
int j = elementCount - index - 1; |
582 |
> |
if (j > 0) { |
583 |
> |
System.arraycopy(elementData, index + 1, elementData, index, j); |
584 |
> |
} |
585 |
> |
modCount++; |
586 |
> |
elementCount--; |
587 |
> |
elementData[elementCount] = null; /* to let gc do its work */ |
588 |
> |
// checkInvariants(); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
592 |
|
* Inserts the specified object as a component in this vector at the |
593 |
< |
* specified <code>index</code>. Each component in this vector with |
594 |
< |
* an index greater or equal to the specified <code>index</code> is |
593 |
> |
* specified {@code index}. Each component in this vector with |
594 |
> |
* an index greater or equal to the specified {@code index} is |
595 |
|
* shifted upward to have an index one greater than the value it had |
596 |
< |
* previously. <p> |
596 |
> |
* previously. |
597 |
|
* |
598 |
< |
* The index must be a value greater than or equal to <code>0</code> |
598 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
599 |
|
* and less than or equal to the current size of the vector. (If the |
600 |
|
* index is equal to the current size of the vector, the new element |
601 |
< |
* is appended to the Vector.)<p> |
601 |
> |
* is appended to the Vector.) |
602 |
|
* |
603 |
< |
* This method is identical in functionality to the add(Object, int) method |
604 |
< |
* (which is part of the List interface). Note that the add method reverses |
605 |
< |
* the order of the parameters, to more closely match array usage. |
603 |
> |
* <p>This method is identical in functionality to the |
604 |
> |
* {@link #add(int, Object) add(int, E)} |
605 |
> |
* method (which is part of the {@link List} interface). Note that the |
606 |
> |
* {@code add} method reverses the order of the parameters, to more closely |
607 |
> |
* match array usage. |
608 |
|
* |
609 |
|
* @param obj the component to insert |
610 |
|
* @param index where to insert the new component |
611 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
612 |
< |
* @see #size() |
551 |
< |
* @see #add(int, Object) |
552 |
< |
* @see List |
611 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
612 |
> |
* ({@code index < 0 || index > size()}) |
613 |
|
*/ |
614 |
|
public synchronized void insertElementAt(E obj, int index) { |
615 |
< |
modCount++; |
616 |
< |
if (index > elementCount) { |
617 |
< |
throw new ArrayIndexOutOfBoundsException(index |
618 |
< |
+ " > " + elementCount); |
619 |
< |
} |
620 |
< |
ensureCapacityHelper(elementCount + 1); |
621 |
< |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
622 |
< |
elementData[index] = obj; |
623 |
< |
elementCount++; |
615 |
> |
if (index > elementCount) { |
616 |
> |
throw new ArrayIndexOutOfBoundsException(index |
617 |
> |
+ " > " + elementCount); |
618 |
> |
} |
619 |
> |
modCount++; |
620 |
> |
final int s = elementCount; |
621 |
> |
Object[] elementData = this.elementData; |
622 |
> |
if (s == elementData.length) |
623 |
> |
elementData = grow(); |
624 |
> |
System.arraycopy(elementData, index, |
625 |
> |
elementData, index + 1, |
626 |
> |
s - index); |
627 |
> |
elementData[index] = obj; |
628 |
> |
elementCount = s + 1; |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
|
* Adds the specified component to the end of this vector, |
633 |
|
* increasing its size by one. The capacity of this vector is |
634 |
< |
* increased if its size becomes greater than its capacity. <p> |
634 |
> |
* increased if its size becomes greater than its capacity. |
635 |
|
* |
636 |
< |
* This method is identical in functionality to the add(Object) method |
637 |
< |
* (which is part of the List interface). |
636 |
> |
* <p>This method is identical in functionality to the |
637 |
> |
* {@link #add(Object) add(E)} |
638 |
> |
* method (which is part of the {@link List} interface). |
639 |
|
* |
640 |
|
* @param obj the component to be added |
575 |
– |
* @see #add(Object) |
576 |
– |
* @see List |
641 |
|
*/ |
642 |
|
public synchronized void addElement(E obj) { |
643 |
< |
modCount++; |
644 |
< |
ensureCapacityHelper(elementCount + 1); |
581 |
< |
elementData[elementCount++] = obj; |
643 |
> |
modCount++; |
644 |
> |
add(obj, elementData, elementCount); |
645 |
|
} |
646 |
|
|
647 |
|
/** |
649 |
|
* from this vector. If the object is found in this vector, each |
650 |
|
* component in the vector with an index greater or equal to the |
651 |
|
* object's index is shifted downward to have an index one smaller |
652 |
< |
* than the value it had previously.<p> |
652 |
> |
* than the value it had previously. |
653 |
|
* |
654 |
< |
* This method is identical in functionality to the remove(Object) |
655 |
< |
* method (which is part of the List interface). |
654 |
> |
* <p>This method is identical in functionality to the |
655 |
> |
* {@link #remove(Object)} method (which is part of the |
656 |
> |
* {@link List} interface). |
657 |
|
* |
658 |
|
* @param obj the component to be removed |
659 |
< |
* @return <code>true</code> if the argument was a component of this |
660 |
< |
* vector; <code>false</code> otherwise. |
597 |
< |
* @see List#remove(Object) |
598 |
< |
* @see List |
659 |
> |
* @return {@code true} if the argument was a component of this |
660 |
> |
* vector; {@code false} otherwise. |
661 |
|
*/ |
662 |
|
public synchronized boolean removeElement(Object obj) { |
663 |
< |
modCount++; |
664 |
< |
int i = indexOf(obj); |
665 |
< |
if (i >= 0) { |
666 |
< |
removeElementAt(i); |
667 |
< |
return true; |
668 |
< |
} |
669 |
< |
return false; |
663 |
> |
modCount++; |
664 |
> |
int i = indexOf(obj); |
665 |
> |
if (i >= 0) { |
666 |
> |
removeElementAt(i); |
667 |
> |
return true; |
668 |
> |
} |
669 |
> |
return false; |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
< |
* Removes all components from this vector and sets its size to zero.<p> |
673 |
> |
* Removes all components from this vector and sets its size to zero. |
674 |
|
* |
675 |
< |
* This method is identical in functionality to the clear method |
676 |
< |
* (which is part of the List interface). |
615 |
< |
* |
616 |
< |
* @see #clear |
617 |
< |
* @see List |
675 |
> |
* <p>This method is identical in functionality to the {@link #clear} |
676 |
> |
* method (which is part of the {@link List} interface). |
677 |
|
*/ |
678 |
|
public synchronized void removeAllElements() { |
679 |
+ |
final Object[] es = elementData; |
680 |
+ |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
681 |
+ |
es[i] = null; |
682 |
|
modCount++; |
621 |
– |
// Let gc do its work |
622 |
– |
for (int i = 0; i < elementCount; i++) |
623 |
– |
elementData[i] = null; |
624 |
– |
|
625 |
– |
elementCount = 0; |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
|
* Returns a clone of this vector. The copy will contain a |
687 |
|
* reference to a clone of the internal data array, not a reference |
688 |
< |
* to the original internal data array of this <tt>Vector</tt> object. |
688 |
> |
* to the original internal data array of this {@code Vector} object. |
689 |
|
* |
690 |
|
* @return a clone of this vector |
691 |
|
*/ |
692 |
|
public synchronized Object clone() { |
693 |
< |
try { |
694 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
695 |
< |
v.elementData = Arrays.copyOf(elementData, elementCount); |
696 |
< |
v.modCount = 0; |
697 |
< |
return v; |
698 |
< |
} catch (CloneNotSupportedException e) { |
699 |
< |
// this shouldn't happen, since we are Cloneable |
700 |
< |
throw new InternalError(); |
701 |
< |
} |
693 |
> |
try { |
694 |
> |
@SuppressWarnings("unchecked") |
695 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
696 |
> |
v.elementData = Arrays.copyOf(elementData, elementCount); |
697 |
> |
v.modCount = 0; |
698 |
> |
return v; |
699 |
> |
} catch (CloneNotSupportedException e) { |
700 |
> |
// this shouldn't happen, since we are Cloneable |
701 |
> |
throw new InternalError(e); |
702 |
> |
} |
703 |
|
} |
704 |
|
|
705 |
|
/** |
717 |
|
* correct order; the runtime type of the returned array is that of the |
718 |
|
* specified array. If the Vector fits in the specified array, it is |
719 |
|
* returned therein. Otherwise, a new array is allocated with the runtime |
720 |
< |
* type of the specified array and the size of this Vector.<p> |
720 |
> |
* type of the specified array and the size of this Vector. |
721 |
|
* |
722 |
< |
* If the Vector fits in the specified array with room to spare |
722 |
> |
* <p>If the Vector fits in the specified array with room to spare |
723 |
|
* (i.e., the array has more elements than the Vector), |
724 |
|
* the element in the array immediately following the end of the |
725 |
|
* Vector is set to null. (This is useful in determining the length |
726 |
|
* of the Vector <em>only</em> if the caller knows that the Vector |
727 |
|
* does not contain any null elements.) |
728 |
|
* |
729 |
+ |
* @param <T> type of array elements. The same type as {@code <E>} or a |
730 |
+ |
* supertype of {@code <E>}. |
731 |
|
* @param a the array into which the elements of the Vector are to |
732 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
733 |
< |
* same runtime type is allocated for this purpose. |
732 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
733 |
> |
* same runtime type is allocated for this purpose. |
734 |
|
* @return an array containing the elements of the Vector |
735 |
< |
* @exception ArrayStoreException the runtime type of a is not a supertype |
736 |
< |
* of the runtime type of every element in this Vector |
735 |
> |
* @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
736 |
> |
* a supertype of the runtime type, {@code <E>}, of every element in this |
737 |
> |
* Vector |
738 |
|
* @throws NullPointerException if the given array is null |
739 |
|
* @since 1.2 |
740 |
|
*/ |
741 |
+ |
@SuppressWarnings("unchecked") |
742 |
|
public synchronized <T> T[] toArray(T[] a) { |
743 |
|
if (a.length < elementCount) |
744 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
745 |
|
|
746 |
< |
System.arraycopy(elementData, 0, a, 0, elementCount); |
746 |
> |
System.arraycopy(elementData, 0, a, 0, elementCount); |
747 |
|
|
748 |
|
if (a.length > elementCount) |
749 |
|
a[elementCount] = null; |
753 |
|
|
754 |
|
// Positional Access Operations |
755 |
|
|
756 |
+ |
@SuppressWarnings("unchecked") |
757 |
+ |
E elementData(int index) { |
758 |
+ |
return (E) elementData[index]; |
759 |
+ |
} |
760 |
+ |
|
761 |
+ |
@SuppressWarnings("unchecked") |
762 |
+ |
static <E> E elementAt(Object[] es, int index) { |
763 |
+ |
return (E) es[index]; |
764 |
+ |
} |
765 |
+ |
|
766 |
|
/** |
767 |
|
* Returns the element at the specified position in this Vector. |
768 |
|
* |
769 |
|
* @param index index of the element to return |
770 |
|
* @return object at the specified index |
771 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range (index |
772 |
< |
* < 0 || index >= size()) |
771 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
772 |
> |
* ({@code index < 0 || index >= size()}) |
773 |
|
* @since 1.2 |
774 |
|
*/ |
775 |
|
public synchronized E get(int index) { |
776 |
< |
if (index >= elementCount) |
777 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
776 |
> |
if (index >= elementCount) |
777 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
778 |
|
|
779 |
< |
return (E)elementData[index]; |
779 |
> |
return elementData(index); |
780 |
|
} |
781 |
|
|
782 |
|
/** |
786 |
|
* @param index index of the element to replace |
787 |
|
* @param element element to be stored at the specified position |
788 |
|
* @return the element previously at the specified position |
789 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range |
790 |
< |
* (index < 0 || index >= size()) |
789 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
790 |
> |
* ({@code index < 0 || index >= size()}) |
791 |
|
* @since 1.2 |
792 |
|
*/ |
793 |
|
public synchronized E set(int index, E element) { |
794 |
< |
if (index >= elementCount) |
795 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
794 |
> |
if (index >= elementCount) |
795 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
796 |
|
|
797 |
< |
Object oldValue = elementData[index]; |
798 |
< |
elementData[index] = element; |
799 |
< |
return (E)oldValue; |
797 |
> |
E oldValue = elementData(index); |
798 |
> |
elementData[index] = element; |
799 |
> |
return oldValue; |
800 |
> |
} |
801 |
> |
|
802 |
> |
/** |
803 |
> |
* This helper method split out from add(E) to keep method |
804 |
> |
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
805 |
> |
* which helps when add(E) is called in a C1-compiled loop. |
806 |
> |
*/ |
807 |
> |
private void add(E e, Object[] elementData, int s) { |
808 |
> |
if (s == elementData.length) |
809 |
> |
elementData = grow(); |
810 |
> |
elementData[s] = e; |
811 |
> |
elementCount = s + 1; |
812 |
> |
// checkInvariants(); |
813 |
|
} |
814 |
|
|
815 |
|
/** |
816 |
|
* Appends the specified element to the end of this Vector. |
817 |
|
* |
818 |
|
* @param e element to be appended to this Vector |
819 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
819 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
820 |
|
* @since 1.2 |
821 |
|
*/ |
822 |
|
public synchronized boolean add(E e) { |
823 |
< |
modCount++; |
824 |
< |
ensureCapacityHelper(elementCount + 1); |
740 |
< |
elementData[elementCount++] = e; |
823 |
> |
modCount++; |
824 |
> |
add(e, elementData, elementCount); |
825 |
|
return true; |
826 |
|
} |
827 |
|
|
829 |
|
* Removes the first occurrence of the specified element in this Vector |
830 |
|
* If the Vector does not contain the element, it is unchanged. More |
831 |
|
* formally, removes the element with the lowest index i such that |
832 |
< |
* <code>(o==null ? get(i)==null : o.equals(get(i)))</code> (if such |
832 |
> |
* {@code Objects.equals(o, get(i))} (if such |
833 |
|
* an element exists). |
834 |
|
* |
835 |
|
* @param o element to be removed from this Vector, if present |
847 |
|
* |
848 |
|
* @param index index at which the specified element is to be inserted |
849 |
|
* @param element element to be inserted |
850 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range |
851 |
< |
* (index < 0 || index > size()) |
850 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
851 |
> |
* ({@code index < 0 || index > size()}) |
852 |
|
* @since 1.2 |
853 |
|
*/ |
854 |
|
public void add(int index, E element) { |
860 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
861 |
|
* indices). Returns the element that was removed from the Vector. |
862 |
|
* |
779 |
– |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
780 |
– |
* < 0 || index >= size()) |
863 |
|
* @param index the index of the element to be removed |
864 |
|
* @return element that was removed |
865 |
+ |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
866 |
+ |
* ({@code index < 0 || index >= size()}) |
867 |
|
* @since 1.2 |
868 |
|
*/ |
869 |
|
public synchronized E remove(int index) { |
870 |
< |
modCount++; |
871 |
< |
if (index >= elementCount) |
872 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
873 |
< |
Object oldValue = elementData[index]; |
874 |
< |
|
875 |
< |
int numMoved = elementCount - index - 1; |
876 |
< |
if (numMoved > 0) |
877 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
878 |
< |
numMoved); |
879 |
< |
elementData[--elementCount] = null; // Let gc do its work |
870 |
> |
modCount++; |
871 |
> |
if (index >= elementCount) |
872 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
873 |
> |
E oldValue = elementData(index); |
874 |
> |
|
875 |
> |
int numMoved = elementCount - index - 1; |
876 |
> |
if (numMoved > 0) |
877 |
> |
System.arraycopy(elementData, index+1, elementData, index, |
878 |
> |
numMoved); |
879 |
> |
elementData[--elementCount] = null; // Let gc do its work |
880 |
|
|
881 |
< |
return (E)oldValue; |
881 |
> |
// checkInvariants(); |
882 |
> |
return oldValue; |
883 |
|
} |
884 |
|
|
885 |
|
/** |
901 |
|
* @param c a collection whose elements will be tested for containment |
902 |
|
* in this Vector |
903 |
|
* @return true if this Vector contains all of the elements in the |
904 |
< |
* specified collection |
904 |
> |
* specified collection |
905 |
|
* @throws NullPointerException if the specified collection is null |
906 |
|
*/ |
907 |
|
public synchronized boolean containsAll(Collection<?> c) { |
917 |
|
* specified Collection is this Vector, and this Vector is nonempty.) |
918 |
|
* |
919 |
|
* @param c elements to be inserted into this Vector |
920 |
< |
* @return <tt>true</tt> if this Vector changed as a result of the call |
920 |
> |
* @return {@code true} if this Vector changed as a result of the call |
921 |
|
* @throws NullPointerException if the specified collection is null |
922 |
|
* @since 1.2 |
923 |
|
*/ |
924 |
< |
public synchronized boolean addAll(Collection<? extends E> c) { |
840 |
< |
modCount++; |
924 |
> |
public boolean addAll(Collection<? extends E> c) { |
925 |
|
Object[] a = c.toArray(); |
926 |
+ |
modCount++; |
927 |
|
int numNew = a.length; |
928 |
< |
ensureCapacityHelper(elementCount + numNew); |
929 |
< |
System.arraycopy(a, 0, elementData, elementCount, numNew); |
930 |
< |
elementCount += numNew; |
931 |
< |
return numNew != 0; |
928 |
> |
if (numNew == 0) |
929 |
> |
return false; |
930 |
> |
synchronized (this) { |
931 |
> |
Object[] elementData = this.elementData; |
932 |
> |
final int s = elementCount; |
933 |
> |
if (numNew > elementData.length - s) |
934 |
> |
elementData = grow(s + numNew); |
935 |
> |
System.arraycopy(a, 0, elementData, s, numNew); |
936 |
> |
elementCount = s + numNew; |
937 |
> |
// checkInvariants(); |
938 |
> |
return true; |
939 |
> |
} |
940 |
|
} |
941 |
|
|
942 |
|
/** |
947 |
|
* @return true if this Vector changed as a result of the call |
948 |
|
* @throws ClassCastException if the types of one or more elements |
949 |
|
* in this vector are incompatible with the specified |
950 |
< |
* collection (optional) |
950 |
> |
* collection |
951 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
952 |
|
* @throws NullPointerException if this vector contains one or more null |
953 |
|
* elements and the specified collection does not support null |
954 |
< |
* elements (optional), or if the specified collection is null |
954 |
> |
* elements |
955 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
956 |
> |
* or if the specified collection is null |
957 |
|
* @since 1.2 |
958 |
|
*/ |
959 |
< |
public synchronized boolean removeAll(Collection<?> c) { |
960 |
< |
return super.removeAll(c); |
959 |
> |
public boolean removeAll(Collection<?> c) { |
960 |
> |
Objects.requireNonNull(c); |
961 |
> |
return bulkRemove(e -> c.contains(e)); |
962 |
|
} |
963 |
|
|
964 |
|
/** |
971 |
|
* @return true if this Vector changed as a result of the call |
972 |
|
* @throws ClassCastException if the types of one or more elements |
973 |
|
* in this vector are incompatible with the specified |
974 |
< |
* collection (optional) |
974 |
> |
* collection |
975 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
976 |
|
* @throws NullPointerException if this vector contains one or more null |
977 |
|
* elements and the specified collection does not support null |
978 |
< |
* elements (optional), or if the specified collection is null |
978 |
> |
* elements |
979 |
> |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
980 |
> |
* or if the specified collection is null |
981 |
|
* @since 1.2 |
982 |
|
*/ |
983 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
984 |
< |
return super.retainAll(c); |
983 |
> |
public boolean retainAll(Collection<?> c) { |
984 |
> |
Objects.requireNonNull(c); |
985 |
> |
return bulkRemove(e -> !c.contains(e)); |
986 |
> |
} |
987 |
> |
|
988 |
> |
@Override |
989 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
990 |
> |
Objects.requireNonNull(filter); |
991 |
> |
return bulkRemove(filter); |
992 |
> |
} |
993 |
> |
|
994 |
> |
// A tiny bit set implementation |
995 |
> |
|
996 |
> |
private static long[] nBits(int n) { |
997 |
> |
return new long[((n - 1) >> 6) + 1]; |
998 |
> |
} |
999 |
> |
private static void setBit(long[] bits, int i) { |
1000 |
> |
bits[i >> 6] |= 1L << i; |
1001 |
> |
} |
1002 |
> |
private static boolean isClear(long[] bits, int i) { |
1003 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
1004 |
> |
} |
1005 |
> |
|
1006 |
> |
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
1007 |
> |
int expectedModCount = modCount; |
1008 |
> |
final Object[] es = elementData; |
1009 |
> |
final int end = elementCount; |
1010 |
> |
int i; |
1011 |
> |
// Optimize for initial run of survivors |
1012 |
> |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
1013 |
> |
; |
1014 |
> |
// Tolerate predicates that reentrantly access the collection for |
1015 |
> |
// read (but writers still get CME), so traverse once to find |
1016 |
> |
// elements to delete, a second pass to physically expunge. |
1017 |
> |
if (i < end) { |
1018 |
> |
final int beg = i; |
1019 |
> |
final long[] deathRow = nBits(end - beg); |
1020 |
> |
deathRow[0] = 1L; // set bit 0 |
1021 |
> |
for (i = beg + 1; i < end; i++) |
1022 |
> |
if (filter.test(elementAt(es, i))) |
1023 |
> |
setBit(deathRow, i - beg); |
1024 |
> |
if (modCount != expectedModCount) |
1025 |
> |
throw new ConcurrentModificationException(); |
1026 |
> |
expectedModCount++; |
1027 |
> |
modCount++; |
1028 |
> |
int w = beg; |
1029 |
> |
for (i = beg; i < end; i++) |
1030 |
> |
if (isClear(deathRow, i - beg)) |
1031 |
> |
es[w++] = es[i]; |
1032 |
> |
for (i = elementCount = w; i < end; i++) |
1033 |
> |
es[i] = null; |
1034 |
> |
// checkInvariants(); |
1035 |
> |
return true; |
1036 |
> |
} else { |
1037 |
> |
if (modCount != expectedModCount) |
1038 |
> |
throw new ConcurrentModificationException(); |
1039 |
> |
// checkInvariants(); |
1040 |
> |
return false; |
1041 |
> |
} |
1042 |
|
} |
1043 |
|
|
1044 |
|
/** |
1052 |
|
* @param index index at which to insert the first element from the |
1053 |
|
* specified collection |
1054 |
|
* @param c elements to be inserted into this Vector |
1055 |
< |
* @return <tt>true</tt> if this Vector changed as a result of the call |
1056 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
1057 |
< |
* < 0 || index > size()) |
1055 |
> |
* @return {@code true} if this Vector changed as a result of the call |
1056 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
1057 |
> |
* ({@code index < 0 || index > size()}) |
1058 |
|
* @throws NullPointerException if the specified collection is null |
1059 |
|
* @since 1.2 |
1060 |
|
*/ |
1061 |
|
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
1062 |
< |
modCount++; |
1063 |
< |
if (index < 0 || index > elementCount) |
907 |
< |
throw new ArrayIndexOutOfBoundsException(index); |
1062 |
> |
if (index < 0 || index > elementCount) |
1063 |
> |
throw new ArrayIndexOutOfBoundsException(index); |
1064 |
|
|
1065 |
|
Object[] a = c.toArray(); |
1066 |
< |
int numNew = a.length; |
1067 |
< |
ensureCapacityHelper(elementCount + numNew); |
1068 |
< |
|
1069 |
< |
int numMoved = elementCount - index; |
1070 |
< |
if (numMoved > 0) |
1071 |
< |
System.arraycopy(elementData, index, elementData, index + numNew, |
1072 |
< |
numMoved); |
1073 |
< |
|
1066 |
> |
modCount++; |
1067 |
> |
int numNew = a.length; |
1068 |
> |
if (numNew == 0) |
1069 |
> |
return false; |
1070 |
> |
Object[] elementData = this.elementData; |
1071 |
> |
final int s = elementCount; |
1072 |
> |
if (numNew > elementData.length - s) |
1073 |
> |
elementData = grow(s + numNew); |
1074 |
> |
|
1075 |
> |
int numMoved = s - index; |
1076 |
> |
if (numMoved > 0) |
1077 |
> |
System.arraycopy(elementData, index, |
1078 |
> |
elementData, index + numNew, |
1079 |
> |
numMoved); |
1080 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
1081 |
< |
elementCount += numNew; |
1082 |
< |
return numNew != 0; |
1081 |
> |
elementCount = s + numNew; |
1082 |
> |
// checkInvariants(); |
1083 |
> |
return true; |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1087 |
|
* Compares the specified Object with this Vector for equality. Returns |
1088 |
|
* true if and only if the specified Object is also a List, both Lists |
1089 |
|
* have the same size, and all corresponding pairs of elements in the two |
1090 |
< |
* Lists are <em>equal</em>. (Two elements <code>e1</code> and |
1091 |
< |
* <code>e2</code> are <em>equal</em> if <code>(e1==null ? e2==null : |
1092 |
< |
* e1.equals(e2))</code>.) In other words, two Lists are defined to be |
1090 |
> |
* Lists are <em>equal</em>. (Two elements {@code e1} and |
1091 |
> |
* {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) |
1092 |
> |
* In other words, two Lists are defined to be |
1093 |
|
* equal if they contain the same elements in the same order. |
1094 |
|
* |
1095 |
|
* @param o the Object to be compared for equality with this Vector |
1115 |
|
} |
1116 |
|
|
1117 |
|
/** |
1118 |
< |
* Removes from this List all of the elements whose index is between |
1119 |
< |
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
1120 |
< |
* elements to the left (reduces their index). |
1121 |
< |
* This call shortens the Vector by (toIndex - fromIndex) elements. (If |
1122 |
< |
* toIndex==fromIndex, this operation has no effect.) |
1118 |
> |
* Returns a view of the portion of this List between fromIndex, |
1119 |
> |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1120 |
> |
* equal, the returned List is empty.) The returned List is backed by this |
1121 |
> |
* List, so changes in the returned List are reflected in this List, and |
1122 |
> |
* vice-versa. The returned List supports all of the optional List |
1123 |
> |
* operations supported by this List. |
1124 |
> |
* |
1125 |
> |
* <p>This method eliminates the need for explicit range operations (of |
1126 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
1127 |
> |
* a List can be used as a range operation by operating on a subList view |
1128 |
> |
* instead of a whole List. For example, the following idiom |
1129 |
> |
* removes a range of elements from a List: |
1130 |
> |
* <pre> |
1131 |
> |
* list.subList(from, to).clear(); |
1132 |
> |
* </pre> |
1133 |
> |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1134 |
> |
* and all of the algorithms in the Collections class can be applied to |
1135 |
> |
* a subList. |
1136 |
|
* |
1137 |
< |
* @param fromIndex index of first element to be removed |
1138 |
< |
* @param toIndex index after last element to be removed |
1137 |
> |
* <p>The semantics of the List returned by this method become undefined if |
1138 |
> |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
1139 |
> |
* any way other than via the returned List. (Structural modifications are |
1140 |
> |
* those that change the size of the List, or otherwise perturb it in such |
1141 |
> |
* a fashion that iterations in progress may yield incorrect results.) |
1142 |
> |
* |
1143 |
> |
* @param fromIndex low endpoint (inclusive) of the subList |
1144 |
> |
* @param toIndex high endpoint (exclusive) of the subList |
1145 |
> |
* @return a view of the specified range within this List |
1146 |
> |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1147 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
1148 |
> |
* @throws IllegalArgumentException if the endpoint indices are out of order |
1149 |
> |
* {@code (fromIndex > toIndex)} |
1150 |
> |
*/ |
1151 |
> |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1152 |
> |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1153 |
> |
this); |
1154 |
> |
} |
1155 |
> |
|
1156 |
> |
/** |
1157 |
> |
* Removes from this list all of the elements whose index is between |
1158 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1159 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1160 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1161 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1162 |
|
*/ |
1163 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1164 |
< |
modCount++; |
1165 |
< |
int numMoved = elementCount - toIndex; |
1166 |
< |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
1167 |
< |
numMoved); |
1164 |
> |
modCount++; |
1165 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1166 |
> |
// checkInvariants(); |
1167 |
> |
} |
1168 |
|
|
1169 |
< |
// Let gc do its work |
1170 |
< |
int newElementCount = elementCount - (toIndex-fromIndex); |
1171 |
< |
while (elementCount != newElementCount) |
1172 |
< |
elementData[--elementCount] = null; |
1169 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1170 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1171 |
> |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1172 |
> |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1173 |
> |
es[i] = null; |
1174 |
|
} |
1175 |
|
|
1176 |
|
/** |
1177 |
< |
* Save the state of the <tt>Vector</tt> instance to a stream (that |
1178 |
< |
* is, serialize it). This method is present merely for synchronization. |
1179 |
< |
* It just calls the default writeObject method. |
1177 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1178 |
> |
* (that is, serializes it). |
1179 |
> |
* This method performs synchronization to ensure the consistency |
1180 |
> |
* of the serialized data. |
1181 |
> |
* |
1182 |
> |
* @param s the stream |
1183 |
> |
* @throws java.io.IOException if an I/O error occurs |
1184 |
|
*/ |
1185 |
< |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
1186 |
< |
throws java.io.IOException |
1187 |
< |
{ |
1188 |
< |
s.defaultWriteObject(); |
1185 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
1186 |
> |
throws java.io.IOException { |
1187 |
> |
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1188 |
> |
final Object[] data; |
1189 |
> |
synchronized (this) { |
1190 |
> |
fields.put("capacityIncrement", capacityIncrement); |
1191 |
> |
fields.put("elementCount", elementCount); |
1192 |
> |
data = elementData.clone(); |
1193 |
> |
} |
1194 |
> |
fields.put("elementData", data); |
1195 |
> |
s.writeFields(); |
1196 |
|
} |
1197 |
|
|
1198 |
|
/** |
1199 |
< |
* Returns a list-iterator of the elements in this list (in proper |
1199 |
> |
* Returns a list iterator over the elements in this list (in proper |
1200 |
|
* sequence), starting at the specified position in the list. |
1201 |
< |
* Obeys the general contract of {@link List#listIterator(int)}. |
1201 |
> |
* The specified index indicates the first element that would be |
1202 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1203 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1204 |
> |
* return the element with the specified index minus one. |
1205 |
> |
* |
1206 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1207 |
|
* |
992 |
– |
* <p>The list-iterator is <i>fail-fast</i>: if the list is structurally |
993 |
– |
* modified at any time after the Iterator is created, in any way except |
994 |
– |
* through the list-iterator's own {@code remove} or {@code add} |
995 |
– |
* methods, the list-iterator will throw a |
996 |
– |
* {@code ConcurrentModificationException}. Thus, in the face of |
997 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
998 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
999 |
– |
* time in the future. |
1000 |
– |
* |
1001 |
– |
* @param index index of the first element to be returned from the |
1002 |
– |
* list-iterator (by a call to {@link ListIterator#next}) |
1003 |
– |
* @return a list-iterator of the elements in this list (in proper |
1004 |
– |
* sequence), starting at the specified position in the list |
1208 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1209 |
|
*/ |
1210 |
|
public synchronized ListIterator<E> listIterator(int index) { |
1211 |
< |
if (index < 0 || index > elementCount) |
1211 |
> |
if (index < 0 || index > elementCount) |
1212 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
1213 |
< |
return new VectorIterator(index, elementCount); |
1213 |
> |
return new ListItr(index); |
1214 |
|
} |
1215 |
|
|
1216 |
|
/** |
1217 |
< |
* {@inheritDoc} |
1217 |
> |
* Returns a list iterator over the elements in this list (in proper |
1218 |
> |
* sequence). |
1219 |
> |
* |
1220 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1221 |
> |
* |
1222 |
> |
* @see #listIterator(int) |
1223 |
|
*/ |
1224 |
|
public synchronized ListIterator<E> listIterator() { |
1225 |
< |
return new VectorIterator(0, elementCount); |
1225 |
> |
return new ListItr(0); |
1226 |
|
} |
1227 |
|
|
1228 |
|
/** |
1229 |
|
* Returns an iterator over the elements in this list in proper sequence. |
1230 |
|
* |
1231 |
+ |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1232 |
+ |
* |
1233 |
|
* @return an iterator over the elements in this list in proper sequence |
1234 |
|
*/ |
1235 |
|
public synchronized Iterator<E> iterator() { |
1236 |
< |
return new VectorIterator(0, elementCount); |
1236 |
> |
return new Itr(); |
1237 |
|
} |
1238 |
|
|
1239 |
|
/** |
1240 |
< |
* Helper method to access array elements under synchronization by |
1031 |
< |
* iterators. The caller performs index check with respect to |
1032 |
< |
* expected bounds, so errors accessing the element are reported |
1033 |
< |
* as ConcurrentModificationExceptions. |
1240 |
> |
* An optimized version of AbstractList.Itr |
1241 |
|
*/ |
1242 |
< |
final synchronized Object iteratorGet(int index, int expectedModCount) { |
1243 |
< |
if (modCount == expectedModCount) { |
1244 |
< |
try { |
1245 |
< |
return elementData[index]; |
1039 |
< |
} catch(IndexOutOfBoundsException fallThrough) { |
1040 |
< |
} |
1041 |
< |
} |
1042 |
< |
throw new ConcurrentModificationException(); |
1043 |
< |
} |
1044 |
< |
|
1045 |
< |
/** |
1046 |
< |
* Streamlined specialization of AbstractList version of iterator. |
1047 |
< |
* Locally perfroms bounds checks, but relies on outer Vector |
1048 |
< |
* to access elements under synchronization. |
1049 |
< |
*/ |
1050 |
< |
private final class VectorIterator implements ListIterator<E> { |
1051 |
< |
int cursor; // Index of next element to return; |
1052 |
< |
int fence; // Upper bound on cursor (cache of size()) |
1053 |
< |
int lastRet; // Index of last element, or -1 if no such |
1054 |
< |
int expectedModCount; // To check for CME |
1055 |
< |
|
1056 |
< |
VectorIterator(int index, int fence) { |
1057 |
< |
this.cursor = index; |
1058 |
< |
this.fence = fence; |
1059 |
< |
this.lastRet = -1; |
1060 |
< |
this.expectedModCount = Vector.this.modCount; |
1061 |
< |
} |
1062 |
< |
|
1063 |
< |
public boolean hasNext() { |
1064 |
< |
return cursor < fence; |
1065 |
< |
} |
1066 |
< |
|
1067 |
< |
public boolean hasPrevious() { |
1068 |
< |
return cursor > 0; |
1069 |
< |
} |
1070 |
< |
|
1071 |
< |
public int nextIndex() { |
1072 |
< |
return cursor; |
1073 |
< |
} |
1074 |
< |
|
1075 |
< |
public int previousIndex() { |
1076 |
< |
return cursor - 1; |
1077 |
< |
} |
1078 |
< |
|
1079 |
< |
public E next() { |
1080 |
< |
int i = cursor; |
1081 |
< |
if (i >= fence) |
1082 |
< |
throw new NoSuchElementException(); |
1083 |
< |
Object next = Vector.this.iteratorGet(i, expectedModCount); |
1084 |
< |
lastRet = i; |
1085 |
< |
cursor = i + 1; |
1086 |
< |
return (E)next; |
1087 |
< |
} |
1242 |
> |
private class Itr implements Iterator<E> { |
1243 |
> |
int cursor; // index of next element to return |
1244 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1245 |
> |
int expectedModCount = modCount; |
1246 |
|
|
1247 |
< |
public E previous() { |
1248 |
< |
int i = cursor - 1; |
1249 |
< |
if (i < 0) |
1250 |
< |
throw new NoSuchElementException(); |
1093 |
< |
Object prev = Vector.this.iteratorGet(i, expectedModCount); |
1094 |
< |
lastRet = i; |
1095 |
< |
cursor = i; |
1096 |
< |
return (E)prev; |
1247 |
> |
public boolean hasNext() { |
1248 |
> |
// Racy but within spec, since modifications are checked |
1249 |
> |
// within or after synchronization in next/previous |
1250 |
> |
return cursor != elementCount; |
1251 |
|
} |
1252 |
|
|
1253 |
< |
public void set(E e) { |
1254 |
< |
if (lastRet < 0) |
1255 |
< |
throw new IllegalStateException(); |
1102 |
< |
if (Vector.this.modCount != expectedModCount) |
1103 |
< |
throw new ConcurrentModificationException(); |
1104 |
< |
try { |
1105 |
< |
Vector.this.set(lastRet, e); |
1106 |
< |
expectedModCount = Vector.this.modCount; |
1107 |
< |
} catch (IndexOutOfBoundsException ex) { |
1108 |
< |
throw new ConcurrentModificationException(); |
1109 |
< |
} |
1110 |
< |
} |
1111 |
< |
|
1112 |
< |
public void remove() { |
1113 |
< |
int i = lastRet; |
1114 |
< |
if (i < 0) |
1115 |
< |
throw new IllegalStateException(); |
1116 |
< |
if (Vector.this.modCount != expectedModCount) |
1117 |
< |
throw new ConcurrentModificationException(); |
1118 |
< |
try { |
1119 |
< |
Vector.this.remove(i); |
1120 |
< |
if (i < cursor) |
1121 |
< |
cursor--; |
1122 |
< |
lastRet = -1; |
1123 |
< |
fence = Vector.this.size(); |
1124 |
< |
expectedModCount = Vector.this.modCount; |
1125 |
< |
} catch (IndexOutOfBoundsException ex) { |
1126 |
< |
throw new ConcurrentModificationException(); |
1127 |
< |
} |
1128 |
< |
} |
1129 |
< |
|
1130 |
< |
public void add(E e) { |
1131 |
< |
if (Vector.this.modCount != expectedModCount) |
1132 |
< |
throw new ConcurrentModificationException(); |
1133 |
< |
try { |
1253 |
> |
public E next() { |
1254 |
> |
synchronized (Vector.this) { |
1255 |
> |
checkForComodification(); |
1256 |
|
int i = cursor; |
1257 |
< |
Vector.this.add(i, e); |
1257 |
> |
if (i >= elementCount) |
1258 |
> |
throw new NoSuchElementException(); |
1259 |
|
cursor = i + 1; |
1260 |
< |
lastRet = -1; |
1138 |
< |
fence = Vector.this.size(); |
1139 |
< |
expectedModCount = Vector.this.modCount; |
1140 |
< |
} catch (IndexOutOfBoundsException ex) { |
1141 |
< |
throw new ConcurrentModificationException(); |
1142 |
< |
} |
1143 |
< |
} |
1144 |
< |
} |
1145 |
< |
|
1146 |
< |
/** |
1147 |
< |
* Returns a view of the portion of this List between fromIndex, |
1148 |
< |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
1149 |
< |
* equal, the returned List is empty.) The returned List is backed by this |
1150 |
< |
* List, so changes in the returned List are reflected in this List, and |
1151 |
< |
* vice-versa. The returned List supports all of the optional List |
1152 |
< |
* operations supported by this List.<p> |
1153 |
< |
* |
1154 |
< |
* This method eliminates the need for explicit range operations (of |
1155 |
< |
* the sort that commonly exist for arrays). Any operation that expects |
1156 |
< |
* a List can be used as a range operation by operating on a subList view |
1157 |
< |
* instead of a whole List. For example, the following idiom |
1158 |
< |
* removes a range of elements from a List: |
1159 |
< |
* <pre> |
1160 |
< |
* list.subList(from, to).clear(); |
1161 |
< |
* </pre> |
1162 |
< |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
1163 |
< |
* and all of the algorithms in the Collections class can be applied to |
1164 |
< |
* a subList.<p> |
1165 |
< |
* |
1166 |
< |
* The semantics of the List returned by this method become undefined if |
1167 |
< |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
1168 |
< |
* any way other than via the returned List. (Structural modifications are |
1169 |
< |
* those that change the size of the List, or otherwise perturb it in such |
1170 |
< |
* a fashion that iterations in progress may yield incorrect results.) |
1171 |
< |
* |
1172 |
< |
* @param fromIndex low endpoint (inclusive) of the subList |
1173 |
< |
* @param toIndex high endpoint (exclusive) of the subList |
1174 |
< |
* @return a view of the specified range within this List |
1175 |
< |
* @throws IndexOutOfBoundsException endpoint index value out of range |
1176 |
< |
* <code>(fromIndex < 0 || toIndex > size)</code> |
1177 |
< |
* @throws IllegalArgumentException endpoint indices out of order |
1178 |
< |
* <code>(fromIndex > toIndex)</code> |
1179 |
< |
*/ |
1180 |
< |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1181 |
< |
return new VectorSubList(this, this, fromIndex, fromIndex, toIndex); |
1182 |
< |
} |
1183 |
< |
|
1184 |
< |
/** |
1185 |
< |
* This class specializes the AbstractList version of SubList to |
1186 |
< |
* avoid the double-indirection penalty that would arise using a |
1187 |
< |
* synchronized wrapper, as well as to avoid some unnecessary |
1188 |
< |
* checks in sublist iterators. |
1189 |
< |
*/ |
1190 |
< |
private static final class VectorSubList<E> extends AbstractList<E> implements RandomAccess { |
1191 |
< |
final Vector<E> base; // base list |
1192 |
< |
final AbstractList<E> parent; // Creating list |
1193 |
< |
final int baseOffset; // index wrt Vector |
1194 |
< |
final int parentOffset; // index wrt parent |
1195 |
< |
int length; // length of sublist |
1196 |
< |
|
1197 |
< |
VectorSubList(Vector<E> base, AbstractList<E> parent, int baseOffset, |
1198 |
< |
int fromIndex, int toIndex) { |
1199 |
< |
if (fromIndex < 0) |
1200 |
< |
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); |
1201 |
< |
if (toIndex > parent.size()) |
1202 |
< |
throw new IndexOutOfBoundsException("toIndex = " + toIndex); |
1203 |
< |
if (fromIndex > toIndex) |
1204 |
< |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
1205 |
< |
") > toIndex(" + toIndex + ")"); |
1206 |
< |
|
1207 |
< |
this.base = base; |
1208 |
< |
this.parent = parent; |
1209 |
< |
this.baseOffset = baseOffset; |
1210 |
< |
this.parentOffset = fromIndex; |
1211 |
< |
this.length = toIndex - fromIndex; |
1212 |
< |
modCount = base.modCount; |
1213 |
< |
} |
1214 |
< |
|
1215 |
< |
/** |
1216 |
< |
* Returns an IndexOutOfBoundsException with nicer message |
1217 |
< |
*/ |
1218 |
< |
private IndexOutOfBoundsException indexError(int index) { |
1219 |
< |
return new IndexOutOfBoundsException("Index: " + index + |
1220 |
< |
", Size: " + length); |
1221 |
< |
} |
1222 |
< |
|
1223 |
< |
public E set(int index, E element) { |
1224 |
< |
synchronized(base) { |
1225 |
< |
if (index < 0 || index >= length) |
1226 |
< |
throw indexError(index); |
1227 |
< |
if (base.modCount != modCount) |
1228 |
< |
throw new ConcurrentModificationException(); |
1229 |
< |
return base.set(index + baseOffset, element); |
1230 |
< |
} |
1231 |
< |
} |
1232 |
< |
|
1233 |
< |
public E get(int index) { |
1234 |
< |
synchronized(base) { |
1235 |
< |
if (index < 0 || index >= length) |
1236 |
< |
throw indexError(index); |
1237 |
< |
if (base.modCount != modCount) |
1238 |
< |
throw new ConcurrentModificationException(); |
1239 |
< |
return base.get(index + baseOffset); |
1240 |
< |
} |
1241 |
< |
} |
1242 |
< |
|
1243 |
< |
public int size() { |
1244 |
< |
synchronized(base) { |
1245 |
< |
if (base.modCount != modCount) |
1246 |
< |
throw new ConcurrentModificationException(); |
1247 |
< |
return length; |
1248 |
< |
} |
1249 |
< |
} |
1250 |
< |
|
1251 |
< |
public void add(int index, E element) { |
1252 |
< |
synchronized(base) { |
1253 |
< |
if (index < 0 || index > length) |
1254 |
< |
throw indexError(index); |
1255 |
< |
if (base.modCount != modCount) |
1256 |
< |
throw new ConcurrentModificationException(); |
1257 |
< |
parent.add(index + parentOffset, element); |
1258 |
< |
length++; |
1259 |
< |
modCount = base.modCount; |
1260 |
< |
} |
1261 |
< |
} |
1262 |
< |
|
1263 |
< |
public E remove(int index) { |
1264 |
< |
synchronized(base) { |
1265 |
< |
if (index < 0 || index >= length) |
1266 |
< |
throw indexError(index); |
1267 |
< |
if (base.modCount != modCount) |
1268 |
< |
throw new ConcurrentModificationException(); |
1269 |
< |
E result = parent.remove(index + parentOffset); |
1270 |
< |
length--; |
1271 |
< |
modCount = base.modCount; |
1272 |
< |
return result; |
1260 |
> |
return elementData(lastRet = i); |
1261 |
|
} |
1262 |
|
} |
1263 |
|
|
1264 |
< |
protected void removeRange(int fromIndex, int toIndex) { |
1265 |
< |
synchronized(base) { |
1266 |
< |
if (base.modCount != modCount) |
1267 |
< |
throw new ConcurrentModificationException(); |
1268 |
< |
parent.removeRange(fromIndex + parentOffset, |
1269 |
< |
toIndex + parentOffset); |
1270 |
< |
length -= (toIndex-fromIndex); |
1283 |
< |
modCount = base.modCount; |
1264 |
> |
public void remove() { |
1265 |
> |
if (lastRet == -1) |
1266 |
> |
throw new IllegalStateException(); |
1267 |
> |
synchronized (Vector.this) { |
1268 |
> |
checkForComodification(); |
1269 |
> |
Vector.this.remove(lastRet); |
1270 |
> |
expectedModCount = modCount; |
1271 |
|
} |
1272 |
+ |
cursor = lastRet; |
1273 |
+ |
lastRet = -1; |
1274 |
|
} |
1275 |
|
|
1276 |
< |
public boolean addAll(Collection<? extends E> c) { |
1277 |
< |
return addAll(length, c); |
1278 |
< |
} |
1279 |
< |
|
1280 |
< |
public boolean addAll(int index, Collection<? extends E> c) { |
1281 |
< |
synchronized(base) { |
1282 |
< |
if (index < 0 || index > length) |
1283 |
< |
throw indexError(index); |
1284 |
< |
int cSize = c.size(); |
1285 |
< |
if (cSize==0) |
1286 |
< |
return false; |
1298 |
< |
|
1299 |
< |
if (base.modCount != modCount) |
1276 |
> |
@Override |
1277 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1278 |
> |
Objects.requireNonNull(action); |
1279 |
> |
synchronized (Vector.this) { |
1280 |
> |
final int size = elementCount; |
1281 |
> |
int i = cursor; |
1282 |
> |
if (i >= size) { |
1283 |
> |
return; |
1284 |
> |
} |
1285 |
> |
final Object[] es = elementData; |
1286 |
> |
if (i >= es.length) |
1287 |
|
throw new ConcurrentModificationException(); |
1288 |
< |
parent.addAll(parentOffset + index, c); |
1289 |
< |
modCount = base.modCount; |
1290 |
< |
length += cSize; |
1291 |
< |
return true; |
1288 |
> |
while (i < size && modCount == expectedModCount) |
1289 |
> |
action.accept(elementAt(es, i++)); |
1290 |
> |
// update once at end of iteration to reduce heap write traffic |
1291 |
> |
cursor = i; |
1292 |
> |
lastRet = i - 1; |
1293 |
> |
checkForComodification(); |
1294 |
|
} |
1295 |
|
} |
1296 |
|
|
1297 |
< |
public boolean equals(Object o) { |
1298 |
< |
synchronized(base) {return super.equals(o);} |
1297 |
> |
final void checkForComodification() { |
1298 |
> |
if (modCount != expectedModCount) |
1299 |
> |
throw new ConcurrentModificationException(); |
1300 |
|
} |
1301 |
+ |
} |
1302 |
|
|
1303 |
< |
public int hashCode() { |
1304 |
< |
synchronized(base) {return super.hashCode();} |
1303 |
> |
/** |
1304 |
> |
* An optimized version of AbstractList.ListItr |
1305 |
> |
*/ |
1306 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1307 |
> |
ListItr(int index) { |
1308 |
> |
super(); |
1309 |
> |
cursor = index; |
1310 |
|
} |
1311 |
|
|
1312 |
< |
public int indexOf(Object o) { |
1313 |
< |
synchronized(base) {return super.indexOf(o);} |
1312 |
> |
public boolean hasPrevious() { |
1313 |
> |
return cursor != 0; |
1314 |
|
} |
1315 |
|
|
1316 |
< |
public int lastIndexOf(Object o) { |
1317 |
< |
synchronized(base) {return super.lastIndexOf(o);} |
1316 |
> |
public int nextIndex() { |
1317 |
> |
return cursor; |
1318 |
|
} |
1319 |
|
|
1320 |
< |
public List<E> subList(int fromIndex, int toIndex) { |
1321 |
< |
return new VectorSubList(base, this, fromIndex + baseOffset, |
1326 |
< |
fromIndex, toIndex); |
1320 |
> |
public int previousIndex() { |
1321 |
> |
return cursor - 1; |
1322 |
|
} |
1323 |
|
|
1324 |
< |
public Iterator<E> iterator() { |
1325 |
< |
synchronized(base) { |
1326 |
< |
return new VectorSubListIterator(this, 0); |
1324 |
> |
public E previous() { |
1325 |
> |
synchronized (Vector.this) { |
1326 |
> |
checkForComodification(); |
1327 |
> |
int i = cursor - 1; |
1328 |
> |
if (i < 0) |
1329 |
> |
throw new NoSuchElementException(); |
1330 |
> |
cursor = i; |
1331 |
> |
return elementData(lastRet = i); |
1332 |
|
} |
1333 |
|
} |
1334 |
|
|
1335 |
< |
public synchronized ListIterator<E> listIterator() { |
1336 |
< |
synchronized(base) { |
1337 |
< |
return new VectorSubListIterator(this, 0); |
1335 |
> |
public void set(E e) { |
1336 |
> |
if (lastRet == -1) |
1337 |
> |
throw new IllegalStateException(); |
1338 |
> |
synchronized (Vector.this) { |
1339 |
> |
checkForComodification(); |
1340 |
> |
Vector.this.set(lastRet, e); |
1341 |
|
} |
1342 |
|
} |
1343 |
|
|
1344 |
< |
public ListIterator<E> listIterator(int index) { |
1345 |
< |
synchronized(base) { |
1346 |
< |
if (index < 0 || index > length) |
1347 |
< |
throw indexError(index); |
1348 |
< |
return new VectorSubListIterator(this, index); |
1344 |
> |
public void add(E e) { |
1345 |
> |
int i = cursor; |
1346 |
> |
synchronized (Vector.this) { |
1347 |
> |
checkForComodification(); |
1348 |
> |
Vector.this.add(i, e); |
1349 |
> |
expectedModCount = modCount; |
1350 |
|
} |
1351 |
+ |
cursor = i + 1; |
1352 |
+ |
lastRet = -1; |
1353 |
|
} |
1354 |
+ |
} |
1355 |
|
|
1356 |
< |
/** |
1357 |
< |
* Same idea as VectorIterator, except routing structural |
1358 |
< |
* change operations through the sublist. |
1359 |
< |
*/ |
1360 |
< |
private static final class VectorSubListIterator<E> implements ListIterator<E> { |
1361 |
< |
final Vector<E> base; // base list |
1362 |
< |
final VectorSubList<E> outer; // Sublist creating this iteraor |
1363 |
< |
final int offset; // cursor offset wrt base |
1364 |
< |
int cursor; // Current index |
1365 |
< |
int fence; // Upper bound on cursor |
1366 |
< |
int lastRet; // Index of returned element, or -1 |
1367 |
< |
int expectedModCount; // Expected modCount of base Vector |
1368 |
< |
|
1369 |
< |
VectorSubListIterator(VectorSubList<E> list, int index) { |
1370 |
< |
this.lastRet = -1; |
1371 |
< |
this.cursor = index; |
1372 |
< |
this.outer = list; |
1373 |
< |
this.offset = list.baseOffset; |
1374 |
< |
this.fence = list.length; |
1375 |
< |
this.base = list.base; |
1376 |
< |
this.expectedModCount = base.modCount; |
1377 |
< |
} |
1378 |
< |
|
1379 |
< |
public boolean hasNext() { |
1380 |
< |
return cursor < fence; |
1381 |
< |
} |
1356 |
> |
@Override |
1357 |
> |
public synchronized void forEach(Consumer<? super E> action) { |
1358 |
> |
Objects.requireNonNull(action); |
1359 |
> |
final int expectedModCount = modCount; |
1360 |
> |
final Object[] es = elementData; |
1361 |
> |
final int size = elementCount; |
1362 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1363 |
> |
action.accept(elementAt(es, i)); |
1364 |
> |
if (modCount != expectedModCount) |
1365 |
> |
throw new ConcurrentModificationException(); |
1366 |
> |
// checkInvariants(); |
1367 |
> |
} |
1368 |
> |
|
1369 |
> |
@Override |
1370 |
> |
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1371 |
> |
Objects.requireNonNull(operator); |
1372 |
> |
final int expectedModCount = modCount; |
1373 |
> |
final Object[] es = elementData; |
1374 |
> |
final int size = elementCount; |
1375 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1376 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1377 |
> |
if (modCount != expectedModCount) |
1378 |
> |
throw new ConcurrentModificationException(); |
1379 |
> |
modCount++; |
1380 |
> |
// checkInvariants(); |
1381 |
> |
} |
1382 |
|
|
1383 |
< |
public boolean hasPrevious() { |
1384 |
< |
return cursor > 0; |
1385 |
< |
} |
1383 |
> |
@SuppressWarnings("unchecked") |
1384 |
> |
@Override |
1385 |
> |
public synchronized void sort(Comparator<? super E> c) { |
1386 |
> |
final int expectedModCount = modCount; |
1387 |
> |
Arrays.sort((E[]) elementData, 0, elementCount, c); |
1388 |
> |
if (modCount != expectedModCount) |
1389 |
> |
throw new ConcurrentModificationException(); |
1390 |
> |
modCount++; |
1391 |
> |
// checkInvariants(); |
1392 |
> |
} |
1393 |
|
|
1394 |
< |
public int nextIndex() { |
1395 |
< |
return cursor; |
1396 |
< |
} |
1394 |
> |
/** |
1395 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
1396 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
1397 |
> |
* list. |
1398 |
> |
* |
1399 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
1400 |
> |
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
1401 |
> |
* Overriding implementations should document the reporting of additional |
1402 |
> |
* characteristic values. |
1403 |
> |
* |
1404 |
> |
* @return a {@code Spliterator} over the elements in this list |
1405 |
> |
* @since 1.8 |
1406 |
> |
*/ |
1407 |
> |
@Override |
1408 |
> |
public Spliterator<E> spliterator() { |
1409 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1410 |
> |
} |
1411 |
|
|
1412 |
< |
public int previousIndex() { |
1413 |
< |
return cursor - 1; |
1414 |
< |
} |
1412 |
> |
/** Similar to ArrayList Spliterator */ |
1413 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1414 |
> |
private Object[] array; |
1415 |
> |
private int index; // current index, modified on advance/split |
1416 |
> |
private int fence; // -1 until used; then one past last index |
1417 |
> |
private int expectedModCount; // initialized when fence set |
1418 |
|
|
1419 |
< |
public E next() { |
1420 |
< |
int i = cursor; |
1421 |
< |
if (cursor >= fence) |
1422 |
< |
throw new NoSuchElementException(); |
1423 |
< |
Object next = base.iteratorGet(i + offset, expectedModCount); |
1424 |
< |
lastRet = i; |
1425 |
< |
cursor = i + 1; |
1426 |
< |
return (E)next; |
1396 |
< |
} |
1397 |
< |
|
1398 |
< |
public E previous() { |
1399 |
< |
int i = cursor - 1; |
1400 |
< |
if (i < 0) |
1401 |
< |
throw new NoSuchElementException(); |
1402 |
< |
Object prev = base.iteratorGet(i + offset, expectedModCount); |
1403 |
< |
lastRet = i; |
1404 |
< |
cursor = i; |
1405 |
< |
return (E)prev; |
1406 |
< |
} |
1419 |
> |
/** Create new spliterator covering the given range */ |
1420 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1421 |
> |
int expectedModCount) { |
1422 |
> |
this.array = array; |
1423 |
> |
this.index = origin; |
1424 |
> |
this.fence = fence; |
1425 |
> |
this.expectedModCount = expectedModCount; |
1426 |
> |
} |
1427 |
|
|
1428 |
< |
public void set(E e) { |
1429 |
< |
if (lastRet < 0) |
1430 |
< |
throw new IllegalStateException(); |
1431 |
< |
if (base.modCount != expectedModCount) |
1432 |
< |
throw new ConcurrentModificationException(); |
1433 |
< |
try { |
1434 |
< |
outer.set(lastRet, e); |
1415 |
< |
expectedModCount = base.modCount; |
1416 |
< |
} catch (IndexOutOfBoundsException ex) { |
1417 |
< |
throw new ConcurrentModificationException(); |
1428 |
> |
private int getFence() { // initialize on first use |
1429 |
> |
int hi; |
1430 |
> |
if ((hi = fence) < 0) { |
1431 |
> |
synchronized (Vector.this) { |
1432 |
> |
array = elementData; |
1433 |
> |
expectedModCount = modCount; |
1434 |
> |
hi = fence = elementCount; |
1435 |
|
} |
1436 |
|
} |
1437 |
+ |
return hi; |
1438 |
+ |
} |
1439 |
|
|
1440 |
< |
public void remove() { |
1441 |
< |
int i = lastRet; |
1442 |
< |
if (i < 0) |
1443 |
< |
throw new IllegalStateException(); |
1444 |
< |
if (base.modCount != expectedModCount) |
1445 |
< |
throw new ConcurrentModificationException(); |
1446 |
< |
try { |
1447 |
< |
outer.remove(i); |
1448 |
< |
if (i < cursor) |
1449 |
< |
cursor--; |
1450 |
< |
lastRet = -1; |
1451 |
< |
fence = outer.length; |
1452 |
< |
expectedModCount = base.modCount; |
1453 |
< |
} catch (IndexOutOfBoundsException ex) { |
1440 |
> |
public Spliterator<E> trySplit() { |
1441 |
> |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1442 |
> |
return (lo >= mid) ? null : |
1443 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1444 |
> |
} |
1445 |
> |
|
1446 |
> |
@SuppressWarnings("unchecked") |
1447 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
1448 |
> |
int i; |
1449 |
> |
if (action == null) |
1450 |
> |
throw new NullPointerException(); |
1451 |
> |
if (getFence() > (i = index)) { |
1452 |
> |
index = i + 1; |
1453 |
> |
action.accept((E)array[i]); |
1454 |
> |
if (modCount != expectedModCount) |
1455 |
|
throw new ConcurrentModificationException(); |
1456 |
< |
} |
1456 |
> |
return true; |
1457 |
|
} |
1458 |
+ |
return false; |
1459 |
+ |
} |
1460 |
|
|
1461 |
< |
public void add(E e) { |
1462 |
< |
if (base.modCount != expectedModCount) |
1463 |
< |
throw new ConcurrentModificationException(); |
1464 |
< |
try { |
1465 |
< |
int i = cursor; |
1466 |
< |
outer.add(i, e); |
1467 |
< |
cursor = i + 1; |
1468 |
< |
lastRet = -1; |
1469 |
< |
fence = outer.length; |
1470 |
< |
expectedModCount = base.modCount; |
1471 |
< |
} catch (IndexOutOfBoundsException ex) { |
1450 |
< |
throw new ConcurrentModificationException(); |
1451 |
< |
} |
1452 |
< |
} |
1461 |
> |
@SuppressWarnings("unchecked") |
1462 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1463 |
> |
if (action == null) |
1464 |
> |
throw new NullPointerException(); |
1465 |
> |
final int hi = getFence(); |
1466 |
> |
final Object[] a = array; |
1467 |
> |
int i; |
1468 |
> |
for (i = index, index = hi; i < hi; i++) |
1469 |
> |
action.accept((E) a[i]); |
1470 |
> |
if (modCount != expectedModCount) |
1471 |
> |
throw new ConcurrentModificationException(); |
1472 |
|
} |
1454 |
– |
} |
1455 |
– |
} |
1473 |
|
|
1474 |
+ |
public long estimateSize() { |
1475 |
+ |
return getFence() - index; |
1476 |
+ |
} |
1477 |
|
|
1478 |
+ |
public int characteristics() { |
1479 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1480 |
+ |
} |
1481 |
+ |
} |
1482 |
|
|
1483 |
+ |
void checkInvariants() { |
1484 |
+ |
// assert elementCount >= 0; |
1485 |
+ |
// assert elementCount == elementData.length || elementData[elementCount] == null; |
1486 |
+ |
} |
1487 |
+ |
} |