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  */
25 
26 package com.sun.tools.javac.util;
27 
28 import java.lang.reflect.Array;
29 
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 {
36 
calculateNewLength(int currentLength, int maxIndex)37     private static int calculateNewLength(int currentLength, int maxIndex) {
38         while (currentLength < maxIndex + 1)
39             currentLength = currentLength * 2;
40         return currentLength;
41     }
42 
ensureCapacity(T[] array, int maxIndex)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     }
54 
ensureCapacity(byte[] array, int maxIndex)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     }
65 
ensureCapacity(char[] array, int maxIndex)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     }
76 
ensureCapacity(int[] array, int maxIndex)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     }
87 
88 }
89