KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > ArrayUtilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import java.util.AbstractList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.RandomAccess JavaDoc;
25
26 /**
27  * Utility methods related to arrays.
28  *
29  * @author Miloslav Metelka
30  * @version 1.00
31  */

32
33 public final class ArrayUtilities {
34
35     private static boolean[] EMPTY_BOOLEAN_ARRAY;
36
37     private static char[] EMPTY_CHAR_ARRAY;
38
39     private static int[] EMPTY_INT_ARRAY;
40
41     private ArrayUtilities() {
42         // no instances
43
}
44
45     public static boolean[] booleanArray(boolean[] oldArray) {
46         return booleanArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1);
47     }
48
49     public static boolean[] booleanArray(boolean[] oldArray, int newSize) {
50         return booleanArray(oldArray, newSize,
51                 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true);
52     }
53     
54     public static boolean[] booleanArray(boolean[] oldArray, int newSize, int copyLen, boolean forwardFill) {
55         boolean[] newArray = new boolean[newSize];
56         if (copyLen > 0)
57             if (forwardFill)
58                 System.arraycopy(oldArray, 0, newArray, 0, copyLen);
59             else // backward fill
60
System.arraycopy(oldArray, oldArray.length - copyLen,
61                         newArray, newSize - copyLen, copyLen);
62         return newArray;
63     }
64
65     public static char[] charArray(char[] oldArray) {
66         return charArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1);
67     }
68     
69     public static char[] charArray(char[] oldArray, int newSize) {
70         return charArray(oldArray, newSize,
71                 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true);
72     }
73     
74     public static char[] charArray(char[] oldArray, int newSize, int copyLen, boolean forwardFill) {
75         char[] newArray = new char[newSize];
76         if (copyLen > 0)
77             if (forwardFill)
78                 System.arraycopy(oldArray, 0, newArray, 0, copyLen);
79             else // backward fill
80
System.arraycopy(oldArray, oldArray.length - copyLen,
81                         newArray, newSize - copyLen, copyLen);
82         return newArray;
83     }
84
85     public static char[] charArray(char[] oldArray, int newSize, int gapStart, int gapLength) {
86         char[] newArray = new char[newSize];
87         if (gapStart > 0)
88             System.arraycopy(oldArray, 0, newArray, 0, gapStart);
89         gapStart += gapLength;
90         gapLength = oldArray.length - gapStart;
91         if (gapLength > 0)
92             System.arraycopy(oldArray, gapStart,
93                         newArray, newSize - gapLength, gapLength);
94         return newArray;
95     }
96
97     public static int[] intArray(int[] oldArray) {
98         return intArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1);
99     }
100
101     public static int[] intArray(int[] oldArray, int newSize) {
102         return intArray(oldArray, newSize,
103                 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true);
104     }
105
106     public static int[] intArray(int[] oldArray, int newSize, int copyLen, boolean forwardFill) {
107         int[] newArray = new int[newSize];
108         if (copyLen > 0)
109             if (forwardFill)
110                 System.arraycopy(oldArray, 0, newArray, 0, copyLen);
111             else // backward fill
112
System.arraycopy(oldArray, oldArray.length - copyLen,
113                         newArray, newSize - copyLen, copyLen);
114         return newArray;
115     }
116     
117     public static int[] intArray(int[] oldArray, int newSize, int gapStart, int gapLength) {
118         int[] newArray = new int[newSize];
119         if (gapStart > 0)
120             System.arraycopy(oldArray, 0, newArray, 0, gapStart);
121         gapStart += gapLength;
122         gapLength = oldArray.length - gapStart;
123         if (gapLength > 0)
124             System.arraycopy(oldArray, gapStart,
125                         newArray, newSize - gapLength, gapLength);
126         return newArray;
127     }
128     
129     public static boolean[] emptyBooleanArray() {
130         if (EMPTY_BOOLEAN_ARRAY == null) { // unsynced intentionally
131
EMPTY_BOOLEAN_ARRAY = new boolean[0];
132         }
133         return EMPTY_BOOLEAN_ARRAY;
134     }
135
136     public static char[] emptyCharArray() {
137         if (EMPTY_CHAR_ARRAY == null) { // unsynced intentionally
138
EMPTY_CHAR_ARRAY = new char[0];
139         }
140         return EMPTY_CHAR_ARRAY;
141     }
142
143     public static int[] emptyIntArray() {
144         if (EMPTY_INT_ARRAY == null) { // unsynced intentionally
145
EMPTY_INT_ARRAY = new int[0];
146         }
147         return EMPTY_INT_ARRAY;
148     }
149
150     public static int digitCount(int number) {
151         return String.valueOf(number).length();
152     }
153
154     public static void appendIndex(StringBuilder JavaDoc sb, int index, int maxDigitCount) {
155         String JavaDoc indexStr = String.valueOf(index);
156         appendSpaces(sb, maxDigitCount - indexStr.length());
157         sb.append(indexStr);
158     }
159
160     public static void appendIndex(StringBuffer JavaDoc sb, int index, int maxDigitCount) {
161         String JavaDoc indexStr = String.valueOf(index);
162         appendSpaces(sb, maxDigitCount - indexStr.length());
163         sb.append(indexStr);
164     }
165
166     public static void appendSpaces(StringBuilder JavaDoc sb, int spaceCount) {
167         while (--spaceCount >= 0) {
168             sb.append(' ');
169         }
170     }
171     
172     public static void appendSpaces(StringBuffer JavaDoc sb, int spaceCount) {
173         while (--spaceCount >= 0) {
174             sb.append(' ');
175         }
176     }
177     
178     public static void appendBracketedIndex(StringBuilder JavaDoc sb, int index, int maxDigitCount) {
179         sb.append('[');
180         appendIndex(sb, index, maxDigitCount);
181         sb.append("]: ");
182     }
183
184     public static void appendBracketedIndex(StringBuffer JavaDoc sb, int index, int maxDigitCount) {
185         sb.append('[');
186         appendIndex(sb, index, maxDigitCount);
187         sb.append("]: ");
188     }
189     
190     /**
191      * Return unmodifiable list for the given array.
192      * <br/>
193      * Unlike <code>Collections.unmodifiableList()</code> this method
194      * does not use any extra wrappers etc.
195      *
196      * @since 1.14
197      */

198     public static <E> List JavaDoc<E> unmodifiableList(E[] array) {
199         return new UnmodifiableList<E>(array);
200     }
201     
202     public static String JavaDoc toString(Object JavaDoc[] array) {
203         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
204         int maxDigitCount = digitCount(array.length);
205         for (int i = 0; i < array.length; i++) {
206             appendBracketedIndex(sb, i, maxDigitCount);
207             sb.append(array[i]);
208             sb.append('\n');
209         }
210         return sb.toString();
211     }
212
213     public static String JavaDoc toString(int[] array) {
214         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
215         int maxDigitCount = digitCount(array.length);
216         for (int i = 0; i < array.length; i++) {
217             appendBracketedIndex(sb, i, maxDigitCount);
218             sb.append(array[i]);
219             sb.append('\n');
220         }
221         return sb.toString();
222     }
223     
224     private static final class UnmodifiableList<E> extends AbstractList JavaDoc<E>
225     implements RandomAccess JavaDoc {
226         
227         private E[] array;
228         
229         UnmodifiableList(E[] array) {
230             this.array = array;
231         }
232         
233         public E get(int index) {
234             if (index >= 0 && index < array.length) {
235                 return array[index];
236             } else {
237                 throw new IndexOutOfBoundsException JavaDoc("index = " + index + ", size = " + array.length); //NOI18N
238
}
239         }
240         
241         public int size() {
242             return array.length;
243         }
244         
245
246         public Object JavaDoc[] toArray() {
247             return array.clone();
248         }
249         
250         public <T> T[] toArray(T[] a) {
251             if (a.length < array.length) {
252                 @SuppressWarnings JavaDoc("unchecked")
253                 T[] aa = (T[])java.lang.reflect.Array.
254                         newInstance(a.getClass().getComponentType(), array.length);
255                 a = aa;
256             }
257             System.arraycopy(array, 0, a, 0, array.length);
258             if (a.length > array.length)
259                 a[array.length] = null;
260             return a;
261         }
262
263     }
264
265 }
266
Popular Tags