--- jsr166/src/main/java/util/Vector.java 2018/05/05 18:29:53 1.50
+++ jsr166/src/main/java/util/Vector.java 2020/07/24 20:57:26 1.58
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,8 @@ import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
+import jdk.internal.util.ArraysSupport;
+
/**
* The {@code Vector} class implements a growable array of
* objects. Like an array, it contains components that can be
@@ -73,7 +75,7 @@ import java.util.function.UnaryOperator;
*
*
As of the Java 2 platform v1.2, this class was retrofitted to
* implement the {@link List} interface, making it a member of the
- *
+ *
* Java Collections Framework. Unlike the new collection
* implementations, {@code Vector} is synchronized. If a thread-safe
* implementation is not needed, it is recommended to use {@link
@@ -100,6 +102,7 @@ public class Vector
*
* @serial
*/
+ @SuppressWarnings("serial") // Conditionally serializable
protected Object[] elementData;
/**
@@ -122,6 +125,7 @@ public class Vector
protected int capacityIncrement;
/** use serialVersionUID from JDK 1.0.2 for interoperability */
+ // OPENJDK @java.io.Serial
private static final long serialVersionUID = -2767605614048989439L;
/**
@@ -175,12 +179,13 @@ public class Vector
* @since 1.2
*/
public Vector(Collection extends E> c) {
- elementData = c.toArray();
- elementCount = elementData.length;
- // defend against c.toArray (incorrectly) not returning Object[]
- // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
- if (elementData.getClass() != Object[].class)
- elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
+ Object[] a = c.toArray();
+ elementCount = a.length;
+ if (c.getClass() == ArrayList.class) {
+ elementData = a;
+ } else {
+ elementData = Arrays.copyOf(a, elementCount, Object[].class);
+ }
}
/**
@@ -242,14 +247,6 @@ public class Vector
}
/**
- * The maximum size of array to allocate (unless necessary).
- * Some VMs reserve some header words in an array.
- * Attempts to allocate larger arrays may result in
- * OutOfMemoryError: Requested array size exceeds VM limit
- */
- private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
-
- /**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
@@ -257,8 +254,12 @@ public class Vector
* @throws OutOfMemoryError if minCapacity is less than zero
*/
private Object[] grow(int minCapacity) {
- return elementData = Arrays.copyOf(elementData,
- newCapacity(minCapacity));
+ int oldCapacity = elementData.length;
+ int newCapacity = ArraysSupport.newLength(oldCapacity,
+ minCapacity - oldCapacity, /* minimum growth */
+ capacityIncrement > 0 ? capacityIncrement : oldCapacity
+ /* preferred growth */);
+ return elementData = Arrays.copyOf(elementData, newCapacity);
}
private Object[] grow() {
@@ -266,37 +267,6 @@ public class Vector
}
/**
- * Returns a capacity at least as large as the given minimum capacity.
- * Will not return a capacity greater than MAX_ARRAY_SIZE unless
- * the given minimum capacity is greater than MAX_ARRAY_SIZE.
- *
- * @param minCapacity the desired minimum capacity
- * @throws OutOfMemoryError if minCapacity is less than zero
- */
- private int newCapacity(int minCapacity) {
- // overflow-conscious code
- int oldCapacity = elementData.length;
- int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
- capacityIncrement : oldCapacity);
- if (newCapacity - minCapacity <= 0) {
- if (minCapacity < 0) // overflow
- throw new OutOfMemoryError();
- return minCapacity;
- }
- return (newCapacity - MAX_ARRAY_SIZE <= 0)
- ? newCapacity
- : hugeCapacity(minCapacity);
- }
-
- private static int hugeCapacity(int minCapacity) {
- if (minCapacity < 0) // overflow
- throw new OutOfMemoryError();
- return (minCapacity > MAX_ARRAY_SIZE) ?
- Integer.MAX_VALUE :
- MAX_ARRAY_SIZE;
- }
-
- /**
* Sets the size of this vector. If the new size is greater than the
* current size, new {@code null} items are added to the end of
* the vector. If the new size is less than the current size, all
@@ -1190,6 +1160,7 @@ public class Vector
* @throws ClassNotFoundException if the stream contains data
* of a non-existing class
*/
+ // OPENJDK @java.io.Serial
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField gfields = in.readFields();
@@ -1211,6 +1182,7 @@ public class Vector
* @param s the stream
* @throws java.io.IOException if an I/O error occurs
*/
+ // OPENJDK @java.io.Serial
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
final java.io.ObjectOutputStream.PutField fields = s.putFields();
@@ -1411,6 +1383,8 @@ public class Vector
es[i] = operator.apply(elementAt(es, i));
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
+ // TODO(8203662): remove increment of modCount from ...
+ modCount++;
// checkInvariants();
}