KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > ArrayUtils


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

16 package org.apache.myfaces.util;
17
18 import java.lang.reflect.Array JavaDoc;
19
20
21 /**
22  * Utility class for managing arrays
23  *
24  * $Log: ArrayUtils.java,v $
25  * Revision 1.3 2004/10/13 11:51:01 matze
26  * renamed packages to org.apache
27  *
28  * Revision 1.2 2004/07/01 22:01:12 mwessendorf
29  * ASF switch
30  *
31  * Revision 1.1 2004/03/29 14:57:01 manolito
32  * refactoring for implementation and non-standard component split
33  *
34  * Revision 1.8 2004/03/25 12:41:19 manolito
35  * convenient constants for empty arrays
36  *
37  * @author Anton Koinov (latest modification by $Author: matze $)
38  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:51:01 $
39  */

40 public class ArrayUtils
41 {
42     public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
43     public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
44
45     //~ Constructors -------------------------------------------------------------------------------
46

47     protected ArrayUtils()
48     {
49         // hide from public access
50
}
51
52     //~ Methods ------------------------------------------------------------------------------------
53

54     public static Class JavaDoc commonClass(Class JavaDoc c1, Class JavaDoc c2)
55     {
56         if (c1 == c2)
57         {
58             return c1;
59         }
60
61         if ((c1 == Object JavaDoc.class) || c1.isAssignableFrom(c2))
62         {
63             return c1;
64         }
65
66         if (c2.isAssignableFrom(c1))
67         {
68             return c2;
69         }
70
71         if (c1.isPrimitive() || c2.isPrimitive())
72         {
73             // REVISIT: we could try to autoconvert to Object or something appropriate
74
throw new IllegalArgumentException JavaDoc("incompatible types " + c1 + " and " + c2);
75         }
76
77         // REVISIT: we could try to find a common supper class or interface
78
return Object JavaDoc.class;
79     }
80
81     /**
82      * Concatenates two arrays into one. If arr1 is null or empty, returns arr2.
83      * If arr2 is null or empty, returns arr1. May return null if both arrays are null,
84      * or one is empty and the other null. <br>
85      * The concatenated array has componentType which is compatible with both input arrays (or Object[])
86      *
87      * @param arr1 input array
88      * @param arr2 input array
89      *
90      * @return Object the concatenated array, elements of arr1 first
91      */

92     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2)
93     {
94         int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1);
95
96         if (len1 <= 0)
97         {
98             return arr2;
99         }
100
101         int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2);
102
103         if (len2 <= 0)
104         {
105             return arr1;
106         }
107
108         Class JavaDoc commonComponentType =
109             commonClass(arr1.getClass().getComponentType(), arr2.getClass().getComponentType());
110         Object JavaDoc newArray = Array.newInstance(commonComponentType, len1 + len2);
111         System.arraycopy(arr1, 0, newArray, 0, len1);
112         System.arraycopy(arr2, 0, newArray, len1, len2);
113
114         return newArray;
115     }
116
117     /**
118      * Concatenates arrays into one. Any null or empty arrays are ignored.
119      * If all arrays are null or empty, returns null.
120      * Elements will be ordered in the order in which the arrays are supplied.
121      *
122      * @param arrs array of arrays
123      * @return the concatenated array
124      */

125     public static Object JavaDoc concat(Object JavaDoc[] arrs)
126     {
127         int totalLen = 0;
128         Class JavaDoc commonComponentType = null;
129         for (int i = 0, len = arrs.length; i < len; i++)
130         {
131             // skip all null arrays
132
if (arrs[i] == null)
133             {
134                 continue;
135             }
136
137             int arrayLen = Array.getLength(arrs[i]);
138
139             // skip all empty arrays
140
if (arrayLen == 0)
141             {
142                 continue;
143             }
144
145             totalLen += arrayLen;
146
147             Class JavaDoc componentType = arrs[i].getClass().getComponentType();
148             commonComponentType =
149                 (commonComponentType == null) ? componentType
150                                               : commonClass(commonComponentType, componentType);
151         }
152
153         if (commonComponentType == null)
154         {
155             return null;
156         }
157
158         return concat(Array.newInstance(commonComponentType, totalLen), totalLen, arrs);
159     }
160
161     public static Object JavaDoc concat(Object JavaDoc toArray, int totalLen, Object JavaDoc[] arrs)
162     {
163         if (totalLen == 0)
164         {
165             // Should we allocate an empty array instead?
166
return toArray;
167         }
168
169         if (totalLen > Array.getLength(toArray))
170         {
171             toArray = Array.newInstance(toArray.getClass().getComponentType(), totalLen);
172         }
173
174         for (int i = 0, len = arrs.length, offset = 0; i < len; i++)
175         {
176             final Object JavaDoc arr = arrs[i];
177             if (arr != null)
178             {
179                 int arrayLen = Array.getLength(arr);
180                 if (arrayLen > 0)
181                 {
182                     System.arraycopy(arr, 0, toArray, offset, arrayLen);
183                     offset += arrayLen;
184                 }
185             }
186         }
187
188         return toArray;
189     }
190
191     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3)
192     {
193         return concat(new Object JavaDoc[] {arr1, arr2, arr3});
194     }
195
196     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3, Object JavaDoc arr4)
197     {
198         return concat(new Object JavaDoc[] {arr1, arr2, arr3, arr4});
199     }
200
201     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3, Object JavaDoc arr4, Object JavaDoc arr5)
202     {
203         return concat(new Object JavaDoc[] {arr1, arr2, arr3, arr4, arr5});
204     }
205
206     public static Object JavaDoc concatSameType(Object JavaDoc toArray, Object JavaDoc[] arrs)
207     {
208         int totalLen = 0;
209         for (int i = 0, len = arrs.length; i < len; i++)
210         {
211             if (arrs[i] != null)
212             {
213                 totalLen += Array.getLength(arrs[i]);
214             }
215         }
216
217         return concat(toArray, totalLen, arrs);
218     }
219
220
221
222     public static boolean contains(Object JavaDoc[] array, Object JavaDoc value)
223     {
224         if (array == null || array.length == 0)
225         {
226             return false;
227         }
228
229         for (int i = 0; i < array.length; i++)
230         {
231             Object JavaDoc o = array[i];
232             if ((o == null && value == null) ||
233                 (o != null && o.equals(value)))
234             {
235                 return true;
236             }
237         }
238
239         return false;
240     }
241
242
243
244 // public static void main(String[] args)
245
// {
246
// // test code
247
// System.out.println(concat(new String[] {"a"}, new Object[] {"b"}));
248
// System.out.println(concat(new String[] {"a"}, new Integer[] {new Integer(0)}));
249
// System.out.println(concat(new Number[] {new Double(0)}, new Integer[] {new Integer(0)}));
250
// System.out.println(concat(new Number[] {}, new Integer[0]));
251
// System.out.println(concat(new Integer[] {new Integer(0)}, new Number[0]));
252
// System.out.println(concat(new Integer[] {new Integer(0)}, new Number[0], new int[0]));
253
// System.out.println(
254
// concat(new Integer[] {new Integer(0)}, new Number[] {new Double(0)}, new int[0]));
255
// System.out.println(concat(new int[] {1}, new int[] {2}));
256
// System.out.println(
257
// concat(new String[] {"a"}, new Integer[] {new Integer(0)}, new Object[] {"b"}));
258
// System.out.println(
259
// concat(new String[0], new Object[] {new String[] {"a"}, new Object[] {"b"}}));
260
// System.out.println(
261
// concat(new Integer[] {new Integer(0)}, new Number[] {new Double(0)}, new int[] {1}));
262
// }
263
}
264
Popular Tags