Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ArrayUtils |
|
| 4.0;4 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.shared_impl.util; | |
20 | ||
21 | import java.lang.reflect.Array; | |
22 | ||
23 | /** | |
24 | * Utility class for managing arrays | |
25 | * | |
26 | * @author Anton Koinov (latest modification by $Author: matzew $) | |
27 | * @version $Revision: 557350 $ $Date: 2007-07-18 13:19:50 -0500 (Wed, 18 Jul 2007) $ | |
28 | */ | |
29 | public class ArrayUtils | |
30 | { | |
31 | 0 | public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
32 | 0 | public static final String[] EMPTY_STRING_ARRAY = new String[0]; |
33 | ||
34 | //~ Constructors ------------------------------------------------------------------------------- | |
35 | ||
36 | protected ArrayUtils() | |
37 | 0 | { |
38 | // hide from public access | |
39 | 0 | } |
40 | ||
41 | //~ Methods ------------------------------------------------------------------------------------ | |
42 | ||
43 | public static Class commonClass(Class c1, Class c2) | |
44 | { | |
45 | 0 | if (c1 == c2) |
46 | { | |
47 | 0 | return c1; |
48 | } | |
49 | ||
50 | 0 | if ((c1 == Object.class) || c1.isAssignableFrom(c2)) |
51 | { | |
52 | 0 | return c1; |
53 | } | |
54 | ||
55 | 0 | if (c2.isAssignableFrom(c1)) |
56 | { | |
57 | 0 | return c2; |
58 | } | |
59 | ||
60 | 0 | if (c1.isPrimitive() || c2.isPrimitive()) |
61 | { | |
62 | // REVISIT: we could try to autoconvert to Object or something appropriate | |
63 | 0 | throw new IllegalArgumentException("incompatible types " + c1 + " and " + c2); |
64 | } | |
65 | ||
66 | // REVISIT: we could try to find a common supper class or interface | |
67 | 0 | return Object.class; |
68 | } | |
69 | ||
70 | /** | |
71 | * Concatenates two arrays into one. If arr1 is null or empty, returns arr2. | |
72 | * If arr2 is null or empty, returns arr1. May return null if both arrays are null, | |
73 | * or one is empty and the other null. <br> | |
74 | * The concatenated array has componentType which is compatible with both input arrays (or Object[]) | |
75 | * | |
76 | * @param arr1 input array | |
77 | * @param arr2 input array | |
78 | * | |
79 | * @return Object the concatenated array, elements of arr1 first | |
80 | */ | |
81 | public static Object concat(Object arr1, Object arr2) | |
82 | { | |
83 | 0 | int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1); |
84 | ||
85 | 0 | if (len1 <= 0) |
86 | { | |
87 | 0 | return arr2; |
88 | } | |
89 | ||
90 | 0 | int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2); |
91 | ||
92 | 0 | if (len2 <= 0) |
93 | { | |
94 | 0 | return arr1; |
95 | } | |
96 | ||
97 | 0 | Class commonComponentType = |
98 | commonClass(arr1.getClass().getComponentType(), arr2.getClass().getComponentType()); | |
99 | 0 | Object newArray = Array.newInstance(commonComponentType, len1 + len2); |
100 | 0 | System.arraycopy(arr1, 0, newArray, 0, len1); |
101 | 0 | System.arraycopy(arr2, 0, newArray, len1, len2); |
102 | ||
103 | 0 | return newArray; |
104 | } | |
105 | ||
106 | /** | |
107 | * Concatenates arrays into one. Any null or empty arrays are ignored. | |
108 | * If all arrays are null or empty, returns null. | |
109 | * Elements will be ordered in the order in which the arrays are supplied. | |
110 | * | |
111 | * @param arrs array of arrays | |
112 | * @return the concatenated array | |
113 | */ | |
114 | public static Object concat(Object[] arrs) | |
115 | { | |
116 | 0 | int totalLen = 0; |
117 | 0 | Class commonComponentType = null; |
118 | 0 | for (int i = 0, len = arrs.length; i < len; i++) |
119 | { | |
120 | // skip all null arrays | |
121 | 0 | if (arrs[i] == null) |
122 | { | |
123 | 0 | continue; |
124 | } | |
125 | ||
126 | 0 | int arrayLen = Array.getLength(arrs[i]); |
127 | ||
128 | // skip all empty arrays | |
129 | 0 | if (arrayLen == 0) |
130 | { | |
131 | 0 | continue; |
132 | } | |
133 | ||
134 | 0 | totalLen += arrayLen; |
135 | ||
136 | 0 | Class componentType = arrs[i].getClass().getComponentType(); |
137 | 0 | commonComponentType = |
138 | (commonComponentType == null) ? componentType | |
139 | : commonClass(commonComponentType, componentType); | |
140 | } | |
141 | ||
142 | 0 | if (commonComponentType == null) |
143 | { | |
144 | 0 | return null; |
145 | } | |
146 | ||
147 | 0 | return concat(Array.newInstance(commonComponentType, totalLen), totalLen, arrs); |
148 | } | |
149 | ||
150 | public static Object concat(Object toArray, int totalLen, Object[] arrs) | |
151 | { | |
152 | 0 | if (totalLen == 0) |
153 | { | |
154 | // Should we allocate an empty array instead? | |
155 | 0 | return toArray; |
156 | } | |
157 | ||
158 | 0 | if (totalLen > Array.getLength(toArray)) |
159 | { | |
160 | 0 | toArray = Array.newInstance(toArray.getClass().getComponentType(), totalLen); |
161 | } | |
162 | ||
163 | 0 | for (int i = 0, len = arrs.length, offset = 0; i < len; i++) |
164 | { | |
165 | 0 | final Object arr = arrs[i]; |
166 | 0 | if (arr != null) |
167 | { | |
168 | 0 | int arrayLen = Array.getLength(arr); |
169 | 0 | if (arrayLen > 0) |
170 | { | |
171 | 0 | System.arraycopy(arr, 0, toArray, offset, arrayLen); |
172 | 0 | offset += arrayLen; |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | 0 | return toArray; |
178 | } | |
179 | ||
180 | public static Object concat(Object arr1, Object arr2, Object arr3) | |
181 | { | |
182 | 0 | return concat(new Object[] {arr1, arr2, arr3}); |
183 | } | |
184 | ||
185 | public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4) | |
186 | { | |
187 | 0 | return concat(new Object[] {arr1, arr2, arr3, arr4}); |
188 | } | |
189 | ||
190 | public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4, Object arr5) | |
191 | { | |
192 | 0 | return concat(new Object[] {arr1, arr2, arr3, arr4, arr5}); |
193 | } | |
194 | ||
195 | public static Object concatSameType(Object toArray, Object[] arrs) | |
196 | { | |
197 | 0 | int totalLen = 0; |
198 | 0 | for (int i = 0, len = arrs.length; i < len; i++) |
199 | { | |
200 | 0 | if (arrs[i] != null) |
201 | { | |
202 | 0 | totalLen += Array.getLength(arrs[i]); |
203 | } | |
204 | } | |
205 | ||
206 | 0 | return concat(toArray, totalLen, arrs); |
207 | } | |
208 | ||
209 | ||
210 | ||
211 | public static boolean contains(Object[] array, Object value) | |
212 | { | |
213 | 0 | if (array == null || array.length == 0) |
214 | { | |
215 | 0 | return false; |
216 | } | |
217 | ||
218 | 0 | for (int i = 0; i < array.length; i++) |
219 | { | |
220 | 0 | Object o = array[i]; |
221 | 0 | if ((o == null && value == null) || |
222 | (o != null && o.equals(value))) | |
223 | { | |
224 | 0 | return true; |
225 | } | |
226 | } | |
227 | ||
228 | 0 | return false; |
229 | } | |
230 | ||
231 | ||
232 | ||
233 | // public static void main(String[] args) | |
234 | // { | |
235 | // // test code | |
236 | // System.out.println(concat(new String[] {"a"}, new Object[] {"b"})); | |
237 | // System.out.println(concat(new String[] {"a"}, new Integer[] {new Integer(0)})); | |
238 | // System.out.println(concat(new Number[] {new Double(0)}, new Integer[] {new Integer(0)})); | |
239 | // System.out.println(concat(new Number[] {}, new Integer[0])); | |
240 | // System.out.println(concat(new Integer[] {new Integer(0)}, new Number[0])); | |
241 | // System.out.println(concat(new Integer[] {new Integer(0)}, new Number[0], new int[0])); | |
242 | // System.out.println( | |
243 | // concat(new Integer[] {new Integer(0)}, new Number[] {new Double(0)}, new int[0])); | |
244 | // System.out.println(concat(new int[] {1}, new int[] {2})); | |
245 | // System.out.println( | |
246 | // concat(new String[] {"a"}, new Integer[] {new Integer(0)}, new Object[] {"b"})); | |
247 | // System.out.println( | |
248 | // concat(new String[0], new Object[] {new String[] {"a"}, new Object[] {"b"}})); | |
249 | // System.out.println( | |
250 | // concat(new Integer[] {new Integer(0)}, new Number[] {new Double(0)}, new int[] {1})); | |
251 | // } | |
252 | } |