< prev index next >

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

Print this page
rev 54801 : [mq]: 8223593-Refactor-code-for-reallocating-storage
   1 /*
   2  * Copyright (c) 1997, 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 /**
  29  * This class provides a skeletal implementation of the {@code Collection}
  30  * interface, to minimize the effort required to implement this interface. <p>
  31  *
  32  * To implement an unmodifiable collection, the programmer needs only to
  33  * extend this class and provide implementations for the {@code iterator} and
  34  * {@code size} methods.  (The iterator returned by the {@code iterator}
  35  * method must implement {@code hasNext} and {@code next}.)<p>
  36  *
  37  * To implement a modifiable collection, the programmer must additionally
  38  * override this class's {@code add} method (which otherwise throws an
  39  * {@code UnsupportedOperationException}), and the iterator returned by the
  40  * {@code iterator} method must additionally implement its {@code remove}
  41  * method.<p>
  42  *
  43  * The programmer should generally provide a void (no argument) and
  44  * {@code Collection} constructor, as per the recommendation in the
  45  * {@code Collection} interface specification.<p>
  46  *
  47  * The documentation for each non-abstract method in this class describes its


 187             if (! it.hasNext()) { // fewer elements than expected
 188                 if (a == r) {
 189                     r[i] = null; // null-terminate
 190                 } else if (a.length < i) {
 191                     return Arrays.copyOf(r, i);
 192                 } else {
 193                     System.arraycopy(r, 0, a, 0, i);
 194                     if (a.length > i) {
 195                         a[i] = null;
 196                     }
 197                 }
 198                 return a;
 199             }
 200             r[i] = (T)it.next();
 201         }
 202         // more elements than expected
 203         return it.hasNext() ? finishToArray(r, it) : r;
 204     }
 205 
 206     /**
 207      * The maximum size of array to allocate.
 208      * Some VMs reserve some header words in an array.
 209      * Attempts to allocate larger arrays may result in
 210      * OutOfMemoryError: Requested array size exceeds VM limit
 211      */
 212     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 213 
 214     /**
 215      * Reallocates the array being used within toArray when the iterator
 216      * returned more elements than expected, and finishes filling it from
 217      * the iterator.
 218      *
 219      * @param r the array, replete with previously stored elements
 220      * @param it the in-progress iterator over this collection
 221      * @return array containing the elements in the given array, plus any
 222      *         further elements returned by the iterator, trimmed to size
 223      */
 224     @SuppressWarnings("unchecked")
 225     private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
 226         int i = r.length;
 227         while (it.hasNext()) {
 228             int cap = r.length;


 229             if (i == cap) {
 230                 int newCap = cap + (cap >> 1) + 1;
 231                 // overflow-conscious code
 232                 if (newCap - MAX_ARRAY_SIZE > 0)
 233                     newCap = hugeCapacity(cap + 1);
 234                 r = Arrays.copyOf(r, newCap);
 235             }
 236             r[i++] = (T)it.next();
 237         }
 238         // trim if overallocated
 239         return (i == r.length) ? r : Arrays.copyOf(r, i);
 240     }
 241 
 242     private static int hugeCapacity(int minCapacity) {
 243         if (minCapacity < 0) // overflow
 244             throw new OutOfMemoryError
 245                 ("Required array size too large");
 246         return (minCapacity > MAX_ARRAY_SIZE) ?
 247             Integer.MAX_VALUE :
 248             MAX_ARRAY_SIZE;
 249     }
 250 
 251     // Modification Operations
 252 
 253     /**
 254      * {@inheritDoc}
 255      *
 256      * @implSpec
 257      * This implementation always throws an
 258      * {@code UnsupportedOperationException}.
 259      *
 260      * @throws UnsupportedOperationException {@inheritDoc}
 261      * @throws ClassCastException            {@inheritDoc}
 262      * @throws NullPointerException          {@inheritDoc}
 263      * @throws IllegalArgumentException      {@inheritDoc}
 264      * @throws IllegalStateException         {@inheritDoc}
 265      */
 266     public boolean add(E e) {
 267         throw new UnsupportedOperationException();
 268     }


   1 /*
   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
   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 jdk.internal.util.ArraysSupport;
  29 
  30 /**
  31  * This class provides a skeletal implementation of the {@code Collection}
  32  * interface, to minimize the effort required to implement this interface. <p>
  33  *
  34  * To implement an unmodifiable collection, the programmer needs only to
  35  * extend this class and provide implementations for the {@code iterator} and
  36  * {@code size} methods.  (The iterator returned by the {@code iterator}
  37  * method must implement {@code hasNext} and {@code next}.)<p>
  38  *
  39  * To implement a modifiable collection, the programmer must additionally
  40  * override this class's {@code add} method (which otherwise throws an
  41  * {@code UnsupportedOperationException}), and the iterator returned by the
  42  * {@code iterator} method must additionally implement its {@code remove}
  43  * method.<p>
  44  *
  45  * The programmer should generally provide a void (no argument) and
  46  * {@code Collection} constructor, as per the recommendation in the
  47  * {@code Collection} interface specification.<p>
  48  *
  49  * The documentation for each non-abstract method in this class describes its


 189             if (! it.hasNext()) { // fewer elements than expected
 190                 if (a == r) {
 191                     r[i] = null; // null-terminate
 192                 } else if (a.length < i) {
 193                     return Arrays.copyOf(r, i);
 194                 } else {
 195                     System.arraycopy(r, 0, a, 0, i);
 196                     if (a.length > i) {
 197                         a[i] = null;
 198                     }
 199                 }
 200                 return a;
 201             }
 202             r[i] = (T)it.next();
 203         }
 204         // more elements than expected
 205         return it.hasNext() ? finishToArray(r, it) : r;
 206     }
 207 
 208     /**








 209      * Reallocates the array being used within toArray when the iterator
 210      * returned more elements than expected, and finishes filling it from
 211      * the iterator.
 212      *
 213      * @param r the array, replete with previously stored elements
 214      * @param it the in-progress iterator over this collection
 215      * @return array containing the elements in the given array, plus any
 216      *         further elements returned by the iterator, trimmed to size
 217      */
 218     @SuppressWarnings("unchecked")
 219     private static <T> T[] finishToArray(T[] r, Iterator<?> it) {


 220         int cap = r.length;
 221         int i = cap;
 222         while (it.hasNext()) {
 223             if (i == cap) {
 224                 cap = ArraysSupport.newCapacity(cap, 1, (cap >> 1) + 1);
 225                 r = Arrays.copyOf(r, cap);



 226             }
 227             r[i++] = (T)it.next();
 228         }
 229         // trim if overallocated
 230         return (i == cap) ? r : Arrays.copyOf(r, i);









 231     }
 232 
 233     // Modification Operations
 234 
 235     /**
 236      * {@inheritDoc}
 237      *
 238      * @implSpec
 239      * This implementation always throws an
 240      * {@code UnsupportedOperationException}.
 241      *
 242      * @throws UnsupportedOperationException {@inheritDoc}
 243      * @throws ClassCastException            {@inheritDoc}
 244      * @throws NullPointerException          {@inheritDoc}
 245      * @throws IllegalArgumentException      {@inheritDoc}
 246      * @throws IllegalStateException         {@inheritDoc}
 247      */
 248     public boolean add(E e) {
 249         throw new UnsupportedOperationException();
 250     }


< prev index next >