ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
(Generate patch)

Comparing jsr166/src/main/java/util/Vector.java (file contents):
Revision 1.50 by jsr166, Sat May 5 18:29:53 2018 UTC vs.
Revision 1.58 by jsr166, Fri Jul 24 20:57:26 2020 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1994, 2018, 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
# Line 32 | Line 32 | 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
# Line 73 | Line 75 | import java.util.function.UnaryOperator;
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}/java/util/package-summary.html#CollectionsFramework">
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
# Line 100 | Line 102 | public class Vector<E>
102       *
103       * @serial
104       */
105 +    @SuppressWarnings("serial") // Conditionally serializable
106      protected Object[] elementData;
107  
108      /**
# Line 122 | Line 125 | public class Vector<E>
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      /**
# Line 175 | Line 179 | public class Vector<E>
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      /**
# Line 242 | Line 247 | public class Vector<E>
247      }
248  
249      /**
245     * The maximum size of array to allocate (unless necessary).
246     * Some VMs reserve some header words in an array.
247     * Attempts to allocate larger arrays may result in
248     * OutOfMemoryError: Requested array size exceeds VM limit
249     */
250    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
251
252    /**
250       * Increases the capacity to ensure that it can hold at least the
251       * number of elements specified by the minimum capacity argument.
252       *
# Line 257 | Line 254 | public class Vector<E>
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() {
# Line 266 | Line 267 | public class Vector<E>
267      }
268  
269      /**
269     * Returns a capacity at least as large as the given minimum capacity.
270     * Will not return a capacity greater than MAX_ARRAY_SIZE unless
271     * the given minimum capacity is greater than MAX_ARRAY_SIZE.
272     *
273     * @param minCapacity the desired minimum capacity
274     * @throws OutOfMemoryError if minCapacity is less than zero
275     */
276    private int newCapacity(int minCapacity) {
277        // overflow-conscious code
278        int oldCapacity = elementData.length;
279        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
280                                         capacityIncrement : oldCapacity);
281        if (newCapacity - minCapacity <= 0) {
282            if (minCapacity < 0) // overflow
283                throw new OutOfMemoryError();
284            return minCapacity;
285        }
286        return (newCapacity - MAX_ARRAY_SIZE <= 0)
287            ? newCapacity
288            : hugeCapacity(minCapacity);
289    }
290
291    private static int hugeCapacity(int minCapacity) {
292        if (minCapacity < 0) // overflow
293            throw new OutOfMemoryError();
294        return (minCapacity > MAX_ARRAY_SIZE) ?
295            Integer.MAX_VALUE :
296            MAX_ARRAY_SIZE;
297    }
298
299    /**
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
# Line 1190 | Line 1160 | public class Vector<E>
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();
# Line 1211 | Line 1182 | public class Vector<E>
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();
# Line 1411 | Line 1383 | public class Vector<E>
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      }
1390  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines