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

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.62 by jsr166, Tue May 22 15:51:31 2018 UTC vs.
Revision 1.69 by jsr166, Fri Aug 30 18:05:39 2019 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1997, 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 28 | Line 28 | package java.util;
28   import java.util.function.Consumer;
29   import java.util.function.Predicate;
30   import java.util.function.UnaryOperator;
31 < import jdk.internal.misc.SharedSecrets;
31 > // OPENJDK import jdk.internal.access.SharedSecrets;
32 > import jdk.internal.util.ArraysSupport;
33  
34   /**
35   * Resizable-array implementation of the {@code List} interface.  Implements
# Line 92 | Line 93 | import jdk.internal.misc.SharedSecrets;
93   * should be used only to detect bugs.</i>
94   *
95   * <p>This class is a member of the
96 < * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
96 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
97   * Java Collections Framework</a>.
98   *
99   * @param <E> the type of elements in this list
# Line 108 | Line 109 | import jdk.internal.misc.SharedSecrets;
109   public class ArrayList<E> extends AbstractList<E>
110          implements List<E>, RandomAccess, Cloneable, java.io.Serializable
111   {
112 +    // OPENJDK @java.io.Serial
113      private static final long serialVersionUID = 8683452581122892189L;
114  
115      /**
# Line 219 | Line 221 | public class ArrayList<E> extends Abstra
221      }
222  
223      /**
222     * The maximum size of array to allocate (unless necessary).
223     * Some VMs reserve some header words in an array.
224     * Attempts to allocate larger arrays may result in
225     * OutOfMemoryError: Requested array size exceeds VM limit
226     */
227    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
228
229    /**
224       * Increases the capacity to ensure that it can hold at least the
225       * number of elements specified by the minimum capacity argument.
226       *
# Line 234 | Line 228 | public class ArrayList<E> extends Abstra
228       * @throws OutOfMemoryError if minCapacity is less than zero
229       */
230      private Object[] grow(int minCapacity) {
231 <        return elementData = Arrays.copyOf(elementData,
232 <                                           newCapacity(minCapacity));
231 >        int oldCapacity = elementData.length;
232 >        if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
233 >            int newCapacity = ArraysSupport.newLength(oldCapacity,
234 >                    minCapacity - oldCapacity, /* minimum growth */
235 >                    oldCapacity >> 1           /* preferred growth */);
236 >            return elementData = Arrays.copyOf(elementData, newCapacity);
237 >        } else {
238 >            return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
239 >        }
240      }
241  
242      private Object[] grow() {
# Line 243 | Line 244 | public class ArrayList<E> extends Abstra
244      }
245  
246      /**
246     * Returns a capacity at least as large as the given minimum capacity.
247     * Returns the current capacity increased by 50% if that suffices.
248     * Will not return a capacity greater than MAX_ARRAY_SIZE unless
249     * the given minimum capacity is greater than MAX_ARRAY_SIZE.
250     *
251     * @param minCapacity the desired minimum capacity
252     * @throws OutOfMemoryError if minCapacity is less than zero
253     */
254    private int newCapacity(int minCapacity) {
255        // overflow-conscious code
256        int oldCapacity = elementData.length;
257        int newCapacity = oldCapacity + (oldCapacity >> 1);
258        if (newCapacity - minCapacity <= 0) {
259            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
260                return Math.max(DEFAULT_CAPACITY, minCapacity);
261            if (minCapacity < 0) // overflow
262                throw new OutOfMemoryError();
263            return minCapacity;
264        }
265        return (newCapacity - MAX_ARRAY_SIZE <= 0)
266            ? newCapacity
267            : hugeCapacity(minCapacity);
268    }
269
270    private static int hugeCapacity(int minCapacity) {
271        if (minCapacity < 0) // overflow
272            throw new OutOfMemoryError();
273        return (minCapacity > MAX_ARRAY_SIZE)
274            ? Integer.MAX_VALUE
275            : MAX_ARRAY_SIZE;
276    }
277
278    /**
247       * Returns the number of elements in this list.
248       *
249       * @return the number of elements in this list
# Line 571 | Line 539 | public class ArrayList<E> extends Abstra
539          if (to > es.length) {
540              throw new ConcurrentModificationException();
541          }
542 <        Iterator<?> oit = other.iterator();
542 >        var oit = other.iterator();
543          for (; from < to; from++) {
544              if (!oit.hasNext() || !Objects.equals(es[from], oit.next())) {
545                  return false;
# Line 888 | Line 856 | public class ArrayList<E> extends Abstra
856       *             instance is emitted (int), followed by all of its elements
857       *             (each an {@code Object}) in the proper order.
858       */
859 +    // OPENJDK @java.io.Serial
860      private void writeObject(java.io.ObjectOutputStream s)
861          throws java.io.IOException {
862          // Write out element count, and any hidden stuff
# Line 915 | Line 884 | public class ArrayList<E> extends Abstra
884       *         could not be found
885       * @throws java.io.IOException if an I/O error occurs
886       */
887 +    // OPENJDK @java.io.Serial
888      private void readObject(java.io.ObjectInputStream s)
889          throws java.io.IOException, ClassNotFoundException {
890  
# Line 926 | Line 896 | public class ArrayList<E> extends Abstra
896  
897          if (size > 0) {
898              // like clone(), allocate array based upon size not capacity
899 <            SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
899 >            jsr166.Platform.checkArray(s, Object[].class, size);
900              Object[] elements = new Object[size];
901  
902              // Read in all elements in the proper order.
# Line 1737 | Line 1707 | public class ArrayList<E> extends Abstra
1707      @Override
1708      public void replaceAll(UnaryOperator<E> operator) {
1709          replaceAllRange(operator, 0, size);
1710 +        // TODO(8203662): remove increment of modCount from ...
1711          modCount++;
1712      }
1713  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines