< prev index next >

src/java.base/share/classes/java/util/Vector.java

Print this page
rev 54892 : imported patch 8223593-Refactor-code-for-reallocating-storage
   1 /*
   2  * Copyright (c) 1994, 2018, 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
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  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 /**
  36  * The {@code Vector} class implements a growable array of
  37  * objects. Like an array, it contains components that can be
  38  * accessed using an integer index. However, the size of a
  39  * {@code Vector} can grow or shrink as needed to accommodate
  40  * adding and removing items after the {@code Vector} has been created.
  41  *
  42  * <p>Each vector tries to optimize storage management by maintaining a
  43  * {@code capacity} and a {@code capacityIncrement}. The
  44  * {@code capacity} is always at least as large as the vector
  45  * size; it is usually larger because as components are added to the
  46  * vector, the vector's storage increases in chunks the size of
  47  * {@code capacityIncrement}. An application can increase the
  48  * capacity of a vector before inserting a large number of
  49  * components; this reduces the amount of incremental reallocation.
  50  *
  51  * <p id="fail-fast">
  52  * The iterators returned by this class's {@link #iterator() iterator} and
  53  * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
  54  * if the vector is structurally modified at any time after the iterator is


 225      * {@code minCapacity}, then its capacity is increased by replacing its
 226      * internal data array, kept in the field {@code elementData}, with a
 227      * larger one.  The size of the new data array will be the old size plus
 228      * {@code capacityIncrement}, unless the value of
 229      * {@code capacityIncrement} is less than or equal to zero, in which case
 230      * the new capacity will be twice the old capacity; but if this new size
 231      * is still smaller than {@code minCapacity}, then the new capacity will
 232      * be {@code minCapacity}.
 233      *
 234      * @param minCapacity the desired minimum capacity
 235      */
 236     public synchronized void ensureCapacity(int minCapacity) {
 237         if (minCapacity > 0) {
 238             modCount++;
 239             if (minCapacity > elementData.length)
 240                 grow(minCapacity);
 241         }
 242     }
 243 
 244     /**
 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     /**
 253      * Increases the capacity to ensure that it can hold at least the
 254      * number of elements specified by the minimum capacity argument.
 255      *
 256      * @param minCapacity the desired minimum capacity
 257      * @throws OutOfMemoryError if minCapacity is less than zero
 258      */
 259     private Object[] grow(int minCapacity) {
 260         return elementData = Arrays.copyOf(elementData,
 261                                            newCapacity(minCapacity));




 262     }
 263 
 264     private Object[] grow() {
 265         return grow(elementCount + 1);
 266     }
 267 
 268     /**
 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     /**
 300      * Sets the size of this vector. If the new size is greater than the
 301      * current size, new {@code null} items are added to the end of
 302      * the vector. If the new size is less than the current size, all
 303      * components at index {@code newSize} and greater are discarded.
 304      *
 305      * @param  newSize   the new size of this vector
 306      * @throws ArrayIndexOutOfBoundsException if the new size is negative
 307      */
 308     public synchronized void setSize(int newSize) {
 309         modCount++;
 310         if (newSize > elementData.length)
 311             grow(newSize);
 312         final Object[] es = elementData;
 313         for (int to = elementCount, i = newSize; i < to; i++)
 314             es[i] = null;
 315         elementCount = newSize;
 316     }


   1 /*
   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
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  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
  40  * accessed using an integer index. However, the size of a
  41  * {@code Vector} can grow or shrink as needed to accommodate
  42  * adding and removing items after the {@code Vector} has been created.
  43  *
  44  * <p>Each vector tries to optimize storage management by maintaining a
  45  * {@code capacity} and a {@code capacityIncrement}. The
  46  * {@code capacity} is always at least as large as the vector
  47  * size; it is usually larger because as components are added to the
  48  * vector, the vector's storage increases in chunks the size of
  49  * {@code capacityIncrement}. An application can increase the
  50  * capacity of a vector before inserting a large number of
  51  * components; this reduces the amount of incremental reallocation.
  52  *
  53  * <p id="fail-fast">
  54  * The iterators returned by this class's {@link #iterator() iterator} and
  55  * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
  56  * if the vector is structurally modified at any time after the iterator is


 227      * {@code minCapacity}, then its capacity is increased by replacing its
 228      * internal data array, kept in the field {@code elementData}, with a
 229      * larger one.  The size of the new data array will be the old size plus
 230      * {@code capacityIncrement}, unless the value of
 231      * {@code capacityIncrement} is less than or equal to zero, in which case
 232      * the new capacity will be twice the old capacity; but if this new size
 233      * is still smaller than {@code minCapacity}, then the new capacity will
 234      * be {@code minCapacity}.
 235      *
 236      * @param minCapacity the desired minimum capacity
 237      */
 238     public synchronized void ensureCapacity(int minCapacity) {
 239         if (minCapacity > 0) {
 240             modCount++;
 241             if (minCapacity > elementData.length)
 242                 grow(minCapacity);
 243         }
 244     }
 245 
 246     /**








 247      * Increases the capacity to ensure that it can hold at least the
 248      * number of elements specified by the minimum capacity argument.
 249      *
 250      * @param minCapacity the desired minimum capacity
 251      * @throws OutOfMemoryError if minCapacity is less than zero
 252      */
 253     private Object[] grow(int minCapacity) {
 254         int oldCapacity = elementData.length;
 255         int newCapacity = ArraysSupport.calcLength(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() {
 263         return grow(elementCount + 1);































 264     }
 265 
 266     /**
 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
 270      * components at index {@code newSize} and greater are discarded.
 271      *
 272      * @param  newSize   the new size of this vector
 273      * @throws ArrayIndexOutOfBoundsException if the new size is negative
 274      */
 275     public synchronized void setSize(int newSize) {
 276         modCount++;
 277         if (newSize > elementData.length)
 278             grow(newSize);
 279         final Object[] es = elementData;
 280         for (int to = elementCount, i = newSize; i < to; i++)
 281             es[i] = null;
 282         elementCount = newSize;
 283     }


< prev index next >