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.54 by jsr166, Thu May 2 14:31:30 2019 UTC vs.
Revision 1.55 by jsr166, Wed May 22 17:36:58 2019 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 242 | Line 244 | public class Vector<E>
244      }
245  
246      /**
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    /**
247       * Increases the capacity to ensure that it can hold at least the
248       * number of elements specified by the minimum capacity argument.
249       *
# Line 257 | Line 251 | public class Vector<E>
251       * @throws OutOfMemoryError if minCapacity is less than zero
252       */
253      private Object[] grow(int minCapacity) {
254 <        return elementData = Arrays.copyOf(elementData,
255 <                                           newCapacity(minCapacity));
254 >        int oldCapacity = elementData.length;
255 >        int newCapacity = ArraysSupport.newLength(oldCapacity,
256 >                minCapacity - oldCapacity, /* minimum growth */
257 >                capacityIncrement > 0 ? capacityIncrement : oldCapacity
258 >                                           /* preferred growth */);
259 >        return elementData = Arrays.copyOf(elementData, newCapacity);
260      }
261  
262      private Object[] grow() {
# Line 266 | Line 264 | public class Vector<E>
264      }
265  
266      /**
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    /**
267       * Sets the size of this vector. If the new size is greater than the
268       * current size, new {@code null} items are added to the end of
269       * the vector. If the new size is less than the current size, all

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines