< prev index next >

src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 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


 155      */
 156     public void trimToSize() {
 157         modCount++;
 158         int oldCapacity = elementData.length;
 159         if (size < oldCapacity) {
 160             elementData = Arrays.copyOf(elementData, size);
 161         }
 162     }
 163 
 164     /**
 165      * Increases the capacity of this {@code IdentityArrayList} instance, if
 166      * necessary, to ensure that it can hold at least the number of elements
 167      * specified by the minimum capacity argument.
 168      *
 169      * @param   minCapacity   the desired minimum capacity
 170      */
 171     public void ensureCapacity(int minCapacity) {
 172         modCount++;
 173         int oldCapacity = elementData.length;
 174         if (minCapacity > oldCapacity) {
 175             Object oldData[] = elementData;
 176             int newCapacity = (oldCapacity * 3)/2 + 1;
 177             if (newCapacity < minCapacity)
 178                 newCapacity = minCapacity;
 179             // minCapacity is usually close to size, so this is a win:
 180             elementData = Arrays.copyOf(elementData, newCapacity);
 181         }
 182     }
 183 
 184     /**
 185      * Returns the number of elements in this list.
 186      *
 187      * @return the number of elements in this list
 188      */
 189     public int size() {
 190         return size;
 191     }
 192 
 193     /**
 194      * Returns {@code true} if this list contains no elements.
 195      *


   1 /*
   2  * Copyright (c) 2007, 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


 155      */
 156     public void trimToSize() {
 157         modCount++;
 158         int oldCapacity = elementData.length;
 159         if (size < oldCapacity) {
 160             elementData = Arrays.copyOf(elementData, size);
 161         }
 162     }
 163 
 164     /**
 165      * Increases the capacity of this {@code IdentityArrayList} instance, if
 166      * necessary, to ensure that it can hold at least the number of elements
 167      * specified by the minimum capacity argument.
 168      *
 169      * @param   minCapacity   the desired minimum capacity
 170      */
 171     public void ensureCapacity(int minCapacity) {
 172         modCount++;
 173         int oldCapacity = elementData.length;
 174         if (minCapacity > oldCapacity) {
 175             Object[] oldData = elementData;
 176             int newCapacity = (oldCapacity * 3)/2 + 1;
 177             if (newCapacity < minCapacity)
 178                 newCapacity = minCapacity;
 179             // minCapacity is usually close to size, so this is a win:
 180             elementData = Arrays.copyOf(elementData, newCapacity);
 181         }
 182     }
 183 
 184     /**
 185      * Returns the number of elements in this list.
 186      *
 187      * @return the number of elements in this list
 188      */
 189     public int size() {
 190         return size;
 191     }
 192 
 193     /**
 194      * Returns {@code true} if this list contains no elements.
 195      *


< prev index next >