1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.util.ArrayList;
23  import java.util.Comparator;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  /**
29   * <a href="ArrayUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   *
33   */
34  public class ArrayUtil {
35  
36      public static boolean[] append(boolean[] array, boolean value) {
37          boolean[] newArray = new boolean[array.length + 1];
38  
39          System.arraycopy(array, 0, newArray, 0, array.length);
40  
41          newArray[newArray.length - 1] = value;
42  
43          return newArray;
44      }
45  
46      public static byte[] append(byte[] array, byte value) {
47          byte[] newArray = new byte[array.length + 1];
48  
49          System.arraycopy(array, 0, newArray, 0, array.length);
50  
51          newArray[newArray.length - 1] = value;
52  
53          return newArray;
54      }
55  
56      public static double[] append(double[] array, double value) {
57          double[] newArray = new double[array.length + 1];
58  
59          System.arraycopy(array, 0, newArray, 0, array.length);
60  
61          newArray[newArray.length - 1] = value;
62  
63          return newArray;
64      }
65  
66      public static float[] append(float[] array, float value) {
67          float[] newArray = new float[array.length + 1];
68  
69          System.arraycopy(array, 0, newArray, 0, array.length);
70  
71          newArray[newArray.length - 1] = value;
72  
73          return newArray;
74      }
75  
76      public static int[] append(int[] array, int value) {
77          int[] newArray = new int[array.length + 1];
78  
79          System.arraycopy(array, 0, newArray, 0, array.length);
80  
81          newArray[newArray.length - 1] = value;
82  
83          return newArray;
84      }
85  
86      public static long[] append(long[] array, long value) {
87          long[] newArray = new long[array.length + 1];
88  
89          System.arraycopy(array, 0, newArray, 0, array.length);
90  
91          newArray[newArray.length - 1] = value;
92  
93          return newArray;
94      }
95  
96      public static short[] append(short[] array, short value) {
97          short[] newArray = new short[array.length + 1];
98  
99          System.arraycopy(array, 0, newArray, 0, array.length);
100 
101         newArray[newArray.length - 1] = value;
102 
103         return newArray;
104     }
105 
106     public static Boolean[] append(Boolean[] array, Boolean value) {
107         Boolean[] newArray = new Boolean[array.length + 1];
108 
109         System.arraycopy(array, 0, newArray, 0, array.length);
110 
111         newArray[newArray.length - 1] = value;
112 
113         return newArray;
114     }
115 
116     public static Double[] append(Double[] array, Double value) {
117         Double[] newArray = new Double[array.length + 1];
118 
119         System.arraycopy(array, 0, newArray, 0, array.length);
120 
121         newArray[newArray.length - 1] = value;
122 
123         return newArray;
124     }
125 
126     public static Float[] append(Float[] array, Float value) {
127         Float[] newArray = new Float[array.length + 1];
128 
129         System.arraycopy(array, 0, newArray, 0, array.length);
130 
131         newArray[newArray.length - 1] = value;
132 
133         return newArray;
134     }
135 
136     public static Integer[] append(Integer[] array, Integer value) {
137         Integer[] newArray = new Integer[array.length + 1];
138 
139         System.arraycopy(array, 0, newArray, 0, array.length);
140 
141         newArray[newArray.length - 1] = value;
142 
143         return newArray;
144     }
145 
146     public static Long[] append(Long[] array, Long value) {
147         Long[] newArray = new Long[array.length + 1];
148 
149         System.arraycopy(array, 0, newArray, 0, array.length);
150 
151         newArray[newArray.length - 1] = value;
152 
153         return newArray;
154     }
155 
156     public static Object[] append(Object[] array, Object value) {
157         Object[] newArray = new Object[array.length + 1];
158 
159         System.arraycopy(array, 0, newArray, 0, array.length);
160 
161         newArray[newArray.length - 1] = value;
162 
163         return newArray;
164     }
165 
166     public static Object[][] append(Object[][] array, Object[] value) {
167         Object[][] newArray = new Object[array.length + 1][];
168 
169         System.arraycopy(array, 0, newArray, 0, array.length);
170 
171         newArray[newArray.length - 1] = value;
172 
173         return newArray;
174     }
175 
176     public static Short[] append(Short[] array, Short value) {
177         Short[] newArray = new Short[array.length + 1];
178 
179         System.arraycopy(array, 0, newArray, 0, array.length);
180 
181         newArray[newArray.length - 1] = value;
182 
183         return newArray;
184     }
185 
186     public static String[] append(String[] array, String value) {
187         String[] newArray = new String[array.length + 1];
188 
189         System.arraycopy(array, 0, newArray, 0, array.length);
190 
191         newArray[newArray.length - 1] = value;
192 
193         return newArray;
194     }
195 
196     public static String[][] append(String[][] array, String[] value) {
197         String[][] newArray = new String[array.length + 1][];
198 
199         System.arraycopy(array, 0, newArray, 0, array.length);
200 
201         newArray[newArray.length - 1] = value;
202 
203         return newArray;
204     }
205 
206     public static boolean[] append(boolean[] array1, boolean[] array2) {
207         boolean[] newArray = new boolean[array1.length + array2.length];
208 
209         System.arraycopy(array1, 0, newArray, 0, array1.length);
210         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
211 
212         return newArray;
213     }
214 
215     public static byte[] append(byte[] array1, byte[] array2) {
216         byte[] newArray = new byte[array1.length + array2.length];
217 
218         System.arraycopy(array1, 0, newArray, 0, array1.length);
219         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
220 
221         return newArray;
222     }
223 
224     public static double[] append(double[] array1, double[] array2) {
225         double[] newArray = new double[array1.length + array2.length];
226 
227         System.arraycopy(array1, 0, newArray, 0, array1.length);
228         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
229 
230         return newArray;
231     }
232 
233     public static float[] append(float[] array1, float[] array2) {
234         float[] newArray = new float[array1.length + array2.length];
235 
236         System.arraycopy(array1, 0, newArray, 0, array1.length);
237         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
238 
239         return newArray;
240     }
241 
242     public static int[] append(int[] array1, int[] array2) {
243         int[] newArray = new int[array1.length + array2.length];
244 
245         System.arraycopy(array1, 0, newArray, 0, array1.length);
246         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
247 
248         return newArray;
249     }
250 
251     public static long[] append(long[] array1, long[] array2) {
252         long[] newArray = new long[array1.length + array2.length];
253 
254         System.arraycopy(array1, 0, newArray, 0, array1.length);
255         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
256 
257         return newArray;
258     }
259 
260     public static short[] append(short[] array1, short[] array2) {
261         short[] newArray = new short[array1.length + array2.length];
262 
263         System.arraycopy(array1, 0, newArray, 0, array1.length);
264         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
265 
266         return newArray;
267     }
268 
269     public static Boolean[] append(Boolean[] array1, Boolean[] array2) {
270         Boolean[] newArray = new Boolean[array1.length + array2.length];
271 
272         System.arraycopy(array1, 0, newArray, 0, array1.length);
273         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
274 
275         return newArray;
276     }
277 
278     public static Double[] append(Double[] array1, Double[] array2) {
279         Double[] newArray = new Double[array1.length + array2.length];
280 
281         System.arraycopy(array1, 0, newArray, 0, array1.length);
282         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
283 
284         return newArray;
285     }
286 
287     public static Float[] append(Float[] array1, Float[] array2) {
288         Float[] newArray = new Float[array1.length + array2.length];
289 
290         System.arraycopy(array1, 0, newArray, 0, array1.length);
291         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
292 
293         return newArray;
294     }
295 
296     public static Integer[] append(Integer[] array1, Integer[] array2) {
297         Integer[] newArray = new Integer[array1.length + array2.length];
298 
299         System.arraycopy(array1, 0, newArray, 0, array1.length);
300         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
301 
302         return newArray;
303     }
304 
305     public static Long[] append(Long[] array1, Long[] array2) {
306         Long[] newArray = new Long[array1.length + array2.length];
307 
308         System.arraycopy(array1, 0, newArray, 0, array1.length);
309         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
310 
311         return newArray;
312     }
313 
314     public static Object[] append(Object[] array1, Object[] array2) {
315         Object[] newArray = new Object[array1.length + array2.length];
316 
317         System.arraycopy(array1, 0, newArray, 0, array1.length);
318         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
319 
320         return newArray;
321     }
322 
323     public static Object[][] append(Object[][] array1, Object[][] array2) {
324         Object[][] newArray = new Object[array1.length + array2.length][];
325 
326         System.arraycopy(array1, 0, newArray, 0, array1.length);
327         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
328 
329         return newArray;
330     }
331 
332     public static Short[] append(Short[] array1, Short[] array2) {
333         Short[] newArray = new Short[array1.length + array2.length];
334 
335         System.arraycopy(array1, 0, newArray, 0, array1.length);
336         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
337 
338         return newArray;
339     }
340 
341     public static String[] append(String[] array1, String[] array2) {
342         String[] newArray = new String[array1.length + array2.length];
343 
344         System.arraycopy(array1, 0, newArray, 0, array1.length);
345         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
346 
347         return newArray;
348     }
349 
350     public static String[][] append(String[][] array1, String[][] array2) {
351         String[][] newArray = new String[array1.length + array2.length][];
352 
353         System.arraycopy(array1, 0, newArray, 0, array1.length);
354         System.arraycopy(array2, 0, newArray, array1.length, array2.length);
355 
356         return newArray;
357     }
358 
359     public static void combine(
360         Object[] array1, Object[] array2, Object[] combinedArray) {
361 
362         System.arraycopy(array1, 0, combinedArray, 0, array1.length);
363 
364         System.arraycopy(
365             array2, 0, combinedArray, array1.length, array2.length);
366     }
367 
368     public static boolean contains(boolean[] array, boolean value) {
369         if ((array == null) || (array.length == 0)) {
370             return false;
371         }
372         else {
373             for (int i = 0; i < array.length; i++) {
374                 if (value == array[i]) {
375                     return true;
376                 }
377             }
378 
379             return false;
380         }
381     }
382 
383     public static boolean contains(byte[] array, byte value) {
384         if ((array == null) || (array.length == 0)) {
385             return false;
386         }
387         else {
388             for (int i = 0; i < array.length; i++) {
389                 if (value == array[i]) {
390                     return true;
391                 }
392             }
393 
394             return false;
395         }
396     }
397 
398     public static boolean contains(char[] array, char value) {
399         if ((array == null) || (array.length == 0)) {
400             return false;
401         }
402         else {
403             for (int i = 0; i < array.length; i++) {
404                 if (value == array[i]) {
405                     return true;
406                 }
407             }
408 
409             return false;
410         }
411     }
412 
413     public static boolean contains(double[] array, double value) {
414         if ((array == null) || (array.length == 0)) {
415             return false;
416         }
417         else {
418             for (int i = 0; i < array.length; i++) {
419                 if (value == array[i]) {
420                     return true;
421                 }
422             }
423 
424             return false;
425         }
426     }
427 
428     public static boolean contains(long[] array, long value) {
429         if ((array == null) || (array.length == 0)) {
430             return false;
431         }
432         else {
433             for (int i = 0; i < array.length; i++) {
434                 if (value == array[i]) {
435                     return true;
436                 }
437             }
438 
439             return false;
440         }
441     }
442 
443     public static boolean contains(int[] array, int value) {
444         if ((array == null) || (array.length == 0)) {
445             return false;
446         }
447         else {
448             for (int i = 0; i < array.length; i++) {
449                 if (value == array[i]) {
450                     return true;
451                 }
452             }
453 
454             return false;
455         }
456     }
457 
458     public static boolean contains(short[] array, short value) {
459         if ((array == null) || (array.length == 0)) {
460             return false;
461         }
462         else {
463             for (int i = 0; i < array.length; i++) {
464                 if (value == array[i]) {
465                     return true;
466                 }
467             }
468 
469             return false;
470         }
471     }
472 
473     public static boolean contains(Object[] array, Object value) {
474         if ((array == null) || (array.length == 0) || (value == null)) {
475             return false;
476         }
477         else {
478             for (int i = 0; i < array.length; i++) {
479                 if (value.equals(array[i])) {
480                     return true;
481                 }
482             }
483 
484             return false;
485         }
486     }
487 
488     public static String[] distinct(String[] array) {
489         return distinct(array, null);
490     }
491 
492     public static String[] distinct(
493         String[] array, Comparator<String> comparator) {
494 
495         if ((array == null) || (array.length == 0)) {
496             return array;
497         }
498 
499         Set<String> set = null;
500 
501         if (comparator == null) {
502             set = new TreeSet<String>();
503         }
504         else {
505             set = new TreeSet<String>(comparator);
506         }
507 
508         for (int i = 0; i < array.length; i++) {
509             String s = array[i];
510 
511             if (!set.contains(s)) {
512                 set.add(s);
513             }
514         }
515 
516         return set.toArray(new String[set.size()]);
517     }
518 
519     public static int getLength(Object[] array) {
520         if (array == null) {
521             return 0;
522         }
523         else {
524             return array.length;
525         }
526     }
527 
528     public static Object getValue(Object[] array, int pos) {
529         if ((array == null) || (array.length <= pos)) {
530             return null;
531         }
532         else {
533             return array[pos];
534         }
535     }
536 
537     public static String[] remove(String[] array, String value) {
538         List<String> list = new ArrayList<String>();
539 
540         for (String s : array) {
541             if (!s.equals(value)) {
542                 list.add(s);
543             }
544         }
545 
546         return list.toArray(new String[list.size()]);
547     }
548 
549     public static String[] removeByPrefix(String[] array, String prefix) {
550         List<String> list = new ArrayList<String>();
551 
552         for (String s : array) {
553             if (!s.startsWith(prefix)) {
554                 list.add(s);
555             }
556         }
557 
558         return list.toArray(new String[list.size()]);
559     }
560 
561     public static Boolean[] toArray(boolean[] array) {
562         Boolean[] newArray = new Boolean[array.length];
563 
564         for (int i = 0; i < array.length; i++) {
565             newArray[i] = Boolean.valueOf(array[i]);
566         }
567 
568         return newArray;
569     }
570 
571     public static Byte[] toArray(byte[] array) {
572         Byte[] newArray = new Byte[array.length];
573 
574         for (int i = 0; i < array.length; i++) {
575             newArray[i] = Byte.valueOf(array[i]);
576         }
577 
578         return newArray;
579     }
580 
581     public static Character[] toArray(char[] array) {
582         Character[] newArray = new Character[array.length];
583 
584         for (int i = 0; i < array.length; i++) {
585             newArray[i] = Character.valueOf(array[i]);
586         }
587 
588         return newArray;
589     }
590 
591     public static Double[] toArray(double[] array) {
592         Double[] newArray = new Double[array.length];
593 
594         for (int i = 0; i < array.length; i++) {
595             newArray[i] = new Double(array[i]);
596         }
597 
598         return newArray;
599     }
600 
601     public static Float[] toArray(float[] array) {
602         Float[] newArray = new Float[array.length];
603 
604         for (int i = 0; i < array.length; i++) {
605             newArray[i] = new Float(array[i]);
606         }
607 
608         return newArray;
609     }
610 
611     public static Integer[] toArray(int[] array) {
612         Integer[] newArray = new Integer[array.length];
613 
614         for (int i = 0; i < array.length; i++) {
615             newArray[i] = new Integer(array[i]);
616         }
617 
618         return newArray;
619     }
620 
621     public static Long[] toArray(long[] array) {
622         Long[] newArray = new Long[array.length];
623 
624         for (int i = 0; i < array.length; i++) {
625             newArray[i] = new Long(array[i]);
626         }
627 
628         return newArray;
629     }
630 
631     public static Short[] toArray(short[] array) {
632         Short[] newArray = new Short[array.length];
633 
634         for (int i = 0; i < array.length; i++) {
635             newArray[i] = new Short(array[i]);
636         }
637 
638         return newArray;
639     }
640 
641     public static boolean[] toArray(Boolean[] array) {
642         boolean[] newArray = new boolean[array.length];
643 
644         for (int i = 0; i < array.length; i++) {
645             newArray[i] = array[i].booleanValue();
646         }
647 
648         return newArray;
649     }
650 
651     public static byte[] toArray(Byte[] array) {
652         byte[] newArray = new byte[array.length];
653 
654         for (int i = 0; i < array.length; i++) {
655             newArray[i] = array[i].byteValue();
656         }
657 
658         return newArray;
659     }
660 
661     public static char[] toArray(Character[] array) {
662         char[] newArray = new char[array.length];
663 
664         for (int i = 0; i < array.length; i++) {
665             newArray[i] = array[i].charValue();
666         }
667 
668         return newArray;
669     }
670 
671     public static double[] toArray(Double[] array) {
672         double[] newArray = new double[array.length];
673 
674         for (int i = 0; i < array.length; i++) {
675             newArray[i] = array[i].doubleValue();
676         }
677 
678         return newArray;
679     }
680 
681     public static float[] toArray(Float[] array) {
682         float[] newArray = new float[array.length];
683 
684         for (int i = 0; i < array.length; i++) {
685             newArray[i] = array[i].floatValue();
686         }
687 
688         return newArray;
689     }
690 
691     public static int[] toArray(Integer[] array) {
692         int[] newArray = new int[array.length];
693 
694         for (int i = 0; i < array.length; i++) {
695             newArray[i] = array[i].intValue();
696         }
697 
698         return newArray;
699     }
700 
701     public static long[] toArray(Long[] array) {
702         long[] newArray = new long[array.length];
703 
704         for (int i = 0; i < array.length; i++) {
705             newArray[i] = array[i].longValue();
706         }
707 
708         return newArray;
709     }
710 
711     public static short[] toArray(Short[] array) {
712         short[] newArray = new short[array.length];
713 
714         for (int i = 0; i < array.length; i++) {
715             newArray[i] = array[i].shortValue();
716         }
717 
718         return newArray;
719     }
720 
721     public static String[] toStringArray(boolean[] array) {
722         String[] newArray = new String[array.length];
723 
724         for (int i = 0; i < array.length; i++) {
725             newArray[i] = String.valueOf(array[i]);
726         }
727 
728         return newArray;
729     }
730 
731     public static String[] toStringArray(byte[] array) {
732         String[] newArray = new String[array.length];
733 
734         for (int i = 0; i < array.length; i++) {
735             newArray[i] = String.valueOf(array[i]);
736         }
737 
738         return newArray;
739     }
740 
741     public static String[] toStringArray(char[] array) {
742         String[] newArray = new String[array.length];
743 
744         for (int i = 0; i < array.length; i++) {
745             newArray[i] = String.valueOf(array[i]);
746         }
747 
748         return newArray;
749     }
750 
751     public static String[] toStringArray(double[] array) {
752         String[] newArray = new String[array.length];
753 
754         for (int i = 0; i < array.length; i++) {
755             newArray[i] = String.valueOf(array[i]);
756         }
757 
758         return newArray;
759     }
760 
761     public static String[] toStringArray(float[] array) {
762         String[] newArray = new String[array.length];
763 
764         for (int i = 0; i < array.length; i++) {
765             newArray[i] = String.valueOf(array[i]);
766         }
767 
768         return newArray;
769     }
770 
771     public static String[] toStringArray(int[] array) {
772         String[] newArray = new String[array.length];
773 
774         for (int i = 0; i < array.length; i++) {
775             newArray[i] = String.valueOf(array[i]);
776         }
777 
778         return newArray;
779     }
780 
781     public static String[] toStringArray(long[] array) {
782         String[] newArray = new String[array.length];
783 
784         for (int i = 0; i < array.length; i++) {
785             newArray[i] = String.valueOf(array[i]);
786         }
787 
788         return newArray;
789     }
790 
791     public static String[] toStringArray(short[] array) {
792         String[] newArray = new String[array.length];
793 
794         for (int i = 0; i < array.length; i++) {
795             newArray[i] = String.valueOf(array[i]);
796         }
797 
798         return newArray;
799     }
800 
801     public static String[] toStringArray(Object[] array) {
802         String[] newArray = new String[array.length];
803 
804         for (int i = 0; i < array.length; i++) {
805             newArray[i] = String.valueOf(array[i]);
806         }
807 
808         return newArray;
809     }
810 
811 }