1 |
|
/* |
2 |
< |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.io.IOException; |
29 |
+ |
import java.io.ObjectInputStream; |
30 |
+ |
import java.io.StreamCorruptedException; |
31 |
|
import java.util.function.Consumer; |
32 |
|
import java.util.function.Predicate; |
33 |
|
import java.util.function.UnaryOperator; |
34 |
|
|
35 |
+ |
import jdk.internal.util.ArraysSupport; |
36 |
+ |
|
37 |
|
/** |
38 |
|
* The {@code Vector} class implements a growable array of |
39 |
|
* objects. Like an array, it contains components that can be |
75 |
|
* |
76 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
77 |
|
* implement the {@link List} interface, making it a member of the |
78 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
78 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
79 |
|
* Java Collections Framework</a>. Unlike the new collection |
80 |
|
* implementations, {@code Vector} is synchronized. If a thread-safe |
81 |
|
* implementation is not needed, it is recommended to use {@link |
102 |
|
* |
103 |
|
* @serial |
104 |
|
*/ |
105 |
+ |
@SuppressWarnings("serial") // Conditionally serializable |
106 |
|
protected Object[] elementData; |
107 |
|
|
108 |
|
/** |
125 |
|
protected int capacityIncrement; |
126 |
|
|
127 |
|
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
128 |
+ |
// OPENJDK @java.io.Serial |
129 |
|
private static final long serialVersionUID = -2767605614048989439L; |
130 |
|
|
131 |
|
/** |
179 |
|
* @since 1.2 |
180 |
|
*/ |
181 |
|
public Vector(Collection<? extends E> c) { |
182 |
< |
elementData = c.toArray(); |
183 |
< |
elementCount = elementData.length; |
184 |
< |
// defend against c.toArray (incorrectly) not returning Object[] |
185 |
< |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
186 |
< |
if (elementData.getClass() != Object[].class) |
187 |
< |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); |
182 |
> |
Object[] a = c.toArray(); |
183 |
> |
elementCount = a.length; |
184 |
> |
if (c.getClass() == ArrayList.class) { |
185 |
> |
elementData = a; |
186 |
> |
} else { |
187 |
> |
elementData = Arrays.copyOf(a, elementCount, Object[].class); |
188 |
> |
} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
247 |
|
} |
248 |
|
|
249 |
|
/** |
242 |
– |
* The maximum size of array to allocate (unless necessary). |
243 |
– |
* Some VMs reserve some header words in an array. |
244 |
– |
* Attempts to allocate larger arrays may result in |
245 |
– |
* OutOfMemoryError: Requested array size exceeds VM limit |
246 |
– |
*/ |
247 |
– |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
248 |
– |
|
249 |
– |
/** |
250 |
|
* Increases the capacity to ensure that it can hold at least the |
251 |
|
* number of elements specified by the minimum capacity argument. |
252 |
|
* |
254 |
|
* @throws OutOfMemoryError if minCapacity is less than zero |
255 |
|
*/ |
256 |
|
private Object[] grow(int minCapacity) { |
257 |
< |
return elementData = Arrays.copyOf(elementData, |
258 |
< |
newCapacity(minCapacity)); |
257 |
> |
int oldCapacity = elementData.length; |
258 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
259 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
260 |
> |
capacityIncrement > 0 ? capacityIncrement : oldCapacity |
261 |
> |
/* preferred growth */); |
262 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
263 |
|
} |
264 |
|
|
265 |
|
private Object[] grow() { |
267 |
|
} |
268 |
|
|
269 |
|
/** |
266 |
– |
* Returns a capacity at least as large as the given minimum capacity. |
267 |
– |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
268 |
– |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
269 |
– |
* |
270 |
– |
* @param minCapacity the desired minimum capacity |
271 |
– |
* @throws OutOfMemoryError if minCapacity is less than zero |
272 |
– |
*/ |
273 |
– |
private int newCapacity(int minCapacity) { |
274 |
– |
// overflow-conscious code |
275 |
– |
int oldCapacity = elementData.length; |
276 |
– |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
277 |
– |
capacityIncrement : oldCapacity); |
278 |
– |
if (newCapacity - minCapacity <= 0) { |
279 |
– |
if (minCapacity < 0) // overflow |
280 |
– |
throw new OutOfMemoryError(); |
281 |
– |
return minCapacity; |
282 |
– |
} |
283 |
– |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
284 |
– |
? newCapacity |
285 |
– |
: hugeCapacity(minCapacity); |
286 |
– |
} |
287 |
– |
|
288 |
– |
private static int hugeCapacity(int minCapacity) { |
289 |
– |
if (minCapacity < 0) // overflow |
290 |
– |
throw new OutOfMemoryError(); |
291 |
– |
return (minCapacity > MAX_ARRAY_SIZE) ? |
292 |
– |
Integer.MAX_VALUE : |
293 |
– |
MAX_ARRAY_SIZE; |
294 |
– |
} |
295 |
– |
|
296 |
– |
/** |
270 |
|
* Sets the size of this vector. If the new size is greater than the |
271 |
|
* current size, new {@code null} items are added to the end of |
272 |
|
* the vector. If the new size is less than the current size, all |
280 |
|
if (newSize > elementData.length) |
281 |
|
grow(newSize); |
282 |
|
final Object[] es = elementData; |
283 |
< |
for (int to = elementCount, i = elementCount = newSize; i < to; i++) |
283 |
> |
for (int to = elementCount, i = newSize; i < to; i++) |
284 |
|
es[i] = null; |
285 |
+ |
elementCount = newSize; |
286 |
|
} |
287 |
|
|
288 |
|
/** |
959 |
|
return bulkRemove(e -> !c.contains(e)); |
960 |
|
} |
961 |
|
|
962 |
+ |
/** |
963 |
+ |
* @throws NullPointerException {@inheritDoc} |
964 |
+ |
*/ |
965 |
|
@Override |
966 |
|
public boolean removeIf(Predicate<? super E> filter) { |
967 |
|
Objects.requireNonNull(filter); |
1000 |
|
setBit(deathRow, i - beg); |
1001 |
|
if (modCount != expectedModCount) |
1002 |
|
throw new ConcurrentModificationException(); |
1026 |
– |
expectedModCount++; |
1003 |
|
modCount++; |
1004 |
|
int w = beg; |
1005 |
|
for (i = beg; i < end; i++) |
1150 |
|
} |
1151 |
|
|
1152 |
|
/** |
1153 |
+ |
* Loads a {@code Vector} instance from a stream |
1154 |
+ |
* (that is, deserializes it). |
1155 |
+ |
* This method performs checks to ensure the consistency |
1156 |
+ |
* of the fields. |
1157 |
+ |
* |
1158 |
+ |
* @param in the stream |
1159 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1160 |
+ |
* @throws ClassNotFoundException if the stream contains data |
1161 |
+ |
* of a non-existing class |
1162 |
+ |
*/ |
1163 |
+ |
// OPENJDK @java.io.Serial |
1164 |
+ |
private void readObject(ObjectInputStream in) |
1165 |
+ |
throws IOException, ClassNotFoundException { |
1166 |
+ |
ObjectInputStream.GetField gfields = in.readFields(); |
1167 |
+ |
int count = gfields.get("elementCount", 0); |
1168 |
+ |
Object[] data = (Object[])gfields.get("elementData", null); |
1169 |
+ |
if (count < 0 || data == null || count > data.length) { |
1170 |
+ |
throw new StreamCorruptedException("Inconsistent vector internals"); |
1171 |
+ |
} |
1172 |
+ |
elementCount = count; |
1173 |
+ |
elementData = data.clone(); |
1174 |
+ |
} |
1175 |
+ |
|
1176 |
+ |
/** |
1177 |
|
* Saves the state of the {@code Vector} instance to a stream |
1178 |
|
* (that is, serializes it). |
1179 |
|
* This method performs synchronization to ensure the consistency |
1182 |
|
* @param s the stream |
1183 |
|
* @throws java.io.IOException if an I/O error occurs |
1184 |
|
*/ |
1185 |
+ |
// OPENJDK @java.io.Serial |
1186 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1187 |
|
throws java.io.IOException { |
1188 |
|
final java.io.ObjectOutputStream.PutField fields = s.putFields(); |
1354 |
|
} |
1355 |
|
} |
1356 |
|
|
1357 |
+ |
/** |
1358 |
+ |
* @throws NullPointerException {@inheritDoc} |
1359 |
+ |
*/ |
1360 |
|
@Override |
1361 |
|
public synchronized void forEach(Consumer<? super E> action) { |
1362 |
|
Objects.requireNonNull(action); |
1370 |
|
// checkInvariants(); |
1371 |
|
} |
1372 |
|
|
1373 |
+ |
/** |
1374 |
+ |
* @throws NullPointerException {@inheritDoc} |
1375 |
+ |
*/ |
1376 |
|
@Override |
1377 |
|
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1378 |
|
Objects.requireNonNull(operator); |
1383 |
|
es[i] = operator.apply(elementAt(es, i)); |
1384 |
|
if (modCount != expectedModCount) |
1385 |
|
throw new ConcurrentModificationException(); |
1386 |
+ |
// TODO(8203662): remove increment of modCount from ... |
1387 |
|
modCount++; |
1388 |
|
// checkInvariants(); |
1389 |
|
} |
1424 |
|
private int fence; // -1 until used; then one past last index |
1425 |
|
private int expectedModCount; // initialized when fence set |
1426 |
|
|
1427 |
< |
/** Create new spliterator covering the given range */ |
1427 |
> |
/** Creates new spliterator covering the given range. */ |
1428 |
|
VectorSpliterator(Object[] array, int origin, int fence, |
1429 |
|
int expectedModCount) { |
1430 |
|
this.array = array; |
1453 |
|
|
1454 |
|
@SuppressWarnings("unchecked") |
1455 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1456 |
+ |
Objects.requireNonNull(action); |
1457 |
|
int i; |
1449 |
– |
if (action == null) |
1450 |
– |
throw new NullPointerException(); |
1458 |
|
if (getFence() > (i = index)) { |
1459 |
|
index = i + 1; |
1460 |
|
action.accept((E)array[i]); |
1467 |
|
|
1468 |
|
@SuppressWarnings("unchecked") |
1469 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1470 |
< |
if (action == null) |
1464 |
< |
throw new NullPointerException(); |
1470 |
> |
Objects.requireNonNull(action); |
1471 |
|
final int hi = getFence(); |
1472 |
|
final Object[] a = array; |
1473 |
|
int i; |