1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1994-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2005 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. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
9 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
27 |
|
|
28 |
|
/** |
29 |
< |
* The <code>Vector</code> class implements a growable array of |
29 |
> |
* The {@code Vector} class implements a growable array of |
30 |
|
* objects. Like an array, it contains components that can be |
31 |
|
* accessed using an integer index. However, the size of a |
32 |
< |
* <code>Vector</code> can grow or shrink as needed to accommodate |
33 |
< |
* adding and removing items after the <code>Vector</code> has been created.<p> |
32 |
> |
* {@code Vector} can grow or shrink as needed to accommodate |
33 |
> |
* adding and removing items after the {@code Vector} has been created. |
34 |
|
* |
35 |
< |
* Each vector tries to optimize storage management by maintaining a |
36 |
< |
* <code>capacity</code> and a <code>capacityIncrement</code>. The |
37 |
< |
* <code>capacity</code> is always at least as large as the vector |
35 |
> |
* <p>Each vector tries to optimize storage management by maintaining a |
36 |
> |
* {@code capacity} and a {@code capacityIncrement}. The |
37 |
> |
* {@code capacity} is always at least as large as the vector |
38 |
|
* size; it is usually larger because as components are added to the |
39 |
|
* vector, the vector's storage increases in chunks the size of |
40 |
< |
* <code>capacityIncrement</code>. An application can increase the |
40 |
> |
* {@code capacityIncrement}. An application can increase the |
41 |
|
* capacity of a vector before inserting a large number of |
42 |
< |
* components; this reduces the amount of incremental reallocation. <p> |
26 |
< |
* |
27 |
< |
* As of the Java 2 platform v1.2, this class has been retrofitted to |
28 |
< |
* implement List, so that it becomes a part of Java's collection framework. |
29 |
< |
* Unlike the new collection implementations, Vector is synchronized.<p> |
42 |
> |
* components; this reduces the amount of incremental reallocation. |
43 |
|
* |
44 |
< |
* The Iterators returned by Vector's iterator and listIterator |
45 |
< |
* methods are <em>fail-fast</em>: if the Vector is structurally modified |
46 |
< |
* at any time after the Iterator is created, in any way except through the |
47 |
< |
* Iterator's own remove or add methods, the Iterator will throw a |
48 |
< |
* ConcurrentModificationException. Thus, in the face of concurrent |
49 |
< |
* modification, the Iterator fails quickly and cleanly, rather than risking |
50 |
< |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
51 |
< |
* The Enumerations returned by Vector's elements method are <em>not</em> |
52 |
< |
* fail-fast. |
44 |
> |
* <p><a name="fail-fast"/> |
45 |
> |
* The iterators returned by this class's {@link #iterator() iterator} and |
46 |
> |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
47 |
> |
* if the vector is structurally modified at any time after the iterator is |
48 |
> |
* created, in any way except through the iterator's own |
49 |
> |
* {@link ListIterator#remove() remove} or |
50 |
> |
* {@link ListIterator#add(Object) add} methods, the iterator will throw a |
51 |
> |
* {@link ConcurrentModificationException}. Thus, in the face of |
52 |
> |
* concurrent modification, the iterator fails quickly and cleanly, rather |
53 |
> |
* than risking arbitrary, non-deterministic behavior at an undetermined |
54 |
> |
* time in the future. The {@link Enumeration Enumerations} returned by |
55 |
> |
* the {@link #elements() elements} method are <em>not</em> fail-fast. |
56 |
|
* |
57 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
58 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
59 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
60 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
60 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
61 |
|
* Therefore, it would be wrong to write a program that depended on this |
62 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
63 |
< |
* should be used only to detect bugs.</i><p> |
63 |
> |
* should be used only to detect bugs.</i> |
64 |
|
* |
65 |
< |
* This class is a member of the |
66 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
67 |
< |
* Java Collections Framework</a>. |
65 |
> |
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
66 |
> |
* implement the {@link List} interface, making it a member of the |
67 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java |
68 |
> |
* Collections Framework</a>. Unlike the new collection |
69 |
> |
* implementations, {@code Vector} is synchronized. |
70 |
|
* |
71 |
|
* @author Lee Boynton |
72 |
|
* @author Jonathan Payne |
84 |
|
/** |
85 |
|
* The array buffer into which the components of the vector are |
86 |
|
* stored. The capacity of the vector is the length of this array buffer, |
87 |
< |
* and is at least large enough to contain all the vector's elements.<p> |
87 |
> |
* and is at least large enough to contain all the vector's elements. |
88 |
|
* |
89 |
< |
* Any array elements following the last element in the Vector are null. |
89 |
> |
* <p>Any array elements following the last element in the Vector are null. |
90 |
|
* |
91 |
|
* @serial |
92 |
|
*/ |
93 |
|
protected Object[] elementData; |
94 |
|
|
95 |
|
/** |
96 |
< |
* The number of valid components in this <tt>Vector</tt> object. |
97 |
< |
* Components <tt>elementData[0]</tt> through |
98 |
< |
* <tt>elementData[elementCount-1]</tt> are the actual items. |
96 |
> |
* The number of valid components in this {@code Vector} object. |
97 |
> |
* Components {@code elementData[0]} through |
98 |
> |
* {@code elementData[elementCount-1]} are the actual items. |
99 |
|
* |
100 |
|
* @serial |
101 |
|
*/ |
121 |
|
* @param initialCapacity the initial capacity of the vector |
122 |
|
* @param capacityIncrement the amount by which the capacity is |
123 |
|
* increased when the vector overflows |
124 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
125 |
< |
* is negative |
124 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
125 |
> |
* is negative |
126 |
|
*/ |
127 |
|
public Vector(int initialCapacity, int capacityIncrement) { |
128 |
|
super(); |
138 |
|
* with its capacity increment equal to zero. |
139 |
|
* |
140 |
|
* @param initialCapacity the initial capacity of the vector |
141 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
142 |
< |
* is negative |
141 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
142 |
> |
* is negative |
143 |
|
*/ |
144 |
|
public Vector(int initialCapacity) { |
145 |
|
this(initialCapacity, 0); |
147 |
|
|
148 |
|
/** |
149 |
|
* Constructs an empty vector so that its internal data array |
150 |
< |
* has size <tt>10</tt> and its standard capacity increment is |
150 |
> |
* has size {@code 10} and its standard capacity increment is |
151 |
|
* zero. |
152 |
|
*/ |
153 |
|
public Vector() { |
165 |
|
* @since 1.2 |
166 |
|
*/ |
167 |
|
public Vector(Collection<? extends E> c) { |
168 |
< |
Object[] a = c.toArray(); |
169 |
< |
elementCount = a.length; |
170 |
< |
// If c.toArray incorrectly doesn't return Object[], copy it. |
171 |
< |
if (a.getClass() == Object[].class) |
172 |
< |
elementData = a; |
155 |
< |
else |
156 |
< |
elementData = Arrays.copyOf(a, a.length, Object[].class); |
168 |
> |
elementData = c.toArray(); |
169 |
> |
elementCount = elementData.length; |
170 |
> |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
171 |
> |
if (elementData.getClass() != Object[].class) |
172 |
> |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
|
* Copies the components of this vector into the specified array. |
177 |
< |
* The item at index <tt>k</tt> in this vector is copied into |
178 |
< |
* component <tt>k</tt> of <tt>anArray</tt>. |
177 |
> |
* The item at index {@code k} in this vector is copied into |
178 |
> |
* component {@code k} of {@code anArray}. |
179 |
|
* |
180 |
|
* @param anArray the array into which the components get copied |
181 |
|
* @throws NullPointerException if the given array is null |
193 |
|
* Trims the capacity of this vector to be the vector's current |
194 |
|
* size. If the capacity of this vector is larger than its current |
195 |
|
* size, then the capacity is changed to equal the size by replacing |
196 |
< |
* its internal data array, kept in the field <tt>elementData</tt>, |
196 |
> |
* its internal data array, kept in the field {@code elementData}, |
197 |
|
* with a smaller one. An application can use this operation to |
198 |
|
* minimize the storage of a vector. |
199 |
|
*/ |
211 |
|
* the minimum capacity argument. |
212 |
|
* |
213 |
|
* <p>If the current capacity of this vector is less than |
214 |
< |
* <tt>minCapacity</tt>, then its capacity is increased by replacing its |
215 |
< |
* internal data array, kept in the field <tt>elementData</tt>, with a |
214 |
> |
* {@code minCapacity}, then its capacity is increased by replacing its |
215 |
> |
* internal data array, kept in the field {@code elementData}, with a |
216 |
|
* larger one. The size of the new data array will be the old size plus |
217 |
< |
* <tt>capacityIncrement</tt>, unless the value of |
218 |
< |
* <tt>capacityIncrement</tt> is less than or equal to zero, in which case |
217 |
> |
* {@code capacityIncrement}, unless the value of |
218 |
> |
* {@code capacityIncrement} is less than or equal to zero, in which case |
219 |
|
* the new capacity will be twice the old capacity; but if this new size |
220 |
< |
* is still smaller than <tt>minCapacity</tt>, then the new capacity will |
221 |
< |
* be <tt>minCapacity</tt>. |
220 |
> |
* is still smaller than {@code minCapacity}, then the new capacity will |
221 |
> |
* be {@code minCapacity}. |
222 |
|
* |
223 |
|
* @param minCapacity the desired minimum capacity |
224 |
|
*/ |
233 |
|
* method for ensuring capacity without incurring the cost of an |
234 |
|
* extra synchronization. |
235 |
|
* |
236 |
< |
* @see java.util.Vector#ensureCapacity(int) |
236 |
> |
* @see #ensureCapacity(int) |
237 |
|
*/ |
238 |
|
private void ensureCapacityHelper(int minCapacity) { |
239 |
|
int oldCapacity = elementData.length; |
250 |
|
|
251 |
|
/** |
252 |
|
* Sets the size of this vector. If the new size is greater than the |
253 |
< |
* current size, new <code>null</code> items are added to the end of |
253 |
> |
* current size, new {@code null} items are added to the end of |
254 |
|
* the vector. If the new size is less than the current size, all |
255 |
< |
* components at index <code>newSize</code> and greater are discarded. |
255 |
> |
* components at index {@code newSize} and greater are discarded. |
256 |
|
* |
257 |
< |
* @param newSize the new size of this vector |
258 |
< |
* @throws ArrayIndexOutOfBoundsException if new size is negative |
257 |
> |
* @param newSize the new size of this vector |
258 |
> |
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
259 |
|
*/ |
260 |
|
public synchronized void setSize(int newSize) { |
261 |
|
modCount++; |
273 |
|
* Returns the current capacity of this vector. |
274 |
|
* |
275 |
|
* @return the current capacity (the length of its internal |
276 |
< |
* data array, kept in the field <tt>elementData</tt> |
276 |
> |
* data array, kept in the field {@code elementData} |
277 |
|
* of this vector) |
278 |
|
*/ |
279 |
|
public synchronized int capacity() { |
292 |
|
/** |
293 |
|
* Tests if this vector has no components. |
294 |
|
* |
295 |
< |
* @return <code>true</code> if and only if this vector has |
295 |
> |
* @return {@code true} if and only if this vector has |
296 |
|
* no components, that is, its size is zero; |
297 |
< |
* <code>false</code> otherwise. |
297 |
> |
* {@code false} otherwise. |
298 |
|
*/ |
299 |
|
public synchronized boolean isEmpty() { |
300 |
|
return elementCount == 0; |
302 |
|
|
303 |
|
/** |
304 |
|
* Returns an enumeration of the components of this vector. The |
305 |
< |
* returned <tt>Enumeration</tt> object will generate all items in |
306 |
< |
* this vector. The first item generated is the item at index <tt>0</tt>, |
307 |
< |
* then the item at index <tt>1</tt>, and so on. |
305 |
> |
* returned {@code Enumeration} object will generate all items in |
306 |
> |
* this vector. The first item generated is the item at index {@code 0}, |
307 |
> |
* then the item at index {@code 1}, and so on. |
308 |
|
* |
309 |
|
* @return an enumeration of the components of this vector |
294 |
– |
* @see Enumeration |
310 |
|
* @see Iterator |
311 |
|
*/ |
312 |
|
public Enumeration<E> elements() { |
320 |
|
public E nextElement() { |
321 |
|
synchronized (Vector.this) { |
322 |
|
if (count < elementCount) { |
323 |
< |
return (E)elementData[count++]; |
323 |
> |
return elementData(count++); |
324 |
|
} |
325 |
|
} |
326 |
|
throw new NoSuchElementException("Vector Enumeration"); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* Returns <tt>true</tt> if this vector contains the specified element. |
333 |
< |
* More formally, returns <tt>true</tt> if and only if this vector |
334 |
< |
* contains at least one element <tt>e</tt> such that |
332 |
> |
* Returns {@code true} if this vector contains the specified element. |
333 |
> |
* More formally, returns {@code true} if and only if this vector |
334 |
> |
* contains at least one element {@code e} such that |
335 |
|
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
336 |
|
* |
337 |
|
* @param o element whose presence in this vector is to be tested |
338 |
< |
* @return <tt>true</tt> if this vector contains the specified element |
338 |
> |
* @return {@code true} if this vector contains the specified element |
339 |
|
*/ |
340 |
|
public boolean contains(Object o) { |
341 |
|
return indexOf(o, 0) >= 0; |
344 |
|
/** |
345 |
|
* Returns the index of the first occurrence of the specified element |
346 |
|
* in this vector, or -1 if this vector does not contain the element. |
347 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
347 |
> |
* More formally, returns the lowest index {@code i} such that |
348 |
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
349 |
|
* or -1 if there is no such index. |
350 |
|
* |
358 |
|
|
359 |
|
/** |
360 |
|
* Returns the index of the first occurrence of the specified element in |
361 |
< |
* this vector, searching forwards from <tt>index</tt>, or returns -1 if |
361 |
> |
* this vector, searching forwards from {@code index}, or returns -1 if |
362 |
|
* the element is not found. |
363 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
363 |
> |
* More formally, returns the lowest index {@code i} such that |
364 |
|
* <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
365 |
|
* or -1 if there is no such index. |
366 |
|
* |
367 |
|
* @param o element to search for |
368 |
|
* @param index index to start searching from |
369 |
|
* @return the index of the first occurrence of the element in |
370 |
< |
* this vector at position <tt>index</tt> or later in the vector; |
371 |
< |
* <tt>-1</tt> if the element is not found. |
370 |
> |
* this vector at position {@code index} or later in the vector; |
371 |
> |
* {@code -1} if the element is not found. |
372 |
|
* @throws IndexOutOfBoundsException if the specified index is negative |
373 |
|
* @see Object#equals(Object) |
374 |
|
*/ |
388 |
|
/** |
389 |
|
* Returns the index of the last occurrence of the specified element |
390 |
|
* in this vector, or -1 if this vector does not contain the element. |
391 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
391 |
> |
* More formally, returns the highest index {@code i} such that |
392 |
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
393 |
|
* or -1 if there is no such index. |
394 |
|
* |
402 |
|
|
403 |
|
/** |
404 |
|
* Returns the index of the last occurrence of the specified element in |
405 |
< |
* this vector, searching backwards from <tt>index</tt>, or returns -1 if |
405 |
> |
* this vector, searching backwards from {@code index}, or returns -1 if |
406 |
|
* the element is not found. |
407 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
407 |
> |
* More formally, returns the highest index {@code i} such that |
408 |
|
* <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, |
409 |
|
* or -1 if there is no such index. |
410 |
|
* |
411 |
|
* @param o element to search for |
412 |
|
* @param index index to start searching backwards from |
413 |
|
* @return the index of the last occurrence of the element at position |
414 |
< |
* less than or equal to <tt>index</tt> in this vector; |
414 |
> |
* less than or equal to {@code index} in this vector; |
415 |
|
* -1 if the element is not found. |
416 |
|
* @throws IndexOutOfBoundsException if the specified index is greater |
417 |
|
* than or equal to the current size of this vector |
433 |
|
} |
434 |
|
|
435 |
|
/** |
436 |
< |
* Returns the component at the specified index.<p> |
436 |
> |
* Returns the component at the specified index. |
437 |
|
* |
438 |
< |
* This method is identical in functionality to the get method |
439 |
< |
* (which is part of the List interface). |
438 |
> |
* <p>This method is identical in functionality to the {@link #get(int)} |
439 |
> |
* method (which is part of the {@link List} interface). |
440 |
|
* |
441 |
|
* @param index an index into this vector |
442 |
|
* @return the component at the specified index |
443 |
< |
* @exception ArrayIndexOutOfBoundsException if the <tt>index</tt> |
444 |
< |
* is negative or not less than the current size of this |
430 |
< |
* <tt>Vector</tt> object. |
431 |
< |
* @see #get(int) |
432 |
< |
* @see List |
443 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
444 |
> |
* ({@code index < 0 || index >= size()}) |
445 |
|
*/ |
446 |
|
public synchronized E elementAt(int index) { |
447 |
|
if (index >= elementCount) { |
448 |
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
449 |
|
} |
450 |
|
|
451 |
< |
return (E)elementData[index]; |
451 |
> |
return elementData(index); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
< |
* Returns the first component (the item at index <tt>0</tt>) of |
455 |
> |
* Returns the first component (the item at index {@code 0}) of |
456 |
|
* this vector. |
457 |
|
* |
458 |
|
* @return the first component of this vector |
459 |
< |
* @exception NoSuchElementException if this vector has no components |
459 |
> |
* @throws NoSuchElementException if this vector has no components |
460 |
|
*/ |
461 |
|
public synchronized E firstElement() { |
462 |
|
if (elementCount == 0) { |
463 |
|
throw new NoSuchElementException(); |
464 |
|
} |
465 |
< |
return (E)elementData[0]; |
465 |
> |
return elementData(0); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
470 |
|
* |
471 |
|
* @return the last component of the vector, i.e., the component at index |
472 |
|
* <code>size() - 1</code>. |
473 |
< |
* @exception NoSuchElementException if this vector is empty |
473 |
> |
* @throws NoSuchElementException if this vector is empty |
474 |
|
*/ |
475 |
|
public synchronized E lastElement() { |
476 |
|
if (elementCount == 0) { |
477 |
|
throw new NoSuchElementException(); |
478 |
|
} |
479 |
< |
return (E)elementData[elementCount - 1]; |
479 |
> |
return elementData(elementCount - 1); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
< |
* Sets the component at the specified <code>index</code> of this |
483 |
> |
* Sets the component at the specified {@code index} of this |
484 |
|
* vector to be the specified object. The previous component at that |
485 |
< |
* position is discarded.<p> |
485 |
> |
* position is discarded. |
486 |
|
* |
487 |
< |
* The index must be a value greater than or equal to <code>0</code> |
488 |
< |
* and less than the current size of the vector. <p> |
487 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
488 |
> |
* and less than the current size of the vector. |
489 |
|
* |
490 |
< |
* This method is identical in functionality to the set method |
491 |
< |
* (which is part of the List interface). Note that the set method reverses |
492 |
< |
* the order of the parameters, to more closely match array usage. Note |
493 |
< |
* also that the set method returns the old value that was stored at the |
494 |
< |
* specified position. |
490 |
> |
* <p>This method is identical in functionality to the |
491 |
> |
* {@link #set(int, Object) set(int, E)} |
492 |
> |
* method (which is part of the {@link List} interface). Note that the |
493 |
> |
* {@code set} method reverses the order of the parameters, to more closely |
494 |
> |
* match array usage. Note also that the {@code set} method returns the |
495 |
> |
* old value that was stored at the specified position. |
496 |
|
* |
497 |
|
* @param obj what the component is to be set to |
498 |
|
* @param index the specified index |
499 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
500 |
< |
* @see #size() |
488 |
< |
* @see List |
489 |
< |
* @see #set(int, java.lang.Object) |
499 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
500 |
> |
* ({@code index < 0 || index >= size()}) |
501 |
|
*/ |
502 |
|
public synchronized void setElementAt(E obj, int index) { |
503 |
|
if (index >= elementCount) { |
510 |
|
/** |
511 |
|
* Deletes the component at the specified index. Each component in |
512 |
|
* this vector with an index greater or equal to the specified |
513 |
< |
* <code>index</code> is shifted downward to have an index one |
513 |
> |
* {@code index} is shifted downward to have an index one |
514 |
|
* smaller than the value it had previously. The size of this vector |
515 |
< |
* is decreased by <tt>1</tt>.<p> |
515 |
> |
* is decreased by {@code 1}. |
516 |
|
* |
517 |
< |
* The index must be a value greater than or equal to <code>0</code> |
518 |
< |
* and less than the current size of the vector. <p> |
517 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
518 |
> |
* and less than the current size of the vector. |
519 |
|
* |
520 |
< |
* This method is identical in functionality to the remove method |
521 |
< |
* (which is part of the List interface). Note that the remove method |
522 |
< |
* returns the old value that was stored at the specified position. |
520 |
> |
* <p>This method is identical in functionality to the {@link #remove(int)} |
521 |
> |
* method (which is part of the {@link List} interface). Note that the |
522 |
> |
* {@code remove} method returns the old value that was stored at the |
523 |
> |
* specified position. |
524 |
|
* |
525 |
|
* @param index the index of the object to remove |
526 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
527 |
< |
* @see #size() |
516 |
< |
* @see #remove(int) |
517 |
< |
* @see List |
526 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
527 |
> |
* ({@code index < 0 || index >= size()}) |
528 |
|
*/ |
529 |
|
public synchronized void removeElementAt(int index) { |
530 |
|
modCount++; |
545 |
|
|
546 |
|
/** |
547 |
|
* Inserts the specified object as a component in this vector at the |
548 |
< |
* specified <code>index</code>. Each component in this vector with |
549 |
< |
* an index greater or equal to the specified <code>index</code> is |
548 |
> |
* specified {@code index}. Each component in this vector with |
549 |
> |
* an index greater or equal to the specified {@code index} is |
550 |
|
* shifted upward to have an index one greater than the value it had |
551 |
< |
* previously. <p> |
551 |
> |
* previously. |
552 |
|
* |
553 |
< |
* The index must be a value greater than or equal to <code>0</code> |
553 |
> |
* <p>The index must be a value greater than or equal to {@code 0} |
554 |
|
* and less than or equal to the current size of the vector. (If the |
555 |
|
* index is equal to the current size of the vector, the new element |
556 |
< |
* is appended to the Vector.)<p> |
556 |
> |
* is appended to the Vector.) |
557 |
|
* |
558 |
< |
* This method is identical in functionality to the add(Object, int) method |
559 |
< |
* (which is part of the List interface). Note that the add method reverses |
560 |
< |
* the order of the parameters, to more closely match array usage. |
558 |
> |
* <p>This method is identical in functionality to the |
559 |
> |
* {@link #add(int, Object) add(int, E)} |
560 |
> |
* method (which is part of the {@link List} interface). Note that the |
561 |
> |
* {@code add} method reverses the order of the parameters, to more closely |
562 |
> |
* match array usage. |
563 |
|
* |
564 |
|
* @param obj the component to insert |
565 |
|
* @param index where to insert the new component |
566 |
< |
* @exception ArrayIndexOutOfBoundsException if the index was invalid |
567 |
< |
* @see #size() |
556 |
< |
* @see #add(int, Object) |
557 |
< |
* @see List |
566 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
567 |
> |
* ({@code index < 0 || index > size()}) |
568 |
|
*/ |
569 |
|
public synchronized void insertElementAt(E obj, int index) { |
570 |
|
modCount++; |
581 |
|
/** |
582 |
|
* Adds the specified component to the end of this vector, |
583 |
|
* increasing its size by one. The capacity of this vector is |
584 |
< |
* increased if its size becomes greater than its capacity. <p> |
584 |
> |
* increased if its size becomes greater than its capacity. |
585 |
|
* |
586 |
< |
* This method is identical in functionality to the add(Object) method |
587 |
< |
* (which is part of the List interface). |
586 |
> |
* <p>This method is identical in functionality to the |
587 |
> |
* {@link #add(Object) add(E)} |
588 |
> |
* method (which is part of the {@link List} interface). |
589 |
|
* |
590 |
|
* @param obj the component to be added |
580 |
– |
* @see #add(Object) |
581 |
– |
* @see List |
591 |
|
*/ |
592 |
|
public synchronized void addElement(E obj) { |
593 |
|
modCount++; |
600 |
|
* from this vector. If the object is found in this vector, each |
601 |
|
* component in the vector with an index greater or equal to the |
602 |
|
* object's index is shifted downward to have an index one smaller |
603 |
< |
* than the value it had previously.<p> |
603 |
> |
* than the value it had previously. |
604 |
|
* |
605 |
< |
* This method is identical in functionality to the remove(Object) |
606 |
< |
* method (which is part of the List interface). |
605 |
> |
* <p>This method is identical in functionality to the |
606 |
> |
* {@link #remove(Object)} method (which is part of the |
607 |
> |
* {@link List} interface). |
608 |
|
* |
609 |
|
* @param obj the component to be removed |
610 |
< |
* @return <code>true</code> if the argument was a component of this |
611 |
< |
* vector; <code>false</code> otherwise. |
602 |
< |
* @see List#remove(Object) |
603 |
< |
* @see List |
610 |
> |
* @return {@code true} if the argument was a component of this |
611 |
> |
* vector; {@code false} otherwise. |
612 |
|
*/ |
613 |
|
public synchronized boolean removeElement(Object obj) { |
614 |
|
modCount++; |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
< |
* Removes all components from this vector and sets its size to zero.<p> |
624 |
> |
* Removes all components from this vector and sets its size to zero. |
625 |
|
* |
626 |
< |
* This method is identical in functionality to the clear method |
627 |
< |
* (which is part of the List interface). |
620 |
< |
* |
621 |
< |
* @see #clear |
622 |
< |
* @see List |
626 |
> |
* <p>This method is identical in functionality to the {@link #clear} |
627 |
> |
* method (which is part of the {@link List} interface). |
628 |
|
*/ |
629 |
|
public synchronized void removeAllElements() { |
630 |
|
modCount++; |
638 |
|
/** |
639 |
|
* Returns a clone of this vector. The copy will contain a |
640 |
|
* reference to a clone of the internal data array, not a reference |
641 |
< |
* to the original internal data array of this <tt>Vector</tt> object. |
641 |
> |
* to the original internal data array of this {@code Vector} object. |
642 |
|
* |
643 |
|
* @return a clone of this vector |
644 |
|
*/ |
645 |
|
public synchronized Object clone() { |
646 |
|
try { |
647 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
647 |
> |
@SuppressWarnings("unchecked") |
648 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
649 |
|
v.elementData = Arrays.copyOf(elementData, elementCount); |
650 |
|
v.modCount = 0; |
651 |
|
return v; |
670 |
|
* correct order; the runtime type of the returned array is that of the |
671 |
|
* specified array. If the Vector fits in the specified array, it is |
672 |
|
* returned therein. Otherwise, a new array is allocated with the runtime |
673 |
< |
* type of the specified array and the size of this Vector.<p> |
673 |
> |
* type of the specified array and the size of this Vector. |
674 |
|
* |
675 |
< |
* If the Vector fits in the specified array with room to spare |
675 |
> |
* <p>If the Vector fits in the specified array with room to spare |
676 |
|
* (i.e., the array has more elements than the Vector), |
677 |
|
* the element in the array immediately following the end of the |
678 |
|
* Vector is set to null. (This is useful in determining the length |
683 |
|
* be stored, if it is big enough; otherwise, a new array of the |
684 |
|
* same runtime type is allocated for this purpose. |
685 |
|
* @return an array containing the elements of the Vector |
686 |
< |
* @exception ArrayStoreException the runtime type of a is not a supertype |
686 |
> |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
687 |
|
* of the runtime type of every element in this Vector |
688 |
|
* @throws NullPointerException if the given array is null |
689 |
|
* @since 1.2 |
690 |
|
*/ |
691 |
+ |
@SuppressWarnings("unchecked") |
692 |
|
public synchronized <T> T[] toArray(T[] a) { |
693 |
|
if (a.length < elementCount) |
694 |
|
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); |
703 |
|
|
704 |
|
// Positional Access Operations |
705 |
|
|
706 |
+ |
@SuppressWarnings("unchecked") |
707 |
+ |
E elementData(int index) { |
708 |
+ |
return (E) elementData[index]; |
709 |
+ |
} |
710 |
+ |
|
711 |
|
/** |
712 |
|
* Returns the element at the specified position in this Vector. |
713 |
|
* |
714 |
|
* @param index index of the element to return |
715 |
|
* @return object at the specified index |
716 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range (index |
717 |
< |
* < 0 || index >= size()) |
716 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
717 |
> |
* ({@code index < 0 || index >= size()}) |
718 |
|
* @since 1.2 |
719 |
|
*/ |
720 |
|
public synchronized E get(int index) { |
721 |
|
if (index >= elementCount) |
722 |
|
throw new ArrayIndexOutOfBoundsException(index); |
723 |
|
|
724 |
< |
return (E)elementData[index]; |
724 |
> |
return elementData(index); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
731 |
|
* @param index index of the element to replace |
732 |
|
* @param element element to be stored at the specified position |
733 |
|
* @return the element previously at the specified position |
734 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range |
735 |
< |
* (index < 0 || index >= size()) |
734 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
735 |
> |
* ({@code index < 0 || index >= size()}) |
736 |
|
* @since 1.2 |
737 |
|
*/ |
738 |
|
public synchronized E set(int index, E element) { |
739 |
|
if (index >= elementCount) |
740 |
|
throw new ArrayIndexOutOfBoundsException(index); |
741 |
|
|
742 |
< |
Object oldValue = elementData[index]; |
742 |
> |
E oldValue = elementData(index); |
743 |
|
elementData[index] = element; |
744 |
< |
return (E)oldValue; |
744 |
> |
return oldValue; |
745 |
|
} |
746 |
|
|
747 |
|
/** |
748 |
|
* Appends the specified element to the end of this Vector. |
749 |
|
* |
750 |
|
* @param e element to be appended to this Vector |
751 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
751 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
752 |
|
* @since 1.2 |
753 |
|
*/ |
754 |
|
public synchronized boolean add(E e) { |
762 |
|
* Removes the first occurrence of the specified element in this Vector |
763 |
|
* If the Vector does not contain the element, it is unchanged. More |
764 |
|
* formally, removes the element with the lowest index i such that |
765 |
< |
* <code>(o==null ? get(i)==null : o.equals(get(i)))</code> (if such |
765 |
> |
* {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such |
766 |
|
* an element exists). |
767 |
|
* |
768 |
|
* @param o element to be removed from this Vector, if present |
780 |
|
* |
781 |
|
* @param index index at which the specified element is to be inserted |
782 |
|
* @param element element to be inserted |
783 |
< |
* @exception ArrayIndexOutOfBoundsException index is out of range |
784 |
< |
* (index < 0 || index > size()) |
783 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
784 |
> |
* ({@code index < 0 || index > size()}) |
785 |
|
* @since 1.2 |
786 |
|
*/ |
787 |
|
public void add(int index, E element) { |
793 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
794 |
|
* indices). Returns the element that was removed from the Vector. |
795 |
|
* |
796 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
797 |
< |
* < 0 || index >= size()) |
796 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
797 |
> |
* ({@code index < 0 || index >= size()}) |
798 |
|
* @param index the index of the element to be removed |
799 |
|
* @return element that was removed |
800 |
|
* @since 1.2 |
803 |
|
modCount++; |
804 |
|
if (index >= elementCount) |
805 |
|
throw new ArrayIndexOutOfBoundsException(index); |
806 |
< |
Object oldValue = elementData[index]; |
806 |
> |
E oldValue = elementData(index); |
807 |
|
|
808 |
|
int numMoved = elementCount - index - 1; |
809 |
|
if (numMoved > 0) |
811 |
|
numMoved); |
812 |
|
elementData[--elementCount] = null; // Let gc do its work |
813 |
|
|
814 |
< |
return (E)oldValue; |
814 |
> |
return oldValue; |
815 |
|
} |
816 |
|
|
817 |
|
/** |
849 |
|
* specified Collection is this Vector, and this Vector is nonempty.) |
850 |
|
* |
851 |
|
* @param c elements to be inserted into this Vector |
852 |
< |
* @return <tt>true</tt> if this Vector changed as a result of the call |
852 |
> |
* @return {@code true} if this Vector changed as a result of the call |
853 |
|
* @throws NullPointerException if the specified collection is null |
854 |
|
* @since 1.2 |
855 |
|
*/ |
912 |
|
* @param index index at which to insert the first element from the |
913 |
|
* specified collection |
914 |
|
* @param c elements to be inserted into this Vector |
915 |
< |
* @return <tt>true</tt> if this Vector changed as a result of the call |
916 |
< |
* @exception ArrayIndexOutOfBoundsException index out of range (index |
917 |
< |
* < 0 || index > size()) |
915 |
> |
* @return {@code true} if this Vector changed as a result of the call |
916 |
> |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
917 |
> |
* ({@code index < 0 || index > size()}) |
918 |
|
* @throws NullPointerException if the specified collection is null |
919 |
|
* @since 1.2 |
920 |
|
*/ |
941 |
|
* Compares the specified Object with this Vector for equality. Returns |
942 |
|
* true if and only if the specified Object is also a List, both Lists |
943 |
|
* have the same size, and all corresponding pairs of elements in the two |
944 |
< |
* Lists are <em>equal</em>. (Two elements <code>e1</code> and |
945 |
< |
* <code>e2</code> are <em>equal</em> if <code>(e1==null ? e2==null : |
946 |
< |
* e1.equals(e2))</code>.) In other words, two Lists are defined to be |
944 |
> |
* Lists are <em>equal</em>. (Two elements {@code e1} and |
945 |
> |
* {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null : |
946 |
> |
* e1.equals(e2))}.) In other words, two Lists are defined to be |
947 |
|
* equal if they contain the same elements in the same order. |
948 |
|
* |
949 |
|
* @param o the Object to be compared for equality with this Vector |
974 |
|
* equal, the returned List is empty.) The returned List is backed by this |
975 |
|
* List, so changes in the returned List are reflected in this List, and |
976 |
|
* vice-versa. The returned List supports all of the optional List |
977 |
< |
* operations supported by this List.<p> |
977 |
> |
* operations supported by this List. |
978 |
|
* |
979 |
< |
* This method eliminates the need for explicit range operations (of |
980 |
< |
* the sort that commonly exist for arrays). Any operation that expects |
979 |
> |
* <p>This method eliminates the need for explicit range operations (of |
980 |
> |
* the sort that commonly exist for arrays). Any operation that expects |
981 |
|
* a List can be used as a range operation by operating on a subList view |
982 |
|
* instead of a whole List. For example, the following idiom |
983 |
|
* removes a range of elements from a List: |
986 |
|
* </pre> |
987 |
|
* Similar idioms may be constructed for indexOf and lastIndexOf, |
988 |
|
* and all of the algorithms in the Collections class can be applied to |
989 |
< |
* a subList.<p> |
989 |
> |
* a subList. |
990 |
|
* |
991 |
< |
* The semantics of the List returned by this method become undefined if |
991 |
> |
* <p>The semantics of the List returned by this method become undefined if |
992 |
|
* the backing list (i.e., this List) is <i>structurally modified</i> in |
993 |
|
* any way other than via the returned List. (Structural modifications are |
994 |
|
* those that change the size of the List, or otherwise perturb it in such |
997 |
|
* @param fromIndex low endpoint (inclusive) of the subList |
998 |
|
* @param toIndex high endpoint (exclusive) of the subList |
999 |
|
* @return a view of the specified range within this List |
1000 |
< |
* @throws IndexOutOfBoundsException endpoint index value out of range |
1001 |
< |
* <code>(fromIndex < 0 || toIndex > size)</code> |
1002 |
< |
* @throws IllegalArgumentException endpoint indices out of order |
1003 |
< |
* <code>(fromIndex > toIndex)</code> |
1000 |
> |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
1001 |
> |
* {@code (fromIndex < 0 || toIndex > size)} |
1002 |
> |
* @throws IllegalArgumentException if the endpoint indices are out of order |
1003 |
> |
* {@code (fromIndex > toIndex)} |
1004 |
|
*/ |
1005 |
|
public synchronized List<E> subList(int fromIndex, int toIndex) { |
1006 |
|
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
1008 |
|
} |
1009 |
|
|
1010 |
|
/** |
1011 |
< |
* Removes from this List all of the elements whose index is between |
1012 |
< |
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding |
1013 |
< |
* elements to the left (reduces their index). |
1014 |
< |
* This call shortens the ArrayList by (toIndex - fromIndex) elements. (If |
1015 |
< |
* toIndex==fromIndex, this operation has no effect.) |
1004 |
< |
* |
1005 |
< |
* @param fromIndex index of first element to be removed |
1006 |
< |
* @param toIndex index after last element to be removed |
1011 |
> |
* Removes from this list all of the elements whose index is between |
1012 |
> |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
1013 |
> |
* Shifts any succeeding elements to the left (reduces their index). |
1014 |
> |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1015 |
> |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1016 |
|
*/ |
1017 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1018 |
|
modCount++; |
1027 |
|
} |
1028 |
|
|
1029 |
|
/** |
1030 |
< |
* Save the state of the <tt>Vector</tt> instance to a stream (that |
1030 |
> |
* Save the state of the {@code Vector} instance to a stream (that |
1031 |
|
* is, serialize it). This method is present merely for synchronization. |
1032 |
|
* It just calls the default writeObject method. |
1033 |
|
*/ |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1041 |
< |
* Returns a list-iterator of the elements in this list (in proper |
1041 |
> |
* Returns a list iterator over the elements in this list (in proper |
1042 |
|
* sequence), starting at the specified position in the list. |
1043 |
< |
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
1043 |
> |
* The specified index indicates the first element that would be |
1044 |
> |
* returned by an initial call to {@link ListIterator#next next}. |
1045 |
> |
* An initial call to {@link ListIterator#previous previous} would |
1046 |
> |
* return the element with the specified index minus one. |
1047 |
> |
* |
1048 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1049 |
|
* |
1036 |
– |
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
1037 |
– |
* modified at any time after the Iterator is created, in any way except |
1038 |
– |
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
1039 |
– |
* methods, the list-iterator will throw a |
1040 |
– |
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
1041 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
1042 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
1043 |
– |
* time in the future. |
1044 |
– |
* |
1045 |
– |
* @param index index of the first element to be returned from the |
1046 |
– |
* list-iterator (by a call to <tt>next</tt>) |
1047 |
– |
* @return a ListIterator of the elements in this list (in proper |
1048 |
– |
* sequence), starting at the specified position in the list |
1050 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
1050 |
– |
* @see List#listIterator(int) |
1051 |
|
*/ |
1052 |
|
public synchronized ListIterator<E> listIterator(int index) { |
1053 |
|
if (index < 0 || index > elementCount) |
1054 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
1055 |
< |
return new VectorIterator(index); |
1055 |
> |
return new ListItr(index); |
1056 |
|
} |
1057 |
< |
|
1057 |
> |
|
1058 |
> |
/** |
1059 |
> |
* Returns a list iterator over the elements in this list (in proper |
1060 |
> |
* sequence). |
1061 |
> |
* |
1062 |
> |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1063 |
> |
* |
1064 |
> |
* @see #listIterator(int) |
1065 |
> |
*/ |
1066 |
> |
public synchronized ListIterator<E> listIterator() { |
1067 |
> |
return new ListItr(0); |
1068 |
> |
} |
1069 |
> |
|
1070 |
|
/** |
1071 |
|
* Returns an iterator over the elements in this list in proper sequence. |
1072 |
|
* |
1073 |
+ |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
1074 |
+ |
* |
1075 |
|
* @return an iterator over the elements in this list in proper sequence |
1076 |
|
*/ |
1077 |
|
public synchronized Iterator<E> iterator() { |
1078 |
< |
return new VectorIterator(0); |
1078 |
> |
return new Itr(); |
1079 |
|
} |
1080 |
|
|
1081 |
|
/** |
1082 |
< |
* A streamlined version of AbstractList.Itr. |
1082 |
> |
* An optimized version of AbstractList.Itr |
1083 |
|
*/ |
1084 |
< |
final class VectorIterator implements ListIterator<E> { |
1085 |
< |
int cursor; // index of next element to return; |
1086 |
< |
int lastRet; // index of last element, or -1 if no such |
1087 |
< |
int expectedModCount; // to check for CME |
1084 |
> |
private class Itr implements Iterator<E> { |
1085 |
> |
int cursor; // index of next element to return |
1086 |
> |
int lastRet = -1; // index of last element returned; -1 if no such |
1087 |
> |
int expectedModCount = modCount; |
1088 |
|
|
1089 |
< |
VectorIterator(int index) { |
1090 |
< |
cursor = index; |
1091 |
< |
lastRet = -1; |
1092 |
< |
expectedModCount = modCount; |
1089 |
> |
public boolean hasNext() { |
1090 |
> |
// Racy but within spec, since modifications are checked |
1091 |
> |
// within or after synchronization in next/previous |
1092 |
> |
return cursor != elementCount; |
1093 |
|
} |
1094 |
|
|
1095 |
< |
public boolean hasNext() { |
1096 |
< |
// Racy but within spec and backwards-compatible |
1097 |
< |
return cursor < elementCount; |
1095 |
> |
public E next() { |
1096 |
> |
synchronized (Vector.this) { |
1097 |
> |
checkForComodification(); |
1098 |
> |
int i = cursor; |
1099 |
> |
if (i >= elementCount) |
1100 |
> |
throw new NoSuchElementException(); |
1101 |
> |
cursor = i + 1; |
1102 |
> |
return elementData(lastRet = i); |
1103 |
> |
} |
1104 |
> |
} |
1105 |
> |
|
1106 |
> |
public void remove() { |
1107 |
> |
if (lastRet == -1) |
1108 |
> |
throw new IllegalStateException(); |
1109 |
> |
synchronized (Vector.this) { |
1110 |
> |
checkForComodification(); |
1111 |
> |
Vector.this.remove(lastRet); |
1112 |
> |
expectedModCount = modCount; |
1113 |
> |
} |
1114 |
> |
cursor = lastRet; |
1115 |
> |
lastRet = -1; |
1116 |
> |
} |
1117 |
> |
|
1118 |
> |
final void checkForComodification() { |
1119 |
> |
if (modCount != expectedModCount) |
1120 |
> |
throw new ConcurrentModificationException(); |
1121 |
> |
} |
1122 |
> |
} |
1123 |
> |
|
1124 |
> |
/** |
1125 |
> |
* An optimized version of AbstractList.ListItr |
1126 |
> |
*/ |
1127 |
> |
final class ListItr extends Itr implements ListIterator<E> { |
1128 |
> |
ListItr(int index) { |
1129 |
> |
super(); |
1130 |
> |
cursor = index; |
1131 |
|
} |
1132 |
|
|
1133 |
|
public boolean hasPrevious() { |
1134 |
< |
return cursor > 0; |
1134 |
> |
return cursor != 0; |
1135 |
|
} |
1136 |
|
|
1137 |
|
public int nextIndex() { |
1142 |
|
return cursor - 1; |
1143 |
|
} |
1144 |
|
|
1145 |
< |
public E next() { |
1146 |
< |
synchronized(Vector.this) { |
1147 |
< |
if (expectedModCount == modCount) { |
1148 |
< |
int i = cursor; |
1149 |
< |
if (i < elementCount) { |
1150 |
< |
try { |
1151 |
< |
E e = (E)elementData[i]; |
1152 |
< |
lastRet = i; |
1153 |
< |
cursor = i + 1; |
1107 |
< |
return e; |
1108 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
1109 |
< |
} |
1110 |
< |
} |
1111 |
< |
} |
1112 |
< |
// Prefer reporting CME if applicable on failures |
1113 |
< |
if (expectedModCount == modCount) |
1114 |
< |
throw new NoSuchElementException(); |
1115 |
< |
throw new ConcurrentModificationException(); |
1116 |
< |
} |
1117 |
< |
} |
1118 |
< |
|
1119 |
< |
public E previous() { |
1120 |
< |
synchronized(Vector.this) { |
1121 |
< |
if (expectedModCount == modCount) { |
1122 |
< |
int i = cursor - 1; |
1123 |
< |
if (i < elementCount) { |
1124 |
< |
try { |
1125 |
< |
E e = (E)elementData[i]; |
1126 |
< |
lastRet = i; |
1127 |
< |
cursor = i; |
1128 |
< |
return e; |
1129 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
1130 |
< |
} |
1131 |
< |
} |
1132 |
< |
} |
1133 |
< |
if (expectedModCount == modCount) |
1134 |
< |
throw new NoSuchElementException(); |
1135 |
< |
throw new ConcurrentModificationException(); |
1136 |
< |
} |
1145 |
> |
public E previous() { |
1146 |
> |
synchronized (Vector.this) { |
1147 |
> |
checkForComodification(); |
1148 |
> |
int i = cursor - 1; |
1149 |
> |
if (i < 0) |
1150 |
> |
throw new NoSuchElementException(); |
1151 |
> |
cursor = i; |
1152 |
> |
return elementData(lastRet = i); |
1153 |
> |
} |
1154 |
|
} |
1155 |
|
|
1139 |
– |
public void remove() { |
1140 |
– |
if (lastRet < 0) |
1141 |
– |
throw new IllegalStateException(); |
1142 |
– |
synchronized(Vector.this) { |
1143 |
– |
if (modCount != expectedModCount) |
1144 |
– |
throw new ConcurrentModificationException(); |
1145 |
– |
Vector.this.remove(lastRet); |
1146 |
– |
if (lastRet < cursor) |
1147 |
– |
cursor--; |
1148 |
– |
lastRet = -1; |
1149 |
– |
expectedModCount = modCount; |
1150 |
– |
} |
1151 |
– |
} |
1152 |
– |
|
1156 |
|
public void set(E e) { |
1157 |
< |
if (lastRet < 0) |
1157 |
> |
if (lastRet == -1) |
1158 |
|
throw new IllegalStateException(); |
1159 |
< |
synchronized(Vector.this) { |
1160 |
< |
if (modCount != expectedModCount) |
1161 |
< |
throw new ConcurrentModificationException(); |
1162 |
< |
Vector.this.set(lastRet, e); |
1160 |
< |
expectedModCount = modCount; |
1161 |
< |
} |
1159 |
> |
synchronized (Vector.this) { |
1160 |
> |
checkForComodification(); |
1161 |
> |
Vector.this.set(lastRet, e); |
1162 |
> |
} |
1163 |
|
} |
1164 |
|
|
1165 |
|
public void add(E e) { |
1166 |
< |
synchronized(Vector.this) { |
1167 |
< |
if (modCount != expectedModCount) |
1168 |
< |
throw new ConcurrentModificationException(); |
1169 |
< |
Vector.this.add(cursor++, e); |
1170 |
< |
lastRet = -1; |
1171 |
< |
expectedModCount = modCount; |
1172 |
< |
} |
1166 |
> |
int i = cursor; |
1167 |
> |
synchronized (Vector.this) { |
1168 |
> |
checkForComodification(); |
1169 |
> |
Vector.this.add(i, e); |
1170 |
> |
expectedModCount = modCount; |
1171 |
> |
} |
1172 |
> |
cursor = i + 1; |
1173 |
> |
lastRet = -1; |
1174 |
|
} |
1175 |
|
} |
1174 |
– |
|
1176 |
|
} |