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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1339
0e5899f09dab
child 1591
dc8b7aa7cef3
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial