langtools/src/share/classes/com/sun/tools/javac/util/ArrayUtils.java
author briangoetz
Wed, 18 Dec 2013 10:29:25 -0500
changeset 22159 682da512ec17
parent 16549 1bdeedb5446c
permissions -rw-r--r--
8030253: Update langtools to use strings-in-switch 8030262: Update langtools to use foreach loops 8030245: Update langtools to use try-with-resources and multi-catch Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14049
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     1
/*
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14049
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
14049
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     4
 *
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    10
 *
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    15
 * accompanied this code).
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    16
 *
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    20
 *
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    23
 * questions.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    24
 */
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    25
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.util;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    27
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    28
import java.lang.reflect.Array;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    29
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    30
/** <p><b>This is NOT part of any supported API.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    31
 *  If you write code that depends on this, you do so at your own risk.
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    32
 *  This code and its internal interfaces are subject to change or
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    33
 *  deletion without notice.</b>
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    34
 */
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    35
public class ArrayUtils {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    36
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    37
    private static int calculateNewLength(int currentLength, int maxIndex) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    38
        while (currentLength < maxIndex + 1)
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    39
            currentLength = currentLength * 2;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    40
        return currentLength;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    41
    }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    42
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    43
    public static <T> T[] ensureCapacity(T[] array, int maxIndex) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    44
        if (maxIndex < array.length) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    45
            return array;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    46
        } else {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    47
            int newLength = calculateNewLength(array.length, maxIndex);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    48
            @SuppressWarnings("unchecked")
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    49
            T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    50
            System.arraycopy(array, 0, result, 0, array.length);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    51
            return result;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    52
        }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    53
    }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    54
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    55
    public static byte[] ensureCapacity(byte[] array, int maxIndex) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    56
        if (maxIndex < array.length) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    57
            return array;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    58
        } else {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    59
            int newLength = calculateNewLength(array.length, maxIndex);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    60
            byte[] result = new byte[newLength];
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    61
            System.arraycopy(array, 0, result, 0, array.length);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    62
            return result;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    63
        }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    64
    }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    65
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    66
    public static char[] ensureCapacity(char[] array, int maxIndex) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    67
        if (maxIndex < array.length) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    68
            return array;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    69
        } else {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    70
            int newLength = calculateNewLength(array.length, maxIndex);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    71
            char[] result = new char[newLength];
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    72
            System.arraycopy(array, 0, result, 0, array.length);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    73
            return result;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    74
        }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    75
    }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    76
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    77
    public static int[] ensureCapacity(int[] array, int maxIndex) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    78
        if (maxIndex < array.length) {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    79
            return array;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    80
        } else {
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    81
            int newLength = calculateNewLength(array.length, maxIndex);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    82
            int[] result = new int[newLength];
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    83
            System.arraycopy(array, 0, result, 0, array.length);
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    84
            return result;
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    85
        }
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    86
    }
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14049
diff changeset
    87
14049
3207422a0f9b 7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
diff changeset
    88
}