KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > 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
17 package com.icesoft.faces.util;
18
19 import java.lang.reflect.Array JavaDoc;
20
21
22 /**
23  * Utility class for managing arrays
24  *
25  * @author Anton Koinov (latest modification by $Author: grantsmith $)
26  * @version $Revision: 169655 $ $Date: 2005-05-11 12:45:06 -0400 (Wed, 11 May
27  * 2005) $
28  */

29 public class ArrayUtils {
30     public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
31     public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
32
33     //~ Constructors -------------------------------------------------------------------------------
34

35     protected ArrayUtils() {
36         // hide from public access
37
}
38
39     //~ Methods ------------------------------------------------------------------------------------
40

41     public static Class JavaDoc commonClass(Class JavaDoc c1, Class JavaDoc c2) {
42         if (c1 == c2) {
43             return c1;
44         }
45
46         if ((c1 == Object JavaDoc.class) || c1.isAssignableFrom(c2)) {
47             return c1;
48         }
49
50         if (c2.isAssignableFrom(c1)) {
51             return c2;
52         }
53
54         if (c1.isPrimitive() || c2.isPrimitive()) {
55             // REVISIT: we could try to autoconvert to Object or something appropriate
56
throw new IllegalArgumentException JavaDoc(
57                     "incompatible types " + c1 + " and " + c2);
58         }
59
60         // REVISIT: we could try to find a common supper class or interface
61
return Object JavaDoc.class;
62     }
63
64     /**
65      * Concatenates two arrays into one. If arr1 is null or empty, returns arr2.
66      * If arr2 is null or empty, returns arr1. May return null if both arrays
67      * are null, or one is empty and the other null. <br> The concatenated array
68      * has componentType which is compatible with both input arrays (or
69      * Object[])
70      *
71      * @param arr1 input array
72      * @param arr2 input array
73      * @return Object the concatenated array, elements of arr1 first
74      */

75     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2) {
76         int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1);
77
78         if (len1 <= 0) {
79             return arr2;
80         }
81
82         int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2);
83
84         if (len2 <= 0) {
85             return arr1;
86         }
87
88         Class JavaDoc commonComponentType =
89                 commonClass(arr1.getClass().getComponentType(),
90                             arr2.getClass().getComponentType());
91         Object JavaDoc newArray = Array.newInstance(commonComponentType, len1 + len2);
92         System.arraycopy(arr1, 0, newArray, 0, len1);
93         System.arraycopy(arr2, 0, newArray, len1, len2);
94
95         return newArray;
96     }
97
98     /**
99      * Concatenates arrays into one. Any null or empty arrays are ignored. If
100      * all arrays are null or empty, returns null. Elements will be ordered in
101      * the order in which the arrays are supplied.
102      *
103      * @param arrs array of arrays
104      * @return the concatenated array
105      */

106     public static Object JavaDoc concat(Object JavaDoc[] arrs) {
107         int totalLen = 0;
108         Class JavaDoc commonComponentType = null;
109         for (int i = 0, len = arrs.length; i < len; i++) {
110             // skip all null arrays
111
if (arrs[i] == null) {
112                 continue;
113             }
114
115             int arrayLen = Array.getLength(arrs[i]);
116
117             // skip all empty arrays
118
if (arrayLen == 0) {
119                 continue;
120             }
121
122             totalLen += arrayLen;
123
124             Class JavaDoc componentType = arrs[i].getClass().getComponentType();
125             commonComponentType =
126                     (commonComponentType == null) ? componentType
127                     : commonClass(commonComponentType, componentType);
128         }
129
130         if (commonComponentType == null) {
131             return null;
132         }
133
134         return concat(Array.newInstance(commonComponentType, totalLen),
135                       totalLen, arrs);
136     }
137
138     public static Object JavaDoc concat(Object JavaDoc toArray, int totalLen, Object JavaDoc[] arrs) {
139         if (totalLen == 0) {
140             // Should we allocate an empty array instead?
141
return toArray;
142         }
143
144         if (totalLen > Array.getLength(toArray)) {
145             toArray = Array.newInstance(toArray.getClass().getComponentType(),
146                                         totalLen);
147         }
148
149         for (int i = 0, len = arrs.length, offset = 0; i < len; i++) {
150             final Object JavaDoc arr = arrs[i];
151             if (arr != null) {
152                 int arrayLen = Array.getLength(arr);
153                 if (arrayLen > 0) {
154                     System.arraycopy(arr, 0, toArray, offset, arrayLen);
155                     offset += arrayLen;
156                 }
157             }
158         }
159
160         return toArray;
161     }
162
163     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3) {
164         return concat(new Object JavaDoc[]{arr1, arr2, arr3});
165     }
166
167     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3,
168                                 Object JavaDoc arr4) {
169         return concat(new Object JavaDoc[]{arr1, arr2, arr3, arr4});
170     }
171
172     public static Object JavaDoc concat(Object JavaDoc arr1, Object JavaDoc arr2, Object JavaDoc arr3,
173                                 Object JavaDoc arr4, Object JavaDoc arr5) {
174         return concat(new Object JavaDoc[]{arr1, arr2, arr3, arr4, arr5});
175     }
176
177     public static Object JavaDoc concatSameType(Object JavaDoc toArray, Object JavaDoc[] arrs) {
178         int totalLen = 0;
179         for (int i = 0, len = arrs.length; i < len; i++) {
180             if (arrs[i] != null) {
181                 totalLen += Array.getLength(arrs[i]);
182             }
183         }
184
185         return concat(toArray, totalLen, arrs);
186     }
187
188
189     public static boolean contains(Object JavaDoc[] array, Object JavaDoc value) {
190         if (array == null || array.length == 0) {
191             return false;
192         }
193
194         for (int i = 0; i < array.length; i++) {
195             Object JavaDoc o = array[i];
196             if ((o == null && value == null) ||
197                 (o != null && o.equals(value))) {
198                 return true;
199             }
200         }
201
202         return false;
203     }
204 }
205
Popular Tags