author | rriggs |
Fri, 24 Mar 2017 15:41:33 -0400 | |
changeset 45985 | dcdb2e744a54 |
parent 45984 | 75fef64e21fa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
28 |
import java.util.function.Consumer; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
29 |
import java.util.function.Predicate; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
30 |
import java.util.function.UnaryOperator; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
31 |
|
2 | 32 |
/** |
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} 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} 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}. 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 |
* |
|
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
22078
diff
changeset
|
48 |
* <p id="fail-fast"> |
2 | 49 |
* The iterators returned by this class's {@link #iterator() iterator} and |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
22078
diff
changeset
|
50 |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: |
2 | 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 |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
59 |
* the {@link #elements() elements} method are <em>not</em> fail-fast; if the |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
60 |
* Vector is structurally modified at any time after the enumeration is |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
61 |
* created then the results of enumerating are undefined. |
2 | 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 {@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 |
|
44743
f0bbd698c486
8177789: fix collections framework links to point to java.util package doc
smarks
parents:
43520
diff
changeset
|
73 |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
64
3244b8bab101
6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents:
2
diff
changeset
|
74 |
* Java Collections Framework</a>. Unlike the new collection |
3244b8bab101
6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents:
2
diff
changeset
|
75 |
* implementations, {@code Vector} is synchronized. If a thread-safe |
3244b8bab101
6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents:
2
diff
changeset
|
76 |
* implementation is not needed, it is recommended to use {@link |
3244b8bab101
6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents:
2
diff
changeset
|
77 |
* ArrayList} in place of {@code Vector}. |
2 | 78 |
* |
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
79 |
* @param <E> Type of component elements |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
80 |
* |
2 | 81 |
* @author Lee Boynton |
82 |
* @author Jonathan Payne |
|
83 |
* @see Collection |
|
84 |
* @see LinkedList |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24255
diff
changeset
|
85 |
* @since 1.0 |
2 | 86 |
*/ |
87 |
public class Vector<E> |
|
88 |
extends AbstractList<E> |
|
89 |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
|
90 |
{ |
|
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. |
|
95 |
* |
|
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 {@code Vector} object. |
|
104 |
* Components {@code elementData[0]} through |
|
105 |
* {@code elementData[elementCount-1]} are the actual items. |
|
106 |
* |
|
107 |
* @serial |
|
108 |
*/ |
|
109 |
protected int elementCount; |
|
110 |
||
111 |
/** |
|
112 |
* The amount by which the capacity of the vector is automatically |
|
113 |
* incremented when its size becomes greater than its capacity. If |
|
114 |
* the capacity increment is less than or equal to zero, the capacity |
|
115 |
* of the vector is doubled each time it needs to grow. |
|
116 |
* |
|
117 |
* @serial |
|
118 |
*/ |
|
119 |
protected int capacityIncrement; |
|
120 |
||
121 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
|
122 |
private static final long serialVersionUID = -2767605614048989439L; |
|
123 |
||
124 |
/** |
|
125 |
* Constructs an empty vector with the specified initial capacity and |
|
126 |
* capacity increment. |
|
127 |
* |
|
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 |
* @throws IllegalArgumentException if the specified initial capacity |
|
132 |
* is negative |
|
133 |
*/ |
|
134 |
public Vector(int initialCapacity, int capacityIncrement) { |
|
135 |
super(); |
|
136 |
if (initialCapacity < 0) |
|
137 |
throw new IllegalArgumentException("Illegal Capacity: "+ |
|
138 |
initialCapacity); |
|
139 |
this.elementData = new Object[initialCapacity]; |
|
140 |
this.capacityIncrement = capacityIncrement; |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Constructs an empty vector with the specified initial capacity and |
|
145 |
* with its capacity increment equal to zero. |
|
146 |
* |
|
147 |
* @param initialCapacity the initial capacity of the vector |
|
148 |
* @throws IllegalArgumentException if the specified initial capacity |
|
149 |
* is negative |
|
150 |
*/ |
|
151 |
public Vector(int initialCapacity) { |
|
152 |
this(initialCapacity, 0); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Constructs an empty vector so that its internal data array |
|
45985 | 157 |
* has size {@code 10} and its standard capacity increment is |
2 | 158 |
* zero. |
159 |
*/ |
|
160 |
public Vector() { |
|
161 |
this(10); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Constructs a vector containing the elements of the specified |
|
166 |
* collection, in the order they are returned by the collection's |
|
167 |
* iterator. |
|
168 |
* |
|
169 |
* @param c the collection whose elements are to be placed into this |
|
170 |
* vector |
|
171 |
* @throws NullPointerException if the specified collection is null |
|
172 |
* @since 1.2 |
|
173 |
*/ |
|
174 |
public Vector(Collection<? extends E> c) { |
|
175 |
elementData = c.toArray(); |
|
176 |
elementCount = elementData.length; |
|
31540
6efd719b3330
6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents:
25859
diff
changeset
|
177 |
// defend against c.toArray (incorrectly) not returning Object[] |
6efd719b3330
6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents:
25859
diff
changeset
|
178 |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
2 | 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 {@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 |
|
190 |
* @throws IndexOutOfBoundsException if the specified array is not |
|
191 |
* large enough to hold all the components of this vector |
|
192 |
* @throws ArrayStoreException if a component of this vector is not of |
|
193 |
* a runtime type that can be stored in the specified array |
|
194 |
* @see #toArray(Object[]) |
|
195 |
*/ |
|
196 |
public synchronized void copyInto(Object[] anArray) { |
|
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 {@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) { |
|
212 |
elementData = Arrays.copyOf(elementData, elementCount); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Increases the capacity of this vector, if necessary, to ensure |
|
218 |
* that it can hold at least the number of components specified by |
|
219 |
* the minimum capacity argument. |
|
220 |
* |
|
221 |
* <p>If the current capacity of this vector is less than |
|
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 |
* {@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 {@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) { |
|
7020
25638687fe82
6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents:
5506
diff
changeset
|
234 |
if (minCapacity > 0) { |
25638687fe82
6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents:
5506
diff
changeset
|
235 |
modCount++; |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
236 |
if (minCapacity > elementData.length) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
237 |
grow(minCapacity); |
7020
25638687fe82
6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents:
5506
diff
changeset
|
238 |
} |
2 | 239 |
} |
240 |
||
241 |
/** |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
242 |
* The maximum size of array to allocate (unless necessary). |
5466
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
243 |
* Some VMs reserve some header words in an array. |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
244 |
* Attempts to allocate larger arrays may result in |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
245 |
* OutOfMemoryError: Requested array size exceeds VM limit |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
246 |
*/ |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
247 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
248 |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
249 |
/** |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
250 |
* Increases the capacity to ensure that it can hold at least the |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
251 |
* number of elements specified by the minimum capacity argument. |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
252 |
* |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
253 |
* @param minCapacity the desired minimum capacity |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
254 |
* @throws OutOfMemoryError if minCapacity is less than zero |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
255 |
*/ |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
256 |
private Object[] grow(int minCapacity) { |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
257 |
return elementData = Arrays.copyOf(elementData, |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
258 |
newCapacity(minCapacity)); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
259 |
} |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
260 |
|
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
261 |
private Object[] grow() { |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
262 |
return grow(elementCount + 1); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
263 |
} |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
264 |
|
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
265 |
/** |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
266 |
* Returns a capacity at least as large as the given minimum capacity. |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
267 |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
268 |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
269 |
* |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
270 |
* @param minCapacity the desired minimum capacity |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
271 |
* @throws OutOfMemoryError if minCapacity is less than zero |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
272 |
*/ |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
273 |
private int newCapacity(int minCapacity) { |
5466
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
274 |
// overflow-conscious code |
2 | 275 |
int oldCapacity = elementData.length; |
5466
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
276 |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
277 |
capacityIncrement : oldCapacity); |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
278 |
if (newCapacity - minCapacity <= 0) { |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
279 |
if (minCapacity < 0) // overflow |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
280 |
throw new OutOfMemoryError(); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
281 |
return minCapacity; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
282 |
} |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
283 |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
284 |
? newCapacity |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
285 |
: hugeCapacity(minCapacity); |
5466
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
286 |
} |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
287 |
|
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
288 |
private static int hugeCapacity(int minCapacity) { |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
289 |
if (minCapacity < 0) // overflow |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
290 |
throw new OutOfMemoryError(); |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
291 |
return (minCapacity > MAX_ARRAY_SIZE) ? |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
292 |
Integer.MAX_VALUE : |
f130bb07764b
6933217: Huge arrays handled poorly in core libraries
martin
parents:
715
diff
changeset
|
293 |
MAX_ARRAY_SIZE; |
2 | 294 |
} |
295 |
||
296 |
/** |
|
297 |
* Sets the size of this vector. If the new size is greater than the |
|
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} and greater are discarded. |
|
301 |
* |
|
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++; |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
307 |
if (newSize > elementData.length) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
308 |
grow(newSize); |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
309 |
final Object[] es = elementData; |
43520
65d31ea930aa
8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents:
42927
diff
changeset
|
310 |
for (int to = elementCount, i = newSize; i < to; i++) |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
311 |
es[i] = null; |
43520
65d31ea930aa
8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents:
42927
diff
changeset
|
312 |
elementCount = newSize; |
2 | 313 |
} |
314 |
||
315 |
/** |
|
316 |
* Returns the current capacity of this vector. |
|
317 |
* |
|
318 |
* @return the current capacity (the length of its internal |
|
319 |
* data array, kept in the field {@code elementData} |
|
320 |
* of this vector) |
|
321 |
*/ |
|
322 |
public synchronized int capacity() { |
|
323 |
return elementData.length; |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Returns the number of components in this vector. |
|
328 |
* |
|
329 |
* @return the number of components in this vector |
|
330 |
*/ |
|
331 |
public synchronized int size() { |
|
332 |
return elementCount; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Tests if this vector has no components. |
|
337 |
* |
|
338 |
* @return {@code true} if and only if this vector has |
|
339 |
* no components, that is, its size is zero; |
|
340 |
* {@code false} otherwise. |
|
341 |
*/ |
|
342 |
public synchronized boolean isEmpty() { |
|
343 |
return elementCount == 0; |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Returns an enumeration of the components of this vector. The |
|
348 |
* returned {@code Enumeration} object will generate all items in |
|
349 |
* this vector. The first item generated is the item at index {@code 0}, |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
350 |
* then the item at index {@code 1}, and so on. If the vector is |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
351 |
* structurally modified while enumerating over the elements then the |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
352 |
* results of enumerating are undefined. |
2 | 353 |
* |
354 |
* @return an enumeration of the components of this vector |
|
355 |
* @see Iterator |
|
356 |
*/ |
|
357 |
public Enumeration<E> elements() { |
|
358 |
return new Enumeration<E>() { |
|
359 |
int count = 0; |
|
360 |
||
361 |
public boolean hasMoreElements() { |
|
362 |
return count < elementCount; |
|
363 |
} |
|
364 |
||
365 |
public E nextElement() { |
|
366 |
synchronized (Vector.this) { |
|
367 |
if (count < elementCount) { |
|
368 |
return elementData(count++); |
|
369 |
} |
|
370 |
} |
|
371 |
throw new NoSuchElementException("Vector Enumeration"); |
|
372 |
} |
|
373 |
}; |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Returns {@code true} if this vector contains the specified element. |
|
378 |
* More formally, returns {@code true} if and only if this vector |
|
379 |
* contains at least one element {@code e} such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
380 |
* {@code Objects.equals(o, e)}. |
2 | 381 |
* |
382 |
* @param o element whose presence in this vector is to be tested |
|
383 |
* @return {@code true} if this vector contains the specified element |
|
384 |
*/ |
|
385 |
public boolean contains(Object o) { |
|
386 |
return indexOf(o, 0) >= 0; |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* Returns the index of the first occurrence of the specified element |
|
391 |
* in this vector, or -1 if this vector does not contain the element. |
|
392 |
* More formally, returns the lowest index {@code i} such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
393 |
* {@code Objects.equals(o, get(i))}, |
2 | 394 |
* or -1 if there is no such index. |
395 |
* |
|
396 |
* @param o element to search for |
|
397 |
* @return the index of the first occurrence of the specified element in |
|
398 |
* this vector, or -1 if this vector does not contain the element |
|
399 |
*/ |
|
400 |
public int indexOf(Object o) { |
|
401 |
return indexOf(o, 0); |
|
402 |
} |
|
403 |
||
404 |
/** |
|
405 |
* Returns the index of the first occurrence of the specified element in |
|
406 |
* this vector, searching forwards from {@code index}, or returns -1 if |
|
407 |
* the element is not found. |
|
408 |
* More formally, returns the lowest index {@code i} such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
409 |
* {@code (i >= index && Objects.equals(o, get(i)))}, |
2 | 410 |
* or -1 if there is no such index. |
411 |
* |
|
412 |
* @param o element to search for |
|
413 |
* @param index index to start searching from |
|
414 |
* @return the index of the first occurrence of the element in |
|
415 |
* this vector at position {@code index} or later in the vector; |
|
416 |
* {@code -1} if the element is not found. |
|
417 |
* @throws IndexOutOfBoundsException if the specified index is negative |
|
418 |
* @see Object#equals(Object) |
|
419 |
*/ |
|
420 |
public synchronized int indexOf(Object o, int index) { |
|
421 |
if (o == null) { |
|
422 |
for (int i = index ; i < elementCount ; i++) |
|
423 |
if (elementData[i]==null) |
|
424 |
return i; |
|
425 |
} else { |
|
426 |
for (int i = index ; i < elementCount ; i++) |
|
427 |
if (o.equals(elementData[i])) |
|
428 |
return i; |
|
429 |
} |
|
430 |
return -1; |
|
431 |
} |
|
432 |
||
433 |
/** |
|
434 |
* Returns the index of the last occurrence of the specified element |
|
435 |
* in this vector, or -1 if this vector does not contain the element. |
|
436 |
* More formally, returns the highest index {@code i} such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
437 |
* {@code Objects.equals(o, get(i))}, |
2 | 438 |
* or -1 if there is no such index. |
439 |
* |
|
440 |
* @param o element to search for |
|
441 |
* @return the index of the last occurrence of the specified element in |
|
442 |
* this vector, or -1 if this vector does not contain the element |
|
443 |
*/ |
|
444 |
public synchronized int lastIndexOf(Object o) { |
|
445 |
return lastIndexOf(o, elementCount-1); |
|
446 |
} |
|
447 |
||
448 |
/** |
|
449 |
* Returns the index of the last occurrence of the specified element in |
|
450 |
* this vector, searching backwards from {@code index}, or returns -1 if |
|
451 |
* the element is not found. |
|
452 |
* More formally, returns the highest index {@code i} such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
453 |
* {@code (i <= index && Objects.equals(o, get(i)))}, |
2 | 454 |
* or -1 if there is no such index. |
455 |
* |
|
456 |
* @param o element to search for |
|
457 |
* @param index index to start searching backwards from |
|
458 |
* @return the index of the last occurrence of the element at position |
|
459 |
* less than or equal to {@code index} in this vector; |
|
460 |
* -1 if the element is not found. |
|
461 |
* @throws IndexOutOfBoundsException if the specified index is greater |
|
462 |
* than or equal to the current size of this vector |
|
463 |
*/ |
|
464 |
public synchronized int lastIndexOf(Object o, int index) { |
|
465 |
if (index >= elementCount) |
|
466 |
throw new IndexOutOfBoundsException(index + " >= "+ elementCount); |
|
467 |
||
468 |
if (o == null) { |
|
469 |
for (int i = index; i >= 0; i--) |
|
470 |
if (elementData[i]==null) |
|
471 |
return i; |
|
472 |
} else { |
|
473 |
for (int i = index; i >= 0; i--) |
|
474 |
if (o.equals(elementData[i])) |
|
475 |
return i; |
|
476 |
} |
|
477 |
return -1; |
|
478 |
} |
|
479 |
||
480 |
/** |
|
481 |
* Returns the component at the specified index. |
|
482 |
* |
|
483 |
* <p>This method is identical in functionality to the {@link #get(int)} |
|
484 |
* method (which is part of the {@link List} interface). |
|
485 |
* |
|
486 |
* @param index an index into this vector |
|
487 |
* @return the component at the specified index |
|
488 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
|
489 |
* ({@code index < 0 || index >= size()}) |
|
490 |
*/ |
|
491 |
public synchronized E elementAt(int index) { |
|
492 |
if (index >= elementCount) { |
|
493 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
|
494 |
} |
|
495 |
||
496 |
return elementData(index); |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* Returns the first component (the item at index {@code 0}) of |
|
501 |
* this vector. |
|
502 |
* |
|
503 |
* @return the first component of this vector |
|
504 |
* @throws NoSuchElementException if this vector has no components |
|
505 |
*/ |
|
506 |
public synchronized E firstElement() { |
|
507 |
if (elementCount == 0) { |
|
508 |
throw new NoSuchElementException(); |
|
509 |
} |
|
510 |
return elementData(0); |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* Returns the last component of the vector. |
|
515 |
* |
|
516 |
* @return the last component of the vector, i.e., the component at index |
|
42319 | 517 |
* {@code size() - 1} |
2 | 518 |
* @throws NoSuchElementException if this vector is empty |
519 |
*/ |
|
520 |
public synchronized E lastElement() { |
|
521 |
if (elementCount == 0) { |
|
522 |
throw new NoSuchElementException(); |
|
523 |
} |
|
524 |
return elementData(elementCount - 1); |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Sets the component at the specified {@code index} of this |
|
529 |
* vector to be the specified object. The previous component at that |
|
530 |
* position is discarded. |
|
531 |
* |
|
532 |
* <p>The index must be a value greater than or equal to {@code 0} |
|
533 |
* and less than the current size of the vector. |
|
534 |
* |
|
535 |
* <p>This method is identical in functionality to the |
|
536 |
* {@link #set(int, Object) set(int, E)} |
|
537 |
* method (which is part of the {@link List} interface). Note that the |
|
538 |
* {@code set} method reverses the order of the parameters, to more closely |
|
539 |
* match array usage. Note also that the {@code set} method returns the |
|
540 |
* old value that was stored at the specified position. |
|
541 |
* |
|
542 |
* @param obj what the component is to be set to |
|
543 |
* @param index the specified index |
|
544 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
|
545 |
* ({@code index < 0 || index >= size()}) |
|
546 |
*/ |
|
547 |
public synchronized void setElementAt(E obj, int index) { |
|
548 |
if (index >= elementCount) { |
|
549 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + |
|
550 |
elementCount); |
|
551 |
} |
|
552 |
elementData[index] = obj; |
|
553 |
} |
|
554 |
||
555 |
/** |
|
556 |
* Deletes the component at the specified index. Each component in |
|
557 |
* this vector with an index greater or equal to the specified |
|
558 |
* {@code index} is shifted downward to have an index one |
|
559 |
* smaller than the value it had previously. The size of this vector |
|
560 |
* is decreased by {@code 1}. |
|
561 |
* |
|
562 |
* <p>The index must be a value greater than or equal to {@code 0} |
|
563 |
* and less than the current size of the vector. |
|
564 |
* |
|
565 |
* <p>This method is identical in functionality to the {@link #remove(int)} |
|
566 |
* method (which is part of the {@link List} interface). Note that the |
|
567 |
* {@code remove} method returns the old value that was stored at the |
|
568 |
* specified position. |
|
569 |
* |
|
570 |
* @param index the index of the object to remove |
|
571 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
|
572 |
* ({@code index < 0 || index >= size()}) |
|
573 |
*/ |
|
574 |
public synchronized void removeElementAt(int index) { |
|
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 |
} |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
586 |
modCount++; |
2 | 587 |
elementCount--; |
588 |
elementData[elementCount] = null; /* to let gc do its work */ |
|
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* Inserts the specified object as a component in this vector at the |
|
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. |
|
597 |
* |
|
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.) |
|
602 |
* |
|
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 |
* @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 |
if (index > elementCount) { |
|
616 |
throw new ArrayIndexOutOfBoundsException(index |
|
617 |
+ " > " + elementCount); |
|
618 |
} |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
619 |
modCount++; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
620 |
final int s = elementCount; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
621 |
Object[] elementData = this.elementData; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
622 |
if (s == elementData.length) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
623 |
elementData = grow(); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
624 |
System.arraycopy(elementData, index, |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
625 |
elementData, index + 1, |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
626 |
s - index); |
2 | 627 |
elementData[index] = obj; |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
628 |
elementCount = s + 1; |
2 | 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. |
|
635 |
* |
|
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 |
|
641 |
*/ |
|
642 |
public synchronized void addElement(E obj) { |
|
643 |
modCount++; |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
644 |
add(obj, elementData, elementCount); |
2 | 645 |
} |
646 |
||
647 |
/** |
|
648 |
* Removes the first (lowest-indexed) occurrence of the argument |
|
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. |
|
653 |
* |
|
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} 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; |
|
670 |
} |
|
671 |
||
672 |
/** |
|
673 |
* Removes all components from this vector and sets its size to zero. |
|
674 |
* |
|
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() { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
679 |
final Object[] es = elementData; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
680 |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
681 |
es[i] = null; |
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
682 |
modCount++; |
2 | 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 {@code Vector} object. |
|
689 |
* |
|
690 |
* @return a clone of this vector |
|
691 |
*/ |
|
692 |
public synchronized Object clone() { |
|
693 |
try { |
|
694 |
@SuppressWarnings("unchecked") |
|
42319 | 695 |
Vector<E> v = (Vector<E>) super.clone(); |
2 | 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 |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
9503
diff
changeset
|
701 |
throw new InternalError(e); |
2 | 702 |
} |
703 |
} |
|
704 |
||
705 |
/** |
|
706 |
* Returns an array containing all of the elements in this Vector |
|
707 |
* in the correct order. |
|
708 |
* |
|
709 |
* @since 1.2 |
|
710 |
*/ |
|
711 |
public synchronized Object[] toArray() { |
|
712 |
return Arrays.copyOf(elementData, elementCount); |
|
713 |
} |
|
714 |
||
715 |
/** |
|
716 |
* Returns an array containing all of the elements in this Vector in the |
|
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. |
|
721 |
* |
|
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 |
* |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
729 |
* @param <T> type of array elements. The same type as {@code <E>} or a |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
730 |
* supertype of {@code <E>}. |
2 | 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. |
|
734 |
* @return an array containing the elements of the Vector |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
735 |
* @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
736 |
* a supertype of the runtime type, {@code <E>}, of every element in this |
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
737 |
* Vector |
2 | 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); |
|
747 |
||
748 |
if (a.length > elementCount) |
|
749 |
a[elementCount] = null; |
|
750 |
||
751 |
return a; |
|
752 |
} |
|
753 |
||
754 |
// Positional Access Operations |
|
755 |
||
756 |
@SuppressWarnings("unchecked") |
|
757 |
E elementData(int index) { |
|
758 |
return (E) elementData[index]; |
|
759 |
} |
|
760 |
||
42319 | 761 |
@SuppressWarnings("unchecked") |
762 |
static <E> E elementAt(Object[] es, int index) { |
|
763 |
return (E) es[index]; |
|
764 |
} |
|
765 |
||
2 | 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 |
* @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); |
|
778 |
||
779 |
return elementData(index); |
|
780 |
} |
|
781 |
||
782 |
/** |
|
783 |
* Replaces the element at the specified position in this Vector with the |
|
784 |
* specified element. |
|
785 |
* |
|
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 |
* @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); |
|
796 |
||
797 |
E oldValue = elementData(index); |
|
798 |
elementData[index] = element; |
|
799 |
return oldValue; |
|
800 |
} |
|
801 |
||
802 |
/** |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
803 |
* This helper method split out from add(E) to keep method |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
804 |
* bytecode size under 35 (the -XX:MaxInlineSize default value), |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
805 |
* which helps when add(E) is called in a C1-compiled loop. |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
806 |
*/ |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
807 |
private void add(E e, Object[] elementData, int s) { |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
808 |
if (s == elementData.length) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
809 |
elementData = grow(); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
810 |
elementData[s] = e; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
811 |
elementCount = s + 1; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
812 |
} |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
813 |
|
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
814 |
/** |
2 | 815 |
* Appends the specified element to the end of this Vector. |
816 |
* |
|
817 |
* @param e element to be appended to this Vector |
|
818 |
* @return {@code true} (as specified by {@link Collection#add}) |
|
819 |
* @since 1.2 |
|
820 |
*/ |
|
821 |
public synchronized boolean add(E e) { |
|
822 |
modCount++; |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
823 |
add(e, elementData, elementCount); |
2 | 824 |
return true; |
825 |
} |
|
826 |
||
827 |
/** |
|
828 |
* Removes the first occurrence of the specified element in this Vector |
|
829 |
* If the Vector does not contain the element, it is unchanged. More |
|
830 |
* formally, removes the element with the lowest index i such that |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
831 |
* {@code Objects.equals(o, get(i))} (if such |
2 | 832 |
* an element exists). |
833 |
* |
|
834 |
* @param o element to be removed from this Vector, if present |
|
835 |
* @return true if the Vector contained the specified element |
|
836 |
* @since 1.2 |
|
837 |
*/ |
|
838 |
public boolean remove(Object o) { |
|
839 |
return removeElement(o); |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Inserts the specified element at the specified position in this Vector. |
|
844 |
* Shifts the element currently at that position (if any) and any |
|
845 |
* subsequent elements to the right (adds one to their indices). |
|
846 |
* |
|
847 |
* @param index index at which the specified element is to be inserted |
|
848 |
* @param element element to be inserted |
|
849 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
|
850 |
* ({@code index < 0 || index > size()}) |
|
851 |
* @since 1.2 |
|
852 |
*/ |
|
853 |
public void add(int index, E element) { |
|
854 |
insertElementAt(element, index); |
|
855 |
} |
|
856 |
||
857 |
/** |
|
858 |
* Removes the element at the specified position in this Vector. |
|
859 |
* Shifts any subsequent elements to the left (subtracts one from their |
|
860 |
* indices). Returns the element that was removed from the Vector. |
|
861 |
* |
|
42319 | 862 |
* @param index the index of the element to be removed |
863 |
* @return element that was removed |
|
2 | 864 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
865 |
* ({@code index < 0 || index >= size()}) |
|
866 |
* @since 1.2 |
|
867 |
*/ |
|
868 |
public synchronized E remove(int index) { |
|
869 |
modCount++; |
|
870 |
if (index >= elementCount) |
|
871 |
throw new ArrayIndexOutOfBoundsException(index); |
|
872 |
E oldValue = elementData(index); |
|
873 |
||
874 |
int numMoved = elementCount - index - 1; |
|
875 |
if (numMoved > 0) |
|
876 |
System.arraycopy(elementData, index+1, elementData, index, |
|
877 |
numMoved); |
|
878 |
elementData[--elementCount] = null; // Let gc do its work |
|
879 |
||
880 |
return oldValue; |
|
881 |
} |
|
882 |
||
883 |
/** |
|
884 |
* Removes all of the elements from this Vector. The Vector will |
|
885 |
* be empty after this call returns (unless it throws an exception). |
|
886 |
* |
|
887 |
* @since 1.2 |
|
888 |
*/ |
|
889 |
public void clear() { |
|
890 |
removeAllElements(); |
|
891 |
} |
|
892 |
||
893 |
// Bulk Operations |
|
894 |
||
895 |
/** |
|
896 |
* Returns true if this Vector contains all of the elements in the |
|
897 |
* specified Collection. |
|
898 |
* |
|
899 |
* @param c a collection whose elements will be tested for containment |
|
900 |
* in this Vector |
|
901 |
* @return true if this Vector contains all of the elements in the |
|
902 |
* specified collection |
|
903 |
* @throws NullPointerException if the specified collection is null |
|
904 |
*/ |
|
905 |
public synchronized boolean containsAll(Collection<?> c) { |
|
906 |
return super.containsAll(c); |
|
907 |
} |
|
908 |
||
909 |
/** |
|
910 |
* Appends all of the elements in the specified Collection to the end of |
|
911 |
* this Vector, in the order that they are returned by the specified |
|
912 |
* Collection's Iterator. The behavior of this operation is undefined if |
|
913 |
* the specified Collection is modified while the operation is in progress. |
|
914 |
* (This implies that the behavior of this call is undefined if the |
|
915 |
* specified Collection is this Vector, and this Vector is nonempty.) |
|
916 |
* |
|
917 |
* @param c elements to be inserted into this Vector |
|
918 |
* @return {@code true} if this Vector changed as a result of the call |
|
919 |
* @throws NullPointerException if the specified collection is null |
|
920 |
* @since 1.2 |
|
921 |
*/ |
|
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
922 |
public boolean addAll(Collection<? extends E> c) { |
2 | 923 |
Object[] a = c.toArray(); |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
924 |
modCount++; |
2 | 925 |
int numNew = a.length; |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
926 |
if (numNew == 0) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
927 |
return false; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
928 |
synchronized (this) { |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
929 |
Object[] elementData = this.elementData; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
930 |
final int s = elementCount; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
931 |
if (numNew > elementData.length - s) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
932 |
elementData = grow(s + numNew); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
933 |
System.arraycopy(a, 0, elementData, s, numNew); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
934 |
elementCount = s + numNew; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
935 |
return true; |
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
936 |
} |
2 | 937 |
} |
938 |
||
939 |
/** |
|
940 |
* Removes from this Vector all of its elements that are contained in the |
|
941 |
* specified Collection. |
|
942 |
* |
|
943 |
* @param c a collection of elements to be removed from the Vector |
|
944 |
* @return true if this Vector changed as a result of the call |
|
945 |
* @throws ClassCastException if the types of one or more elements |
|
946 |
* in this vector are incompatible with the specified |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
947 |
* collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
948 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 949 |
* @throws NullPointerException if this vector contains one or more null |
950 |
* elements and the specified collection does not support null |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
951 |
* elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
952 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
953 |
* or if the specified collection is null |
2 | 954 |
* @since 1.2 |
955 |
*/ |
|
42319 | 956 |
public boolean removeAll(Collection<?> c) { |
957 |
Objects.requireNonNull(c); |
|
958 |
return bulkRemove(e -> c.contains(e)); |
|
2 | 959 |
} |
960 |
||
961 |
/** |
|
962 |
* Retains only the elements in this Vector that are contained in the |
|
963 |
* specified Collection. In other words, removes from this Vector all |
|
964 |
* of its elements that are not contained in the specified Collection. |
|
965 |
* |
|
966 |
* @param c a collection of elements to be retained in this Vector |
|
967 |
* (all other elements are removed) |
|
968 |
* @return true if this Vector changed as a result of the call |
|
969 |
* @throws ClassCastException if the types of one or more elements |
|
970 |
* in this vector are incompatible with the specified |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
971 |
* collection |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
972 |
* (<a href="Collection.html#optional-restrictions">optional</a>) |
2 | 973 |
* @throws NullPointerException if this vector contains one or more null |
974 |
* elements and the specified collection does not support null |
|
9503
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
975 |
* elements |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
976 |
* (<a href="Collection.html#optional-restrictions">optional</a>), |
588cf31d584a
6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents:
9035
diff
changeset
|
977 |
* or if the specified collection is null |
2 | 978 |
* @since 1.2 |
979 |
*/ |
|
42319 | 980 |
public boolean retainAll(Collection<?> c) { |
981 |
Objects.requireNonNull(c); |
|
982 |
return bulkRemove(e -> !c.contains(e)); |
|
983 |
} |
|
984 |
||
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
985 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
986 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
987 |
*/ |
42319 | 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]; |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1032 |
for (i = elementCount = w; i < end; i++) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1033 |
es[i] = null; |
42319 | 1034 |
return true; |
1035 |
} else { |
|
1036 |
if (modCount != expectedModCount) |
|
1037 |
throw new ConcurrentModificationException(); |
|
1038 |
return false; |
|
1039 |
} |
|
2 | 1040 |
} |
1041 |
||
1042 |
/** |
|
1043 |
* Inserts all of the elements in the specified Collection into this |
|
1044 |
* Vector at the specified position. Shifts the element currently at |
|
1045 |
* that position (if any) and any subsequent elements to the right |
|
1046 |
* (increases their indices). The new elements will appear in the Vector |
|
1047 |
* in the order that they are returned by the specified Collection's |
|
1048 |
* iterator. |
|
1049 |
* |
|
1050 |
* @param index index at which to insert the first element from the |
|
1051 |
* specified collection |
|
1052 |
* @param c elements to be inserted into this Vector |
|
1053 |
* @return {@code true} if this Vector changed as a result of the call |
|
1054 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
|
1055 |
* ({@code index < 0 || index > size()}) |
|
1056 |
* @throws NullPointerException if the specified collection is null |
|
1057 |
* @since 1.2 |
|
1058 |
*/ |
|
1059 |
public synchronized boolean addAll(int index, Collection<? extends E> c) { |
|
1060 |
if (index < 0 || index > elementCount) |
|
1061 |
throw new ArrayIndexOutOfBoundsException(index); |
|
1062 |
||
1063 |
Object[] a = c.toArray(); |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1064 |
modCount++; |
2 | 1065 |
int numNew = a.length; |
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1066 |
if (numNew == 0) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1067 |
return false; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1068 |
Object[] elementData = this.elementData; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1069 |
final int s = elementCount; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1070 |
if (numNew > elementData.length - s) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1071 |
elementData = grow(s + numNew); |
2 | 1072 |
|
35383
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1073 |
int numMoved = s - index; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1074 |
if (numMoved > 0) |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1075 |
System.arraycopy(elementData, index, |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1076 |
elementData, index + numNew, |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1077 |
numMoved); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1078 |
System.arraycopy(a, 0, elementData, index, numNew); |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1079 |
elementCount = s + numNew; |
61820dd2360e
8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents:
32108
diff
changeset
|
1080 |
return true; |
2 | 1081 |
} |
1082 |
||
1083 |
/** |
|
1084 |
* Compares the specified Object with this Vector for equality. Returns |
|
1085 |
* true if and only if the specified Object is also a List, both Lists |
|
1086 |
* have the same size, and all corresponding pairs of elements in the two |
|
1087 |
* Lists are <em>equal</em>. (Two elements {@code e1} and |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
1088 |
* {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
31540
diff
changeset
|
1089 |
* In other words, two Lists are defined to be |
2 | 1090 |
* equal if they contain the same elements in the same order. |
1091 |
* |
|
1092 |
* @param o the Object to be compared for equality with this Vector |
|
1093 |
* @return true if the specified Object is equal to this Vector |
|
1094 |
*/ |
|
1095 |
public synchronized boolean equals(Object o) { |
|
1096 |
return super.equals(o); |
|
1097 |
} |
|
1098 |
||
1099 |
/** |
|
1100 |
* Returns the hash code value for this Vector. |
|
1101 |
*/ |
|
1102 |
public synchronized int hashCode() { |
|
1103 |
return super.hashCode(); |
|
1104 |
} |
|
1105 |
||
1106 |
/** |
|
1107 |
* Returns a string representation of this Vector, containing |
|
1108 |
* the String representation of each element. |
|
1109 |
*/ |
|
1110 |
public synchronized String toString() { |
|
1111 |
return super.toString(); |
|
1112 |
} |
|
1113 |
||
1114 |
/** |
|
1115 |
* Returns a view of the portion of this List between fromIndex, |
|
1116 |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are |
|
1117 |
* equal, the returned List is empty.) The returned List is backed by this |
|
1118 |
* List, so changes in the returned List are reflected in this List, and |
|
1119 |
* vice-versa. The returned List supports all of the optional List |
|
1120 |
* operations supported by this List. |
|
1121 |
* |
|
1122 |
* <p>This method eliminates the need for explicit range operations (of |
|
1123 |
* the sort that commonly exist for arrays). Any operation that expects |
|
1124 |
* a List can be used as a range operation by operating on a subList view |
|
1125 |
* instead of a whole List. For example, the following idiom |
|
1126 |
* removes a range of elements from a List: |
|
1127 |
* <pre> |
|
1128 |
* list.subList(from, to).clear(); |
|
1129 |
* </pre> |
|
1130 |
* Similar idioms may be constructed for indexOf and lastIndexOf, |
|
1131 |
* and all of the algorithms in the Collections class can be applied to |
|
1132 |
* a subList. |
|
1133 |
* |
|
1134 |
* <p>The semantics of the List returned by this method become undefined if |
|
1135 |
* the backing list (i.e., this List) is <i>structurally modified</i> in |
|
1136 |
* any way other than via the returned List. (Structural modifications are |
|
1137 |
* those that change the size of the List, or otherwise perturb it in such |
|
1138 |
* a fashion that iterations in progress may yield incorrect results.) |
|
1139 |
* |
|
1140 |
* @param fromIndex low endpoint (inclusive) of the subList |
|
1141 |
* @param toIndex high endpoint (exclusive) of the subList |
|
1142 |
* @return a view of the specified range within this List |
|
1143 |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range |
|
1144 |
* {@code (fromIndex < 0 || toIndex > size)} |
|
1145 |
* @throws IllegalArgumentException if the endpoint indices are out of order |
|
1146 |
* {@code (fromIndex > toIndex)} |
|
1147 |
*/ |
|
1148 |
public synchronized List<E> subList(int fromIndex, int toIndex) { |
|
1149 |
return Collections.synchronizedList(super.subList(fromIndex, toIndex), |
|
1150 |
this); |
|
1151 |
} |
|
1152 |
||
1153 |
/** |
|
1154 |
* Removes from this list all of the elements whose index is between |
|
1155 |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
|
1156 |
* Shifts any succeeding elements to the left (reduces their index). |
|
1157 |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
|
1158 |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
|
1159 |
*/ |
|
1160 |
protected synchronized void removeRange(int fromIndex, int toIndex) { |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1161 |
modCount++; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1162 |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1163 |
} |
2 | 1164 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1165 |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1166 |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1167 |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1168 |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1169 |
es[i] = null; |
2 | 1170 |
} |
1171 |
||
1172 |
/** |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1173 |
* Saves the state of the {@code Vector} instance to a stream |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1174 |
* (that is, serializes it). |
8392
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1175 |
* This method performs synchronization to ensure the consistency |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1176 |
* of the serialized data. |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1177 |
* |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1178 |
* @param s the stream |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1179 |
* @throws java.io.IOException if an I/O error occurs |
2 | 1180 |
*/ |
8392
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1181 |
private void writeObject(java.io.ObjectOutputStream s) |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1182 |
throws java.io.IOException { |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1183 |
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1184 |
final Object[] data; |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1185 |
synchronized (this) { |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1186 |
fields.put("capacityIncrement", capacityIncrement); |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1187 |
fields.put("elementCount", elementCount); |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1188 |
data = elementData.clone(); |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1189 |
} |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1190 |
fields.put("elementData", data); |
3e5784c9f73e
6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
7668
diff
changeset
|
1191 |
s.writeFields(); |
2 | 1192 |
} |
1193 |
||
1194 |
/** |
|
1195 |
* Returns a list iterator over the elements in this list (in proper |
|
1196 |
* sequence), starting at the specified position in the list. |
|
1197 |
* The specified index indicates the first element that would be |
|
1198 |
* returned by an initial call to {@link ListIterator#next next}. |
|
1199 |
* An initial call to {@link ListIterator#previous previous} would |
|
1200 |
* return the element with the specified index minus one. |
|
1201 |
* |
|
1202 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
1203 |
* |
|
1204 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
1205 |
*/ |
|
1206 |
public synchronized ListIterator<E> listIterator(int index) { |
|
1207 |
if (index < 0 || index > elementCount) |
|
1208 |
throw new IndexOutOfBoundsException("Index: "+index); |
|
1209 |
return new ListItr(index); |
|
1210 |
} |
|
1211 |
||
1212 |
/** |
|
1213 |
* Returns a list iterator over the elements in this list (in proper |
|
1214 |
* sequence). |
|
1215 |
* |
|
1216 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
1217 |
* |
|
1218 |
* @see #listIterator(int) |
|
1219 |
*/ |
|
1220 |
public synchronized ListIterator<E> listIterator() { |
|
1221 |
return new ListItr(0); |
|
1222 |
} |
|
1223 |
||
1224 |
/** |
|
1225 |
* Returns an iterator over the elements in this list in proper sequence. |
|
1226 |
* |
|
1227 |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. |
|
1228 |
* |
|
1229 |
* @return an iterator over the elements in this list in proper sequence |
|
1230 |
*/ |
|
1231 |
public synchronized Iterator<E> iterator() { |
|
1232 |
return new Itr(); |
|
1233 |
} |
|
1234 |
||
1235 |
/** |
|
1236 |
* An optimized version of AbstractList.Itr |
|
1237 |
*/ |
|
1238 |
private class Itr implements Iterator<E> { |
|
1239 |
int cursor; // index of next element to return |
|
1240 |
int lastRet = -1; // index of last element returned; -1 if no such |
|
1241 |
int expectedModCount = modCount; |
|
1242 |
||
1243 |
public boolean hasNext() { |
|
1244 |
// Racy but within spec, since modifications are checked |
|
1245 |
// within or after synchronization in next/previous |
|
1246 |
return cursor != elementCount; |
|
1247 |
} |
|
1248 |
||
1249 |
public E next() { |
|
1250 |
synchronized (Vector.this) { |
|
1251 |
checkForComodification(); |
|
1252 |
int i = cursor; |
|
1253 |
if (i >= elementCount) |
|
1254 |
throw new NoSuchElementException(); |
|
1255 |
cursor = i + 1; |
|
1256 |
return elementData(lastRet = i); |
|
1257 |
} |
|
1258 |
} |
|
1259 |
||
1260 |
public void remove() { |
|
1261 |
if (lastRet == -1) |
|
1262 |
throw new IllegalStateException(); |
|
1263 |
synchronized (Vector.this) { |
|
1264 |
checkForComodification(); |
|
1265 |
Vector.this.remove(lastRet); |
|
1266 |
expectedModCount = modCount; |
|
1267 |
} |
|
1268 |
cursor = lastRet; |
|
1269 |
lastRet = -1; |
|
1270 |
} |
|
1271 |
||
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1272 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1273 |
public void forEachRemaining(Consumer<? super E> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1274 |
Objects.requireNonNull(action); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1275 |
synchronized (Vector.this) { |
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17168
diff
changeset
|
1276 |
final int size = elementCount; |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1277 |
int i = cursor; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1278 |
if (i >= size) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1279 |
return; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1280 |
} |
42319 | 1281 |
final Object[] es = elementData; |
1282 |
if (i >= es.length) |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1283 |
throw new ConcurrentModificationException(); |
42319 | 1284 |
while (i < size && modCount == expectedModCount) |
1285 |
action.accept(elementAt(es, i++)); |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1286 |
// update once at end of iteration to reduce heap write traffic |
17432
efdf6eb85a17
8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
17180
diff
changeset
|
1287 |
cursor = i; |
efdf6eb85a17
8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
17180
diff
changeset
|
1288 |
lastRet = i - 1; |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1289 |
checkForComodification(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1290 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1291 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1292 |
|
2 | 1293 |
final void checkForComodification() { |
1294 |
if (modCount != expectedModCount) |
|
1295 |
throw new ConcurrentModificationException(); |
|
1296 |
} |
|
1297 |
} |
|
1298 |
||
1299 |
/** |
|
1300 |
* An optimized version of AbstractList.ListItr |
|
1301 |
*/ |
|
1302 |
final class ListItr extends Itr implements ListIterator<E> { |
|
1303 |
ListItr(int index) { |
|
1304 |
super(); |
|
1305 |
cursor = index; |
|
1306 |
} |
|
1307 |
||
1308 |
public boolean hasPrevious() { |
|
1309 |
return cursor != 0; |
|
1310 |
} |
|
1311 |
||
1312 |
public int nextIndex() { |
|
1313 |
return cursor; |
|
1314 |
} |
|
1315 |
||
1316 |
public int previousIndex() { |
|
1317 |
return cursor - 1; |
|
1318 |
} |
|
1319 |
||
1320 |
public E previous() { |
|
1321 |
synchronized (Vector.this) { |
|
1322 |
checkForComodification(); |
|
1323 |
int i = cursor - 1; |
|
1324 |
if (i < 0) |
|
1325 |
throw new NoSuchElementException(); |
|
1326 |
cursor = i; |
|
1327 |
return elementData(lastRet = i); |
|
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
public void set(E e) { |
|
1332 |
if (lastRet == -1) |
|
1333 |
throw new IllegalStateException(); |
|
1334 |
synchronized (Vector.this) { |
|
1335 |
checkForComodification(); |
|
1336 |
Vector.this.set(lastRet, e); |
|
1337 |
} |
|
1338 |
} |
|
1339 |
||
1340 |
public void add(E e) { |
|
1341 |
int i = cursor; |
|
1342 |
synchronized (Vector.this) { |
|
1343 |
checkForComodification(); |
|
1344 |
Vector.this.add(i, e); |
|
1345 |
expectedModCount = modCount; |
|
1346 |
} |
|
1347 |
cursor = i + 1; |
|
1348 |
lastRet = -1; |
|
1349 |
} |
|
1350 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1351 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1352 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1353 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1354 |
*/ |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1355 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1356 |
public synchronized void forEach(Consumer<? super E> action) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1357 |
Objects.requireNonNull(action); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1358 |
final int expectedModCount = modCount; |
42319 | 1359 |
final Object[] es = elementData; |
1360 |
final int size = elementCount; |
|
1361 |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
|
1362 |
action.accept(elementAt(es, i)); |
|
1363 |
if (modCount != expectedModCount) |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1364 |
throw new ConcurrentModificationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1365 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1366 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1367 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1368 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1369 |
*/ |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1370 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1371 |
public synchronized void replaceAll(UnaryOperator<E> operator) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1372 |
Objects.requireNonNull(operator); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1373 |
final int expectedModCount = modCount; |
42319 | 1374 |
final Object[] es = elementData; |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1375 |
final int size = elementCount; |
42319 | 1376 |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1377 |
es[i] = operator.apply(elementAt(es, i)); |
|
1378 |
if (modCount != expectedModCount) |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1379 |
throw new ConcurrentModificationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1380 |
modCount++; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1381 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1382 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
19074
diff
changeset
|
1383 |
@SuppressWarnings("unchecked") |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1384 |
@Override |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1385 |
public synchronized void sort(Comparator<? super E> c) { |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1386 |
final int expectedModCount = modCount; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1387 |
Arrays.sort((E[]) elementData, 0, elementCount, c); |
42319 | 1388 |
if (modCount != expectedModCount) |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1389 |
throw new ConcurrentModificationException(); |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1390 |
modCount++; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
10419
diff
changeset
|
1391 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1392 |
|
19435
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1393 |
/** |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1394 |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1395 |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1396 |
* list. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1397 |
* |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1398 |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1399 |
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1400 |
* Overriding implementations should document the reporting of additional |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1401 |
* characteristic values. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1402 |
* |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1403 |
* @return a {@code Spliterator} over the elements in this list |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1404 |
* @since 1.8 |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
19208
diff
changeset
|
1405 |
*/ |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1406 |
@Override |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1407 |
public Spliterator<E> spliterator() { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1408 |
return new VectorSpliterator(null, 0, -1, 0); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1409 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1410 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1411 |
/** Similar to ArrayList Spliterator */ |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1412 |
final class VectorSpliterator implements Spliterator<E> { |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1413 |
private Object[] array; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1414 |
private int index; // current index, modified on advance/split |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1415 |
private int fence; // -1 until used; then one past last index |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1416 |
private int expectedModCount; // initialized when fence set |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1417 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1418 |
/** Creates new spliterator covering the given range. */ |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1419 |
VectorSpliterator(Object[] array, int origin, int fence, |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1420 |
int expectedModCount) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1421 |
this.array = array; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1422 |
this.index = origin; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1423 |
this.fence = fence; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1424 |
this.expectedModCount = expectedModCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1425 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1426 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1427 |
private int getFence() { // initialize on first use |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1428 |
int hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1429 |
if ((hi = fence) < 0) { |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1430 |
synchronized (Vector.this) { |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1431 |
array = elementData; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1432 |
expectedModCount = modCount; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1433 |
hi = fence = elementCount; |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1434 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1435 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1436 |
return hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1437 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1438 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1439 |
public Spliterator<E> trySplit() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1440 |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1441 |
return (lo >= mid) ? null : |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1442 |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1443 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1444 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1445 |
@SuppressWarnings("unchecked") |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1446 |
public boolean tryAdvance(Consumer<? super E> action) { |
43520
65d31ea930aa
8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents:
42927
diff
changeset
|
1447 |
Objects.requireNonNull(action); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1448 |
int i; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1449 |
if (getFence() > (i = index)) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1450 |
index = i + 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1451 |
action.accept((E)array[i]); |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1452 |
if (modCount != expectedModCount) |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1453 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1454 |
return true; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1455 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1456 |
return false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1457 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1458 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1459 |
@SuppressWarnings("unchecked") |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1460 |
public void forEachRemaining(Consumer<? super E> action) { |
43520
65d31ea930aa
8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents:
42927
diff
changeset
|
1461 |
Objects.requireNonNull(action); |
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1462 |
final int hi = getFence(); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1463 |
final Object[] a = array; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1464 |
int i; |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1465 |
for (i = index, index = hi; i < hi; i++) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1466 |
action.accept((E) a[i]); |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1467 |
if (modCount != expectedModCount) |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
1468 |
throw new ConcurrentModificationException(); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1469 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1470 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1471 |
public long estimateSize() { |
24255
91f5e4399160
8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents:
24196
diff
changeset
|
1472 |
return getFence() - index; |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1473 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1474 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1475 |
public int characteristics() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1476 |
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1477 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
17166
diff
changeset
|
1478 |
} |
42319 | 1479 |
|
1480 |
void checkInvariants() { |
|
1481 |
// assert elementCount >= 0; |
|
1482 |
// assert elementCount == elementData.length || elementData[elementCount] == null; |
|
1483 |
} |
|
2 | 1484 |
} |