jjg@1339: /* vromero@1591: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1339: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1339: * jjg@1339: * This code is free software; you can redistribute it and/or modify it jjg@1339: * under the terms of the GNU General Public License version 2 only, as jjg@1339: * published by the Free Software Foundation. Oracle designates this jjg@1339: * particular file as subject to the "Classpath" exception as provided jjg@1339: * by Oracle in the LICENSE file that accompanied this code. jjg@1339: * jjg@1339: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1339: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1339: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1339: * version 2 for more details (a copy is included in the LICENSE file that jjg@1339: * accompanied this code). jjg@1339: * jjg@1339: * You should have received a copy of the GNU General Public License version jjg@1339: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1339: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1339: * jjg@1339: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1339: * or visit www.oracle.com if you need additional information or have any jjg@1339: * questions. jjg@1339: */ jjg@1339: jjg@1339: package com.sun.tools.javac.util; jjg@1339: jjg@1339: import java.lang.reflect.Array; jjg@1339: jjg@1339: /**

This is NOT part of any supported API. jjg@1339: * If you write code that depends on this, you do so at your own risk. jjg@1339: * This code and its internal interfaces are subject to change or jjg@1339: * deletion without notice. jjg@1339: */ jjg@1339: public class ArrayUtils { jjg@1339: jjg@1339: private static int calculateNewLength(int currentLength, int maxIndex) { jjg@1339: while (currentLength < maxIndex + 1) jjg@1339: currentLength = currentLength * 2; jjg@1339: return currentLength; jjg@1339: } jjg@1339: jjg@1339: public static T[] ensureCapacity(T[] array, int maxIndex) { jjg@1339: if (maxIndex < array.length) { jjg@1339: return array; jjg@1339: } else { jjg@1339: int newLength = calculateNewLength(array.length, maxIndex); jjg@1339: @SuppressWarnings("unchecked") jjg@1339: T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength); jjg@1339: System.arraycopy(array, 0, result, 0, array.length); jjg@1339: return result; jjg@1339: } jjg@1339: } jjg@1339: jjg@1339: public static byte[] ensureCapacity(byte[] array, int maxIndex) { jjg@1339: if (maxIndex < array.length) { jjg@1339: return array; jjg@1339: } else { jjg@1339: int newLength = calculateNewLength(array.length, maxIndex); jjg@1339: byte[] result = new byte[newLength]; jjg@1339: System.arraycopy(array, 0, result, 0, array.length); jjg@1339: return result; jjg@1339: } jjg@1339: } jjg@1339: jjg@1339: public static char[] ensureCapacity(char[] array, int maxIndex) { jjg@1339: if (maxIndex < array.length) { jjg@1339: return array; jjg@1339: } else { jjg@1339: int newLength = calculateNewLength(array.length, maxIndex); jjg@1339: char[] result = new char[newLength]; jjg@1339: System.arraycopy(array, 0, result, 0, array.length); jjg@1339: return result; jjg@1339: } jjg@1339: } jjg@1339: jjg@1339: public static int[] ensureCapacity(int[] array, int maxIndex) { jjg@1339: if (maxIndex < array.length) { jjg@1339: return array; jjg@1339: } else { jjg@1339: int newLength = calculateNewLength(array.length, maxIndex); jjg@1339: int[] result = new int[newLength]; jjg@1339: System.arraycopy(array, 0, result, 0, array.length); jjg@1339: return result; jjg@1339: } jjg@1339: } vromero@1591: jjg@1339: }