KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ArrayUtil


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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