src/share/classes/com/sun/tools/javac/util/ArrayUtils.java

Tue, 19 Feb 2013 17:53:16 +0000

author
vromero
date
Tue, 19 Feb 2013 17:53:16 +0000
changeset 1591
dc8b7aa7cef3
parent 1339
0e5899f09dab
child 1637
2e21ecd7a5ad
permissions
-rw-r--r--

8006212: javac, convert jtreg tests from shell script to java
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2013, 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  */
    26 package com.sun.tools.javac.util;
    28 import java.lang.reflect.Array;
    30 /** <p><b>This is NOT part of any supported API.
    31  *  If you write code that depends on this, you do so at your own risk.
    32  *  This code and its internal interfaces are subject to change or
    33  *  deletion without notice.</b>
    34  */
    35 public class ArrayUtils {
    37     private static int calculateNewLength(int currentLength, int maxIndex) {
    38         while (currentLength < maxIndex + 1)
    39             currentLength = currentLength * 2;
    40         return currentLength;
    41     }
    43     public static <T> T[] ensureCapacity(T[] array, int maxIndex) {
    44         if (maxIndex < array.length) {
    45             return array;
    46         } else {
    47             int newLength = calculateNewLength(array.length, maxIndex);
    48             @SuppressWarnings("unchecked")
    49             T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength);
    50             System.arraycopy(array, 0, result, 0, array.length);
    51             return result;
    52         }
    53     }
    55     public static byte[] ensureCapacity(byte[] array, int maxIndex) {
    56         if (maxIndex < array.length) {
    57             return array;
    58         } else {
    59             int newLength = calculateNewLength(array.length, maxIndex);
    60             byte[] result = new byte[newLength];
    61             System.arraycopy(array, 0, result, 0, array.length);
    62             return result;
    63         }
    64     }
    66     public static char[] ensureCapacity(char[] array, int maxIndex) {
    67         if (maxIndex < array.length) {
    68             return array;
    69         } else {
    70             int newLength = calculateNewLength(array.length, maxIndex);
    71             char[] result = new char[newLength];
    72             System.arraycopy(array, 0, result, 0, array.length);
    73             return result;
    74         }
    75     }
    77     public static int[] ensureCapacity(int[] array, int maxIndex) {
    78         if (maxIndex < array.length) {
    79             return array;
    80         } else {
    81             int newLength = calculateNewLength(array.length, maxIndex);
    82             int[] result = new int[newLength];
    83             System.arraycopy(array, 0, result, 0, array.length);
    84             return result;
    85         }
    86     }
    88     public static <T> T[] concat(T[] anArr, T[] anotherArr) {
    89         int newLength = anArr.length + anotherArr.length;
    90         @SuppressWarnings("unchecked")
    91         T[] result = (T[]) Array.newInstance(anArr.getClass().getComponentType(), newLength);
    92         System.arraycopy(anArr, 0, result, 0, anArr.length);
    93         System.arraycopy(anotherArr, 0, result, anArr.length, anotherArr.length);
    94         return result;
    95     }
    97     @SuppressWarnings("unchecked")
    98     public static <T> T[] concatOpen(T[] anArr, T... anotherArr) {
    99         return concat(anArr, anotherArr);
   100     }
   101 }

mercurial