KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ArrayUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 public class ArrayUtil
22 {
23     public static final boolean[] EMPTY_BOOLEAN_ARRAY =
24     {
25     }
26     ;
27     public static final char[] EMPTY_CHAR_ARRAY =
28     {
29     }
30     ;
31     public static final byte[] EMPTY_BYTE_ARRAY =
32     {
33     }
34     ;
35     public static final short[] EMPTY_SHORT_ARRAY =
36     {
37     }
38     ;
39     public static final int[] EMPTY_INT_ARRAY =
40     {
41     }
42     ;
43     public static final long[] EMPTY_LONG_ARRAY =
44     {
45     }
46     ;
47     public static final float[] EMPTY_FLOAT_ARRAY =
48     {
49     }
50     ;
51     public static final double[] EMPTY_DOUBLE_ARRAY =
52     {
53     }
54     ;
55     public static final Class[] EMPTY_CLASS_ARRAY =
56     {
57     }
58     ;
59     public static final Object[] EMPTY_OBJECT_ARRAY =
60     {
61     }
62     ;
63     public static final String[] EMPTY_STRING_ARRAY =
64     {
65     }
66     ;
67
68     public static byte[] copy(byte[] x)
69     {
70         return getBytes(x, 0, x.length);
71     }
72
73     public static byte[] concat(byte[] x, byte[] y)
74     {
75         byte[] z = new byte[x.length + y.length];
76         System.arraycopy(x, 0, z, 0, x.length);
77         System.arraycopy(y, 0, z, x.length, y.length);
78         return z;
79     }
80
81     public static byte[] getBytes(byte[] x, int offset, int length)
82     {
83         byte[] y = new byte[length];
84         System.arraycopy(x, offset, y, 0, length);
85         return y;
86     }
87
88     public static int indexOf(byte[] x, byte b)
89     {
90         return indexOf(x, b, 0);
91     }
92
93     public static int indexOf(byte[] x, byte b, int startOffset)
94     {
95         int n = x.length;
96         for (int i = startOffset; i < n; i++)
97         {
98             if (x[i] == b)
99             {
100                 return i;
101             }
102         }
103         return -1;
104     }
105
106     public static boolean[] newBooleanArray(int size, boolean[] init)
107     {
108         boolean[] array = new boolean[size];
109         if (init != null)
110         {
111             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
112         }
113         return array;
114     }
115
116     public static char[] newCharArray(int size, char[] init)
117     {
118         char[] array = new char[size];
119         if (init != null)
120         {
121             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
122         }
123         return array;
124     }
125
126     public static byte[] newByteArray(int size, byte[] init)
127     {
128         byte[] array = new byte[size];
129         if (init != null)
130         {
131             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
132         }
133         return array;
134     }
135
136     public static short[] newShortArray(int size, short[] init)
137     {
138         short[] array = new short[size];
139         if (init != null)
140         {
141             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
142         }
143         return array;
144     }
145
146     public static int[] newIntArray(int size, int[] init)
147     {
148         int[] array = new int[size];
149         if (init != null)
150         {
151             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
152         }
153         return array;
154     }
155
156     public static long[] newLongArray(int size, long[] init)
157     {
158         long[] array = new long[size];
159         if (init != null)
160         {
161             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
162         }
163         return array;
164     }
165
166     public static float[] newFloatArray(int size, float[] init)
167     {
168         float[] array = new float[size];
169         if (init != null)
170         {
171             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
172         }
173         return array;
174     }
175
176     public static double[] newDoubleArray(int size, double[] init)
177     {
178         double[] array = new double[size];
179         if (init != null)
180         {
181             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
182         }
183         return array;
184     }
185
186     public static Object[] newObjectArray(int size, Object[] init)
187     {
188         Object[] array = new Object[size];
189         if (init != null)
190         {
191             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
192         }
193         return array;
194     }
195
196     public static Object[] newObjectArray(int size, Object[] init, Class type)
197     {
198         Object[] array = (Object[])java.lang.reflect.Array.newInstance(type, size);
199         if (init != null)
200         {
201             System.arraycopy(init, 0, array, 0, Math.min(size, init.length));
202         }
203         return array;
204     }
205 }
206
Popular Tags