1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.util;
20
21 import java.lang.reflect.Array;
22
23
24
25
26
27
28
29
30 public final class ArrayUtils
31 {
32 public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
33 public static final String[] EMPTY_STRING_ARRAY = new String[0];
34
35
36
37 protected ArrayUtils()
38 {
39
40 }
41
42
43
44 private static Class commonClass(Class c1, Class c2)
45 {
46 if (c1 == c2)
47 {
48 return c1;
49 }
50
51 if ((c1 == Object.class) || c1.isAssignableFrom(c2))
52 {
53 return c1;
54 }
55
56 if (c2.isAssignableFrom(c1))
57 {
58 return c2;
59 }
60
61 if (c1.isPrimitive() || c2.isPrimitive())
62 {
63
64 throw new IllegalArgumentException("incompatible types " + c1 + " and " + c2);
65 }
66
67
68 return Object.class;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 public static Object concat(Object arr1, Object arr2)
83 {
84 int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1);
85
86 if (len1 <= 0)
87 {
88 return arr2;
89 }
90
91 int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2);
92
93 if (len2 <= 0)
94 {
95 return arr1;
96 }
97
98 Class commonComponentType =
99 commonClass(arr1.getClass().getComponentType(), arr2.getClass().getComponentType());
100 Object newArray = Array.newInstance(commonComponentType, len1 + len2);
101 System.arraycopy(arr1, 0, newArray, 0, len1);
102 System.arraycopy(arr2, 0, newArray, len1, len2);
103
104 return newArray;
105 }
106
107
108
109
110
111
112
113
114
115 public static Object concat(Object[] arrs)
116 {
117 int totalLen = 0;
118 Class commonComponentType = null;
119 for (int i = 0, len = arrs.length; i < len; i++)
120 {
121
122 if (arrs[i] == null)
123 {
124 continue;
125 }
126
127 int arrayLen = Array.getLength(arrs[i]);
128
129
130 if (arrayLen == 0)
131 {
132 continue;
133 }
134
135 totalLen += arrayLen;
136
137 Class componentType = arrs[i].getClass().getComponentType();
138 commonComponentType =
139 (commonComponentType == null) ? componentType
140 : commonClass(commonComponentType, componentType);
141 }
142
143 if (commonComponentType == null)
144 {
145 return null;
146 }
147
148 return concat(Array.newInstance(commonComponentType, totalLen), totalLen, arrs);
149 }
150
151 public static Object concat(Object toArray, int totalLen, Object[] arrs)
152 {
153 if (totalLen == 0)
154 {
155
156 return toArray;
157 }
158
159 if (totalLen > Array.getLength(toArray))
160 {
161 toArray = Array.newInstance(toArray.getClass().getComponentType(), totalLen);
162 }
163
164 for (int i = 0, len = arrs.length, offset = 0; i < len; i++)
165 {
166 final Object arr = arrs[i];
167 if (arr != null)
168 {
169 int arrayLen = Array.getLength(arr);
170 if (arrayLen > 0)
171 {
172 System.arraycopy(arr, 0, toArray, offset, arrayLen);
173 offset += arrayLen;
174 }
175 }
176 }
177
178 return toArray;
179 }
180
181 public static Object concat(Object arr1, Object arr2, Object arr3)
182 {
183 return concat(new Object[] {arr1, arr2, arr3});
184 }
185
186 public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4)
187 {
188 return concat(new Object[] {arr1, arr2, arr3, arr4});
189 }
190
191 public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4, Object arr5)
192 {
193 return concat(new Object[] {arr1, arr2, arr3, arr4, arr5});
194 }
195
196 public static Object concatSameType(Object toArray, Object[] arrs)
197 {
198 int totalLen = 0;
199 for (int i = 0, len = arrs.length; i < len; i++)
200 {
201 if (arrs[i] != null)
202 {
203 totalLen += Array.getLength(arrs[i]);
204 }
205 }
206
207 return concat(toArray, totalLen, arrs);
208 }
209
210 public static boolean contains(Object[] array, Object value)
211 {
212 if (array == null || array.length == 0)
213 {
214 return false;
215 }
216
217 for (int i = 0; i < array.length; i++)
218 {
219 Object o = array[i];
220 if ((o == null && value == null) ||
221 (o != null && o.equals(value)))
222 {
223 return true;
224 }
225 }
226
227 return false;
228 }
229 }