1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.util.Comparator;
26  import java.util.Set;
27  import java.util.TreeSet;
28  
29  /**
30   * <a href="ArrayUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class ArrayUtil {
36  
37      public static Boolean[] append(Boolean[] array, Boolean obj) {
38          Boolean[] newArray = new Boolean[array.length + 1];
39  
40          System.arraycopy(array, 0, newArray, 0, array.length);
41  
42          newArray[newArray.length - 1] = obj;
43  
44          return newArray;
45      }
46  
47      public static Double[] append(Double[] array, Double obj) {
48          Double[] newArray = new Double[array.length + 1];
49  
50          System.arraycopy(array, 0, newArray, 0, array.length);
51  
52          newArray[newArray.length - 1] = obj;
53  
54          return newArray;
55      }
56  
57      public static Float[] append(Float[] array, Float obj) {
58          Float[] newArray = new Float[array.length + 1];
59  
60          System.arraycopy(array, 0, newArray, 0, array.length);
61  
62          newArray[newArray.length - 1] = obj;
63  
64          return newArray;
65      }
66  
67      public static Integer[] append(Integer[] array, Integer obj) {
68          Integer[] newArray = new Integer[array.length + 1];
69  
70          System.arraycopy(array, 0, newArray, 0, array.length);
71  
72          newArray[newArray.length - 1] = obj;
73  
74          return newArray;
75      }
76  
77      public static Long[] append(Long[] array, Long obj) {
78          Long[] newArray = new Long[array.length + 1];
79  
80          System.arraycopy(array, 0, newArray, 0, array.length);
81  
82          newArray[newArray.length - 1] = obj;
83  
84          return newArray;
85      }
86  
87      public static Object[] append(Object[] array, Object obj) {
88          Object[] newArray = new Object[array.length + 1];
89  
90          System.arraycopy(array, 0, newArray, 0, array.length);
91  
92          newArray[newArray.length - 1] = obj;
93  
94          return newArray;
95      }
96  
97      public static Object[][] append(Object[][] array, Object[] obj) {
98          Object[][] newArray = new Object[array.length + 1][];
99  
100         System.arraycopy(array, 0, newArray, 0, array.length);
101 
102         newArray[newArray.length - 1] = obj;
103 
104         return newArray;
105     }
106 
107     public static Short[] append(Short[] array, Short obj) {
108         Short[] newArray = new Short[array.length + 1];
109 
110         System.arraycopy(array, 0, newArray, 0, array.length);
111 
112         newArray[newArray.length - 1] = obj;
113 
114         return newArray;
115     }
116 
117     public static String[] append(String[] array, String obj) {
118         String[] newArray = new String[array.length + 1];
119 
120         System.arraycopy(array, 0, newArray, 0, array.length);
121 
122         newArray[newArray.length - 1] = obj;
123 
124         return newArray;
125     }
126 
127     public static String[][] append(String[][] array, String[] obj) {
128         String[][] newArray = new String[array.length + 1][];
129 
130         System.arraycopy(array, 0, newArray, 0, array.length);
131 
132         newArray[newArray.length - 1] = obj;
133 
134         return newArray;
135     }
136 
137     public static Boolean[] append(Boolean[] array1, Boolean[] array2) {
138         Boolean[] newArray = new Boolean[array1.length + array2.length];
139 
140         System.arraycopy(array1, 0, newArray, 0, array1.length);
141         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
142 
143         return newArray;
144     }
145 
146     public static Double[] append(Double[] array1, Double[] array2) {
147         Double[] newArray = new Double[array1.length + array2.length];
148 
149         System.arraycopy(array1, 0, newArray, 0, array1.length);
150         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
151 
152         return newArray;
153     }
154 
155     public static Float[] append(Float[] array1, Float[] array2) {
156         Float[] newArray = new Float[array1.length + array2.length];
157 
158         System.arraycopy(array1, 0, newArray, 0, array1.length);
159         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
160 
161         return newArray;
162     }
163 
164     public static Integer[] append(Integer[] array1, Integer[] array2) {
165         Integer[] newArray = new Integer[array1.length + array2.length];
166 
167         System.arraycopy(array1, 0, newArray, 0, array1.length);
168         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
169 
170         return newArray;
171     }
172 
173     public static Long[] append(Long[] array1, Long[] array2) {
174         Long[] newArray = new Long[array1.length + array2.length];
175 
176         System.arraycopy(array1, 0, newArray, 0, array1.length);
177         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
178 
179         return newArray;
180     }
181 
182     public static Object[] append(Object[] array1, Object[] array2) {
183         Object[] newArray = new Object[array1.length + array2.length];
184 
185         System.arraycopy(array1, 0, newArray, 0, array1.length);
186         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
187 
188         return newArray;
189     }
190 
191     public static Object[][] append(Object[][] array1, Object[][] array2) {
192         Object[][] newArray = new Object[array1.length + array2.length][];
193 
194         System.arraycopy(array1, 0, newArray, 0, array1.length);
195         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
196 
197         return newArray;
198     }
199 
200     public static Short[] append(Short[] array1, Short[] array2) {
201         Short[] newArray = new Short[array1.length + array2.length];
202 
203         System.arraycopy(array1, 0, newArray, 0, array1.length);
204         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
205 
206         return newArray;
207     }
208 
209     public static String[] append(String[] array1, String[] array2) {
210         String[] newArray = new String[array1.length + array2.length];
211 
212         System.arraycopy(array1, 0, newArray, 0, array1.length);
213         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
214 
215         return newArray;
216     }
217 
218     public static String[][] append(String[][] array1, String[][] array2) {
219         String[][] newArray = new String[array1.length + array2.length][];
220 
221         System.arraycopy(array1, 0, newArray, 0, array1.length);
222         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
223 
224         return newArray;
225     }
226 
227     public static void combine(
228         Object[] array1, Object[] array2, Object[] combinedArray) {
229 
230         System.arraycopy(array1, 0, combinedArray, 0, array1.length);
231 
232         System.arraycopy(
233             array2, 0, combinedArray, array1.length, array2.length);
234     }
235 
236     public static boolean contains(boolean[] array, boolean value) {
237         if (array == null) {
238             return false;
239         }
240         else {
241             for (int i = 0; i < array.length; i++) {
242                 if (value == array[i]) {
243                     return true;
244                 }
245             }
246 
247             return false;
248         }
249     }
250 
251     public static boolean contains(char[] array, char value) {
252         if (array == null) {
253             return false;
254         }
255         else {
256             for (int i = 0; i < array.length; i++) {
257                 if (value == array[i]) {
258                     return true;
259                 }
260             }
261 
262             return false;
263         }
264     }
265 
266     public static boolean contains(double[] array, double value) {
267         if (array == null) {
268             return false;
269         }
270         else {
271             for (int i = 0; i < array.length; i++) {
272                 if (value == array[i]) {
273                     return true;
274                 }
275             }
276 
277             return false;
278         }
279     }
280 
281     public static boolean contains(long[] array, long value) {
282         if (array == null) {
283             return false;
284         }
285         else {
286             for (int i = 0; i < array.length; i++) {
287                 if (value == array[i]) {
288                     return true;
289                 }
290             }
291 
292             return false;
293         }
294     }
295 
296     public static boolean contains(int[] array, int value) {
297         if (array == null) {
298             return false;
299         }
300         else {
301             for (int i = 0; i < array.length; i++) {
302                 if (value == array[i]) {
303                     return true;
304                 }
305             }
306 
307             return false;
308         }
309     }
310 
311     public static boolean contains(short[] array, short value) {
312         if (array == null) {
313             return false;
314         }
315         else {
316             for (int i = 0; i < array.length; i++) {
317                 if (value == array[i]) {
318                     return true;
319                 }
320             }
321 
322             return false;
323         }
324     }
325 
326     public static boolean contains(Object[] array, Object value) {
327         if ((array == null) || (value == null)) {
328             return false;
329         }
330         else {
331             for (int i = 0; i < array.length; i++) {
332                 if (value.equals(array[i])) {
333                     return true;
334                 }
335             }
336 
337             return false;
338         }
339     }
340 
341     public static String[] distinct(String[] array) {
342         return distinct(array, null);
343     }
344 
345     public static String[] distinct(String[] array, Comparator comparator) {
346         if ((array == null) || (array.length == 0)) {
347             return array;
348         }
349 
350         Set set = null;
351 
352         if (comparator == null) {
353             set = new TreeSet();
354         }
355         else {
356             set = new TreeSet(comparator);
357         }
358 
359         for (int i = 0; i < array.length; i++) {
360             Object obj = array[i];
361 
362             if (!set.contains(obj)) {
363                 set.add(obj);
364             }
365         }
366 
367         return (String[])set.toArray(new String[0]);
368     }
369 
370     public static int getLength(Object[] array) {
371         if (array == null) {
372             return 0;
373         }
374         else {
375             return array.length;
376         }
377     }
378 
379     public static Object getValue(Object[] array, int pos) {
380         if ((array == null) || (array.length <= pos)) {
381             return null;
382         }
383         else {
384             return array[pos];
385         }
386     }
387 
388     public static Boolean[] toObjectArray(boolean[] array) {
389         Boolean[] objArray = new Boolean[array.length];
390 
391         for (int i = 0; i < array.length; i++) {
392             objArray[i] = Boolean.valueOf(array[i]);
393         }
394 
395         return objArray;
396     }
397 
398     public static Double[] toObjectArray(double[] array) {
399         Double[] objArray = new Double[array.length];
400 
401         for (int i = 0; i < array.length; i++) {
402             objArray[i] = new Double(array[i]);
403         }
404 
405         return objArray;
406     }
407 
408     public static Float[] toObjectArray(float[] array) {
409         Float[] objArray = new Float[array.length];
410 
411         for (int i = 0; i < array.length; i++) {
412             objArray[i] = new Float(array[i]);
413         }
414 
415         return objArray;
416     }
417 
418     public static Integer[] toObjectArray(int[] array) {
419         Integer[] objArray = new Integer[array.length];
420 
421         for (int i = 0; i < array.length; i++) {
422             objArray[i] = new Integer(array[i]);
423         }
424 
425         return objArray;
426     }
427 
428     public static Long[] toObjectArray(long[] array) {
429         Long[] objArray = new Long[array.length];
430 
431         for (int i = 0; i < array.length; i++) {
432             objArray[i] = new Long(array[i]);
433         }
434 
435         return objArray;
436     }
437 
438     public static Short[] toObjectArray(short[] array) {
439         Short[] objArray = new Short[array.length];
440 
441         for (int i = 0; i < array.length; i++) {
442             objArray[i] = new Short(array[i]);
443         }
444 
445         return objArray;
446     }
447 
448 }