001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    
021    import java.lang.reflect.Array;
022    
023    import java.text.DateFormat;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Comparator;
028    import java.util.Date;
029    import java.util.Iterator;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Set;
033    import java.util.TreeSet;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ArrayUtil {
039    
040            public static boolean[] append(boolean[]... arrays) {
041                    int length = 0;
042    
043                    for (boolean[] array : arrays) {
044                            length += array.length;
045                    }
046    
047                    boolean[] newArray = new boolean[length];
048    
049                    int previousLength = 0;
050    
051                    for (boolean[] array : arrays) {
052                            System.arraycopy(array, 0, newArray, previousLength, array.length);
053    
054                            previousLength += array.length;
055                    }
056    
057                    return newArray;
058            }
059    
060            public static boolean[] append(boolean[] array, boolean value) {
061                    boolean[] newArray = new boolean[array.length + 1];
062    
063                    System.arraycopy(array, 0, newArray, 0, array.length);
064    
065                    newArray[newArray.length - 1] = value;
066    
067                    return newArray;
068            }
069    
070            public static byte[] append(byte[]... arrays) {
071                    int length = 0;
072    
073                    for (byte[] array : arrays) {
074                            length += array.length;
075                    }
076    
077                    byte[] newArray = new byte[length];
078    
079                    int previousLength = 0;
080    
081                    for (byte[] array : arrays) {
082                            System.arraycopy(array, 0, newArray, previousLength, array.length);
083    
084                            previousLength += array.length;
085                    }
086    
087                    return newArray;
088            }
089    
090            public static byte[] append(byte[] array, byte value) {
091                    byte[] newArray = new byte[array.length + 1];
092    
093                    System.arraycopy(array, 0, newArray, 0, array.length);
094    
095                    newArray[newArray.length - 1] = value;
096    
097                    return newArray;
098            }
099    
100            public static char[] append(char[]... arrays) {
101                    int length = 0;
102    
103                    for (char[] array : arrays) {
104                            length += array.length;
105                    }
106    
107                    char[] newArray = new char[length];
108    
109                    int previousLength = 0;
110    
111                    for (char[] array : arrays) {
112                            System.arraycopy(array, 0, newArray, previousLength, array.length);
113    
114                            previousLength += array.length;
115                    }
116    
117                    return newArray;
118            }
119    
120            public static char[] append(char[] array, char value) {
121                    char[] newArray = new char[array.length + 1];
122    
123                    System.arraycopy(array, 0, newArray, 0, array.length);
124    
125                    newArray[newArray.length - 1] = value;
126    
127                    return newArray;
128            }
129    
130            public static double[] append(double[]... arrays) {
131                    int length = 0;
132    
133                    for (double[] array : arrays) {
134                            length += array.length;
135                    }
136    
137                    double[] newArray = new double[length];
138    
139                    int previousLength = 0;
140    
141                    for (double[] array : arrays) {
142                            System.arraycopy(array, 0, newArray, previousLength, array.length);
143    
144                            previousLength += array.length;
145                    }
146    
147                    return newArray;
148            }
149    
150            public static double[] append(double[] array, double value) {
151                    double[] newArray = new double[array.length + 1];
152    
153                    System.arraycopy(array, 0, newArray, 0, array.length);
154    
155                    newArray[newArray.length - 1] = value;
156    
157                    return newArray;
158            }
159    
160            public static float[] append(float[]... arrays) {
161                    int length = 0;
162    
163                    for (float[] array : arrays) {
164                            length += array.length;
165                    }
166    
167                    float[] newArray = new float[length];
168    
169                    int previousLength = 0;
170    
171                    for (float[] array : arrays) {
172                            System.arraycopy(array, 0, newArray, previousLength, array.length);
173    
174                            previousLength += array.length;
175                    }
176    
177                    return newArray;
178            }
179    
180            public static float[] append(float[] array, float value) {
181                    float[] newArray = new float[array.length + 1];
182    
183                    System.arraycopy(array, 0, newArray, 0, array.length);
184    
185                    newArray[newArray.length - 1] = value;
186    
187                    return newArray;
188            }
189    
190            public static int[] append(int[]... arrays) {
191                    int length = 0;
192    
193                    for (int[] array : arrays) {
194                            length += array.length;
195                    }
196    
197                    int[] newArray = new int[length];
198    
199                    int previousLength = 0;
200    
201                    for (int[] array : arrays) {
202                            System.arraycopy(array, 0, newArray, previousLength, array.length);
203    
204                            previousLength += array.length;
205                    }
206    
207                    return newArray;
208            }
209    
210            public static int[] append(int[] array, int value) {
211                    int[] newArray = new int[array.length + 1];
212    
213                    System.arraycopy(array, 0, newArray, 0, array.length);
214    
215                    newArray[newArray.length - 1] = value;
216    
217                    return newArray;
218            }
219    
220            public static long[] append(long[]... arrays) {
221                    int length = 0;
222    
223                    for (long[] array : arrays) {
224                            length += array.length;
225                    }
226    
227                    long[] newArray = new long[length];
228    
229                    int previousLength = 0;
230    
231                    for (long[] array : arrays) {
232                            System.arraycopy(array, 0, newArray, previousLength, array.length);
233    
234                            previousLength += array.length;
235                    }
236    
237                    return newArray;
238            }
239    
240            public static long[] append(long[] array, long value) {
241                    long[] newArray = new long[array.length + 1];
242    
243                    System.arraycopy(array, 0, newArray, 0, array.length);
244    
245                    newArray[newArray.length - 1] = value;
246    
247                    return newArray;
248            }
249    
250            public static short[] append(short[]... arrays) {
251                    int length = 0;
252    
253                    for (short[] array : arrays) {
254                            length += array.length;
255                    }
256    
257                    short[] newArray = new short[length];
258    
259                    int previousLength = 0;
260    
261                    for (short[] array : arrays) {
262                            System.arraycopy(array, 0, newArray, previousLength, array.length);
263    
264                            previousLength += array.length;
265                    }
266    
267                    return newArray;
268            }
269    
270            public static short[] append(short[] array, short value) {
271                    short[] newArray = new short[array.length + 1];
272    
273                    System.arraycopy(array, 0, newArray, 0, array.length);
274    
275                    newArray[newArray.length - 1] = value;
276    
277                    return newArray;
278            }
279    
280            public static <T> T[] append(T[]... arrays) {
281                    int length = 0;
282    
283                    for (T[] array : arrays) {
284                            length += array.length;
285                    }
286    
287                    Class<?> arraysClass = arrays[0].getClass();
288    
289                    T[] newArray = (T[])Array.newInstance(
290                            arraysClass.getComponentType(), length);
291    
292                    int previousLength = 0;
293    
294                    for (T[] array : arrays) {
295                            System.arraycopy(array, 0, newArray, previousLength, array.length);
296    
297                            previousLength += array.length;
298                    }
299    
300                    return newArray;
301            }
302    
303            public static <T> T[] append(T[] array, T value) {
304                    Class<?> arrayClass = array.getClass();
305    
306                    T[] newArray = (T[])Array.newInstance(
307                            arrayClass.getComponentType(), array.length + 1);
308    
309                    System.arraycopy(array, 0, newArray, 0, array.length);
310    
311                    newArray[array.length] = value;
312    
313                    return newArray;
314            }
315    
316            public static <T> T[] append(T[] array1, T[] array2) {
317                    Class<?> array1Class = array1.getClass();
318    
319                    T[] newArray = (T[])Array.newInstance(
320                            array1Class.getComponentType(), array1.length + array2.length);
321    
322                    System.arraycopy(array1, 0, newArray, 0, array1.length);
323    
324                    System.arraycopy(array2, 0, newArray, array1.length, array2.length);
325    
326                    return newArray;
327            }
328    
329            public static <T> T[][] append(T[][] array1, T[] value) {
330                    Class<?> array1Class = array1.getClass();
331    
332                    T[][] newArray = (T[][])Array.newInstance(
333                            array1Class.getComponentType(), array1.length + 1);
334    
335                    System.arraycopy(array1, 0, newArray, 0, array1.length);
336    
337                    newArray[array1.length] = value;
338    
339                    return newArray;
340            }
341    
342            public static <T> T[][] append(T[][] array1, T[][] array2) {
343                    Class<?> array1Class = array1.getClass();
344    
345                    T[][] newArray = (T[][])Array.newInstance(
346                            array1Class.getComponentType(), array1.length + array2.length);
347    
348                    System.arraycopy(array1, 0, newArray, 0, array1.length);
349                    System.arraycopy(array2, 0, newArray, array1.length, array2.length);
350    
351                    return newArray;
352            }
353    
354            public static boolean[] clone(boolean[] array) {
355                    boolean[] newArray = new boolean[array.length];
356    
357                    System.arraycopy(array, 0, newArray, 0, array.length);
358    
359                    return newArray;
360            }
361    
362            public static boolean[] clone(boolean[] array, int from, int to) {
363                    boolean[] newArray = new boolean[to - from];
364    
365                    System.arraycopy(
366                            array, from, newArray, 0,
367                            Math.min(array.length - from, newArray.length));
368    
369                    return newArray;
370            }
371    
372            public static byte[] clone(byte[] array) {
373                    byte[] newArray = new byte[array.length];
374    
375                    System.arraycopy(array, 0, newArray, 0, array.length);
376    
377                    return newArray;
378            }
379    
380            public static byte[] clone(byte[] array, int from, int to) {
381                    byte[] newArray = new byte[to - from];
382    
383                    System.arraycopy(
384                            array, from, newArray, 0,
385                            Math.min(array.length - from, newArray.length));
386    
387                    return newArray;
388            }
389    
390            public static char[] clone(char[] array) {
391                    char[] newArray = new char[array.length];
392    
393                    System.arraycopy(array, 0, newArray, 0, array.length);
394    
395                    return newArray;
396            }
397    
398            public static char[] clone(char[] array, int from, int to) {
399                    char[] newArray = new char[to - from];
400    
401                    System.arraycopy(
402                            array, from, newArray, 0,
403                            Math.min(array.length - from, newArray.length));
404    
405                    return newArray;
406            }
407    
408            public static double[] clone(double[] array) {
409                    double[] newArray = new double[array.length];
410    
411                    System.arraycopy(array, 0, newArray, 0, array.length);
412    
413                    return newArray;
414            }
415    
416            public static double[] clone(double[] array, int from, int to) {
417                    double[] newArray = new double[to - from];
418    
419                    System.arraycopy(
420                            array, from, newArray, 0,
421                            Math.min(array.length - from, newArray.length));
422    
423                    return newArray;
424            }
425    
426            public static float[] clone(float[] array) {
427                    float[] newArray = new float[array.length];
428    
429                    System.arraycopy(array, 0, newArray, 0, array.length);
430    
431                    return newArray;
432            }
433    
434            public static float[] clone(float[] array, int from, int to) {
435                    float[] newArray = new float[to - from];
436    
437                    System.arraycopy(
438                            array, from, newArray, 0,
439                            Math.min(array.length - from, newArray.length));
440    
441                    return newArray;
442            }
443    
444            public static int[] clone(int[] array) {
445                    int[] newArray = new int[array.length];
446    
447                    System.arraycopy(array, 0, newArray, 0, array.length);
448    
449                    return newArray;
450            }
451    
452            public static int[] clone(int[] array, int from, int to) {
453                    int[] newArray = new int[to - from];
454    
455                    System.arraycopy(
456                            array, from, newArray, 0,
457                            Math.min(array.length - from, newArray.length));
458    
459                    return newArray;
460            }
461    
462            public static long[] clone(long[] array) {
463                    long[] newArray = new long[array.length];
464    
465                    System.arraycopy(array, 0, newArray, 0, array.length);
466    
467                    return newArray;
468            }
469    
470            public static long[] clone(long[] array, int from, int to) {
471                    long[] newArray = new long[to - from];
472    
473                    System.arraycopy(
474                            array, from, newArray, 0,
475                            Math.min(array.length - from, newArray.length));
476    
477                    return newArray;
478            }
479    
480            public static short[] clone(short[] array) {
481                    short[] newArray = new short[array.length];
482    
483                    System.arraycopy(array, 0, newArray, 0, array.length);
484    
485                    return newArray;
486            }
487    
488            public static short[] clone(short[] array, int from, int to) {
489                    short[] newArray = new short[to - from];
490    
491                    System.arraycopy(
492                            array, from, newArray, 0,
493                            Math.min(array.length - from, newArray.length));
494    
495                    return newArray;
496            }
497    
498            public static <T> T[] clone(T[] array) {
499                    Class<?> arrayClass = array.getClass();
500    
501                    T[] newArray = (T[])Array.newInstance(
502                            arrayClass.getComponentType(), array.length);
503    
504                    System.arraycopy(array, 0, newArray, 0, array.length);
505    
506                    return newArray;
507            }
508    
509            public static <T> T[] clone(T[] array, int from, int to) {
510                    Class<?> arrayClass = array.getClass();
511    
512                    T[] newArray = (T[])Array.newInstance(
513                            arrayClass.getComponentType(), to - from);
514    
515                    System.arraycopy(
516                            array, from, newArray, 0,
517                            Math.min(array.length - from, newArray.length));
518    
519                    return newArray;
520            }
521    
522            public static <T> T[][] clone(T[][] array) {
523                    Class<?> arrayClass = array.getClass();
524    
525                    T[][] newArray = (T[][])Array.newInstance(
526                            arrayClass.getComponentType(), array.length);
527    
528                    System.arraycopy(array, 0, newArray, 0, array.length);
529    
530                    return newArray;
531            }
532    
533            public static <T> T[][] clone(T[][] array, int from, int to) {
534                    Class<?> arrayClass = array.getClass();
535    
536                    T[][] newArray = (T[][])Array.newInstance(
537                            arrayClass.getComponentType(), to - from);
538    
539                    System.arraycopy(
540                            array, from, newArray, 0,
541                            Math.min(array.length - from, newArray.length));
542    
543                    return newArray;
544            }
545    
546            public static void combine(
547                    Object[] array1, Object[] array2, Object[] combinedArray) {
548    
549                    System.arraycopy(array1, 0, combinedArray, 0, array1.length);
550    
551                    System.arraycopy(
552                            array2, 0, combinedArray, array1.length, array2.length);
553            }
554    
555            public static boolean contains(boolean[] array, boolean value) {
556                    if ((array == null) || (array.length == 0)) {
557                            return false;
558                    }
559    
560                    for (int i = 0; i < array.length; i++) {
561                            if (value == array[i]) {
562                                    return true;
563                            }
564                    }
565    
566                    return false;
567            }
568    
569            public static boolean contains(byte[] array, byte value) {
570                    if ((array == null) || (array.length == 0)) {
571                            return false;
572                    }
573    
574                    for (int i = 0; i < array.length; i++) {
575                            if (value == array[i]) {
576                                    return true;
577                            }
578                    }
579    
580                    return false;
581            }
582    
583            public static boolean contains(char[] array, char value) {
584                    if ((array == null) || (array.length == 0)) {
585                            return false;
586                    }
587    
588                    for (int i = 0; i < array.length; i++) {
589                            if (value == array[i]) {
590                                    return true;
591                            }
592                    }
593    
594                    return false;
595            }
596    
597            public static boolean contains(double[] array, double value) {
598                    if ((array == null) || (array.length == 0)) {
599                            return false;
600                    }
601    
602                    for (int i = 0; i < array.length; i++) {
603                            if (value == array[i]) {
604                                    return true;
605                            }
606                    }
607    
608                    return false;
609            }
610    
611            public static boolean contains(float[] array, float value) {
612                    if ((array == null) || (array.length == 0)) {
613                            return false;
614                    }
615    
616                    for (int i = 0; i < array.length; i++) {
617                            if (value == array[i]) {
618                                    return true;
619                            }
620                    }
621    
622                    return false;
623            }
624    
625            public static boolean contains(int[] array, int value) {
626                    if ((array == null) || (array.length == 0)) {
627                            return false;
628                    }
629    
630                    for (int i = 0; i < array.length; i++) {
631                            if (value == array[i]) {
632                                    return true;
633                            }
634                    }
635    
636                    return false;
637            }
638    
639            public static boolean contains(long[] array, long value) {
640                    if ((array == null) || (array.length == 0)) {
641                            return false;
642                    }
643    
644                    for (int i = 0; i < array.length; i++) {
645                            if (value == array[i]) {
646                                    return true;
647                            }
648                    }
649    
650                    return false;
651            }
652    
653            public static boolean contains(Object[] array, Object value) {
654                    if ((array == null) || (array.length == 0) || (value == null)) {
655                            return false;
656                    }
657    
658                    for (int i = 0; i < array.length; i++) {
659                            if (value.equals(array[i])) {
660                                    return true;
661                            }
662                    }
663    
664                    return false;
665            }
666    
667            public static boolean contains(short[] array, short value) {
668                    if ((array == null) || (array.length == 0)) {
669                            return false;
670                    }
671    
672                    for (int i = 0; i < array.length; i++) {
673                            if (value == array[i]) {
674                                    return true;
675                            }
676                    }
677    
678                    return false;
679            }
680    
681            public static String[] distinct(String[] array) {
682                    return distinct(array, null);
683            }
684    
685            public static String[] distinct(
686                    String[] array, Comparator<String> comparator) {
687    
688                    if ((array == null) || (array.length == 0)) {
689                            return array;
690                    }
691    
692                    Set<String> set = null;
693    
694                    if (comparator == null) {
695                            set = new TreeSet<String>();
696                    }
697                    else {
698                            set = new TreeSet<String>(comparator);
699                    }
700    
701                    for (int i = 0; i < array.length; i++) {
702                            String s = array[i];
703    
704                            if (!set.contains(s)) {
705                                    set.add(s);
706                            }
707                    }
708    
709                    return set.toArray(new String[set.size()]);
710            }
711    
712            public static int getLength(Object[] array) {
713                    if (array == null) {
714                            return 0;
715                    }
716                    else {
717                            return array.length;
718                    }
719            }
720    
721            public static Object getValue(Object[] array, int pos) {
722                    if ((array == null) || (array.length <= pos)) {
723                            return null;
724                    }
725                    else {
726                            return array[pos];
727                    }
728            }
729    
730            public static boolean[] remove(boolean[] array, boolean value) {
731                    List<Boolean> list = new ArrayList<Boolean>();
732    
733                    for (int i = 0; i < array.length; i++) {
734                            if (value != array[i]) {
735                                    list.add(new Boolean(array[i]));
736                            }
737                    }
738    
739                    return toArray(list.toArray(new Boolean[list.size()]));
740            }
741    
742            public static byte[] remove(byte[] array, byte value) {
743                    List<Byte> list = new ArrayList<Byte>();
744    
745                    for (int i = 0; i < array.length; i++) {
746                            if (value != array[i]) {
747                                    list.add(new Byte(array[i]));
748                            }
749                    }
750    
751                    return toArray(list.toArray(new Byte[list.size()]));
752            }
753    
754            public static char[] remove(char[] array, char value) {
755                    List<Character> list = new ArrayList<Character>();
756    
757                    for (int i = 0; i < array.length; i++) {
758                            if (value != array[i]) {
759                                    list.add(new Character(array[i]));
760                            }
761                    }
762    
763                    return toArray(list.toArray(new Character[list.size()]));
764            }
765    
766            public static double[] remove(double[] array, double value) {
767                    List<Double> list = new ArrayList<Double>();
768    
769                    for (int i = 0; i < array.length; i++) {
770                            if (value != array[i]) {
771                                    list.add(new Double(array[i]));
772                            }
773                    }
774    
775                    return toArray(list.toArray(new Double[list.size()]));
776            }
777    
778            public static int[] remove(int[] array, int value) {
779                    List<Integer> list = new ArrayList<Integer>();
780    
781                    for (int i = 0; i < array.length; i++) {
782                            if (value != array[i]) {
783                                    list.add(new Integer(array[i]));
784                            }
785                    }
786    
787                    return toArray(list.toArray(new Integer[list.size()]));
788            }
789    
790            public static long[] remove(long[] array, long value) {
791                    List<Long> list = new ArrayList<Long>();
792    
793                    for (int i = 0; i < array.length; i++) {
794                            if (value != array[i]) {
795                                    list.add(new Long(array[i]));
796                            }
797                    }
798    
799                    return toArray(list.toArray(new Long[list.size()]));
800            }
801    
802            public static short[] remove(short[] array, short value) {
803                    List<Short> list = new ArrayList<Short>();
804    
805                    for (int i = 0; i < array.length; i++) {
806                            if (value != array[i]) {
807                                    list.add(new Short(array[i]));
808                            }
809                    }
810    
811                    return toArray(list.toArray(new Short[list.size()]));
812            }
813    
814            public static String[] remove(String[] array, String value) {
815                    List<String> list = new ArrayList<String>();
816    
817                    for (String s : array) {
818                            if (!s.equals(value)) {
819                                    list.add(s);
820                            }
821                    }
822    
823                    return list.toArray(new String[list.size()]);
824            }
825    
826            public static String[] removeByPrefix(String[] array, String prefix) {
827                    List<String> list = new ArrayList<String>();
828    
829                    for (String s : array) {
830                            if (!s.startsWith(prefix)) {
831                                    list.add(s);
832                            }
833                    }
834    
835                    return list.toArray(new String[list.size()]);
836            }
837    
838            public static void reverse(String[] array) {
839                    for (int left = 0, right = array.length - 1; left < right;
840                                    left++, right--) {
841    
842                            String value = array[left];
843    
844                            array[left] = array[right];
845                            array[right] = value;
846                    }
847            }
848    
849            public static boolean[] subset(boolean[] array, int start, int end) {
850                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
851                            return array;
852                    }
853    
854                    boolean[] newArray = new boolean[end - start];
855    
856                    System.arraycopy(array, start, newArray, 0, end - start);
857    
858                    return newArray;
859            }
860    
861            public static byte[] subset(byte[] array, int start, int end) {
862                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
863                            return array;
864                    }
865    
866                    byte[] newArray = new byte[end - start];
867    
868                    System.arraycopy(array, start, newArray, 0, end - start);
869    
870                    return newArray;
871            }
872    
873            public static char[] subset(char[] array, int start, int end) {
874                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
875                            return array;
876                    }
877    
878                    char[] newArray = new char[end - start];
879    
880                    System.arraycopy(array, start, newArray, 0, end - start);
881    
882                    return newArray;
883            }
884    
885            public static double[] subset(double[] array, int start, int end) {
886                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
887                            return array;
888                    }
889    
890                    double[] newArray = new double[end - start];
891    
892                    System.arraycopy(array, start, newArray, 0, end - start);
893    
894                    return newArray;
895            }
896    
897            public static float[] subset(float[] array, int start, int end) {
898                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
899                            return array;
900                    }
901    
902                    float[] newArray = new float[end - start];
903    
904                    System.arraycopy(array, start, newArray, 0, end - start);
905    
906                    return newArray;
907            }
908    
909            public static int[] subset(int[] array, int start, int end) {
910                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
911                            return array;
912                    }
913    
914                    int[] newArray = new int[end - start];
915    
916                    System.arraycopy(array, start, newArray, 0, end - start);
917    
918                    return newArray;
919            }
920    
921            public static long[] subset(long[] array, int start, int end) {
922                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
923                            return array;
924                    }
925    
926                    long[] newArray = new long[end - start];
927    
928                    System.arraycopy(array, start, newArray, 0, end - start);
929    
930                    return newArray;
931            }
932    
933            public static short[] subset(short[] array, int start, int end) {
934                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
935                            return array;
936                    }
937    
938                    short[] newArray = new short[end - start];
939    
940                    System.arraycopy(array, start, newArray, 0, end - start);
941    
942                    return newArray;
943            }
944    
945            public static <T> T[] subset(T[] array, int start, int end) {
946                    if ((start < 0) || (end < 0) || ((end - start) < 0)) {
947                            return array;
948                    }
949    
950                    Class<?> arrayClass = array.getClass();
951    
952                    T[] newArray = (T[])Array.newInstance(
953                            arrayClass.getComponentType(), end - start);
954    
955                    System.arraycopy(array, start, newArray, 0, end - start);
956    
957                    return newArray;
958            }
959    
960            public static Boolean[] toArray(boolean[] array) {
961                    Boolean[] newArray = new Boolean[array.length];
962    
963                    for (int i = 0; i < array.length; i++) {
964                            newArray[i] = Boolean.valueOf(array[i]);
965                    }
966    
967                    return newArray;
968            }
969    
970            public static boolean[] toArray(Boolean[] array) {
971                    boolean[] newArray = new boolean[array.length];
972    
973                    for (int i = 0; i < array.length; i++) {
974                            newArray[i] = array[i].booleanValue();
975                    }
976    
977                    return newArray;
978            }
979    
980            public static Byte[] toArray(byte[] array) {
981                    Byte[] newArray = new Byte[array.length];
982    
983                    for (int i = 0; i < array.length; i++) {
984                            newArray[i] = Byte.valueOf(array[i]);
985                    }
986    
987                    return newArray;
988            }
989    
990            public static byte[] toArray(Byte[] array) {
991                    byte[] newArray = new byte[array.length];
992    
993                    for (int i = 0; i < array.length; i++) {
994                            newArray[i] = array[i].byteValue();
995                    }
996    
997                    return newArray;
998            }
999    
1000            public static Character[] toArray(char[] array) {
1001                    Character[] newArray = new Character[array.length];
1002    
1003                    for (int i = 0; i < array.length; i++) {
1004                            newArray[i] = Character.valueOf(array[i]);
1005                    }
1006    
1007                    return newArray;
1008            }
1009    
1010            public static char[] toArray(Character[] array) {
1011                    char[] newArray = new char[array.length];
1012    
1013                    for (int i = 0; i < array.length; i++) {
1014                            newArray[i] = array[i].charValue();
1015                    }
1016    
1017                    return newArray;
1018            }
1019    
1020            public static Double[] toArray(double[] array) {
1021                    Double[] newArray = new Double[array.length];
1022    
1023                    for (int i = 0; i < array.length; i++) {
1024                            newArray[i] = new Double(array[i]);
1025                    }
1026    
1027                    return newArray;
1028            }
1029    
1030            public static double[] toArray(Double[] array) {
1031                    double[] newArray = new double[array.length];
1032    
1033                    for (int i = 0; i < array.length; i++) {
1034                            newArray[i] = array[i].doubleValue();
1035                    }
1036    
1037                    return newArray;
1038            }
1039    
1040            public static Float[] toArray(float[] array) {
1041                    Float[] newArray = new Float[array.length];
1042    
1043                    for (int i = 0; i < array.length; i++) {
1044                            newArray[i] = new Float(array[i]);
1045                    }
1046    
1047                    return newArray;
1048            }
1049    
1050            public static float[] toArray(Float[] array) {
1051                    float[] newArray = new float[array.length];
1052    
1053                    for (int i = 0; i < array.length; i++) {
1054                            newArray[i] = array[i].floatValue();
1055                    }
1056    
1057                    return newArray;
1058            }
1059    
1060            public static Integer[] toArray(int[] array) {
1061                    Integer[] newArray = new Integer[array.length];
1062    
1063                    for (int i = 0; i < array.length; i++) {
1064                            newArray[i] = new Integer(array[i]);
1065                    }
1066    
1067                    return newArray;
1068            }
1069    
1070            public static int[] toArray(Integer[] array) {
1071                    int[] newArray = new int[array.length];
1072    
1073                    for (int i = 0; i < array.length; i++) {
1074                            newArray[i] = array[i].intValue();
1075                    }
1076    
1077                    return newArray;
1078            }
1079    
1080            public static Long[] toArray(long[] array) {
1081                    Long[] newArray = new Long[array.length];
1082    
1083                    for (int i = 0; i < array.length; i++) {
1084                            newArray[i] = new Long(array[i]);
1085                    }
1086    
1087                    return newArray;
1088            }
1089    
1090            public static long[] toArray(Long[] array) {
1091                    long[] newArray = new long[array.length];
1092    
1093                    for (int i = 0; i < array.length; i++) {
1094                            newArray[i] = array[i].longValue();
1095                    }
1096    
1097                    return newArray;
1098            }
1099    
1100            public static Short[] toArray(short[] array) {
1101                    Short[] newArray = new Short[array.length];
1102    
1103                    for (int i = 0; i < array.length; i++) {
1104                            newArray[i] = new Short(array[i]);
1105                    }
1106    
1107                    return newArray;
1108            }
1109    
1110            public static short[] toArray(Short[] array) {
1111                    short[] newArray = new short[array.length];
1112    
1113                    for (int i = 0; i < array.length; i++) {
1114                            newArray[i] = array[i].shortValue();
1115                    }
1116    
1117                    return newArray;
1118            }
1119    
1120            public static double[] toDoubleArray(Collection<Double> collection) {
1121                    double[] newArray = new double[collection.size()];
1122    
1123                    if (collection instanceof List) {
1124                            List<Double> list = (List<Double>)collection;
1125    
1126                            for (int i = 0; i < list.size(); i++) {
1127                                    Double value = list.get(i);
1128    
1129                                    newArray[i] = value.doubleValue();
1130                            }
1131                    }
1132                    else {
1133                            int i = 0;
1134    
1135                            Iterator<Double> iterator = collection.iterator();
1136    
1137                            while (iterator.hasNext()) {
1138                                    Double value = iterator.next();
1139    
1140                                    newArray[i++] = value.doubleValue();
1141                            }
1142                    }
1143    
1144                    return newArray;
1145            }
1146    
1147            public static float[] toFloatArray(Collection<Float> collection) {
1148                    float[] newArray = new float[collection.size()];
1149    
1150                    if (collection instanceof List) {
1151                            List<Float> list = (List<Float>)collection;
1152    
1153                            for (int i = 0; i < list.size(); i++) {
1154                                    Float value = list.get(i);
1155    
1156                                    newArray[i] = value.floatValue();
1157                            }
1158                    }
1159                    else {
1160                            int i = 0;
1161    
1162                            Iterator<Float> iterator = collection.iterator();
1163    
1164                            while (iterator.hasNext()) {
1165                                    Float value = iterator.next();
1166    
1167                                    newArray[i++] = value.floatValue();
1168                            }
1169                    }
1170    
1171                    return newArray;
1172            }
1173    
1174            public static int[] toIntArray(Collection<Integer> collection) {
1175                    int[] newArray = new int[collection.size()];
1176    
1177                    if (collection instanceof List) {
1178                            List<Integer> list = (List<Integer>)collection;
1179    
1180                            for (int i = 0; i < list.size(); i++) {
1181                                    Integer value = list.get(i);
1182    
1183                                    newArray[i] = value.intValue();
1184                            }
1185                    }
1186                    else {
1187                            int i = 0;
1188    
1189                            Iterator<Integer> iterator = collection.iterator();
1190    
1191                            while (iterator.hasNext()) {
1192                                    Integer value = iterator.next();
1193    
1194                                    newArray[i++] = value.intValue();
1195                            }
1196                    }
1197    
1198                    return newArray;
1199            }
1200    
1201            public static long[] toLongArray(Collection<Long> collection) {
1202                    long[] newArray = new long[collection.size()];
1203    
1204                    if (collection instanceof List) {
1205                            List<Long> list = (List<Long>)collection;
1206    
1207                            for (int i = 0; i < list.size(); i++) {
1208                                    Long value = list.get(i);
1209    
1210                                    newArray[i] = value.longValue();
1211                            }
1212                    }
1213                    else {
1214                            int i = 0;
1215    
1216                            Iterator<Long> iterator = collection.iterator();
1217    
1218                            while (iterator.hasNext()) {
1219                                    Long value = iterator.next();
1220    
1221                                    newArray[i++] = value.longValue();
1222                            }
1223                    }
1224    
1225                    return newArray;
1226            }
1227    
1228            public static Long[] toLongArray(Object[] array) {
1229                    Long[] newArray = new Long[array.length];
1230    
1231                    for (int i = 0; i < array.length; i++) {
1232                            newArray[i] = (Long)array[i];
1233                    }
1234    
1235                    return newArray;
1236            }
1237    
1238            public static short[] toShortArray(Collection<Short> collection) {
1239                    short[] newArray = new short[collection.size()];
1240    
1241                    if (collection instanceof List) {
1242                            List<Short> list = (List<Short>)collection;
1243    
1244                            for (int i = 0; i < list.size(); i++) {
1245                                    Short value = list.get(i);
1246    
1247                                    newArray[i] = value.shortValue();
1248                            }
1249                    }
1250                    else {
1251                            int i = 0;
1252    
1253                            Iterator<Short> iterator = collection.iterator();
1254    
1255                            while (iterator.hasNext()) {
1256                                    Short value = iterator.next();
1257    
1258                                    newArray[i++] = value.shortValue();
1259                            }
1260                    }
1261    
1262                    return newArray;
1263            }
1264    
1265            /**
1266             * @see ListUtil#toString(List, String)
1267             */
1268            public static String toString(Object[] array, String param) {
1269                    return toString(array, param, StringPool.COMMA);
1270            }
1271    
1272            /**
1273             * @see ListUtil#toString(List, String, String)
1274             */
1275            public static String toString(
1276                    Object[] array, String param, String delimiter) {
1277    
1278                    return toString(array, param, delimiter, null);
1279            }
1280    
1281            public static String toString(
1282                    Object[] array, String param, String delimiter, Locale locale) {
1283    
1284                    if ((array == null) || (array.length == 0)) {
1285                            return StringPool.BLANK;
1286                    }
1287    
1288                    StringBundler sb = new StringBundler(2 * array.length - 1);
1289    
1290                    for (int i = 0; i < array.length; i++) {
1291                            Object bean = array[i];
1292    
1293                            Object value = BeanPropertiesUtil.getObject(bean, param);
1294    
1295                            if (value != null) {
1296                                    if (locale != null) {
1297                                            sb.append(LanguageUtil.get(locale, value.toString()));
1298                                    }
1299                                    else {
1300                                            sb.append(value);
1301                                    }
1302                            }
1303    
1304                            if ((i + 1) != array.length) {
1305                                    sb.append(delimiter);
1306                            }
1307                    }
1308    
1309                    return sb.toString();
1310            }
1311    
1312            /**
1313             * @see ListUtil#toString(List, Accessor)
1314             */
1315            public static <T, V> String toString(T[] list, Accessor<T, V> accessor) {
1316                    return toString(list, accessor, StringPool.COMMA);
1317            }
1318    
1319            /**
1320             * @see ListUtil#toString(List, Accessor, String)
1321             */
1322            public static <T, V> String toString(
1323                    T[] list, Accessor<T, V> accessor, String delimiter) {
1324    
1325                    return toString(list, accessor, delimiter, null);
1326            }
1327    
1328            public static <T, V> String toString(
1329                    T[] list, Accessor<T, V> accessor, String delimiter, Locale locale) {
1330    
1331                    if ((list == null) || (list.length == 0)) {
1332                            return StringPool.BLANK;
1333                    }
1334    
1335                    StringBundler sb = new StringBundler(2 * list.length - 1);
1336    
1337                    for (int i = 0; i < list.length; i++) {
1338                            T bean = list[i];
1339    
1340                            V value = accessor.get(bean);
1341    
1342                            if (value != null) {
1343                                    if (locale != null) {
1344                                            sb.append(LanguageUtil.get(locale, value.toString()));
1345                                    }
1346                                    else {
1347                                            sb.append(value);
1348                                    }
1349                            }
1350    
1351                            if ((i + 1) != list.length) {
1352                                    sb.append(delimiter);
1353                            }
1354                    }
1355    
1356                    return sb.toString();
1357            }
1358    
1359            public static String[] toStringArray(boolean[] array) {
1360                    String[] newArray = new String[array.length];
1361    
1362                    for (int i = 0; i < array.length; i++) {
1363                            newArray[i] = String.valueOf(array[i]);
1364                    }
1365    
1366                    return newArray;
1367            }
1368    
1369            public static String[] toStringArray(byte[] array) {
1370                    String[] newArray = new String[array.length];
1371    
1372                    for (int i = 0; i < array.length; i++) {
1373                            newArray[i] = String.valueOf(array[i]);
1374                    }
1375    
1376                    return newArray;
1377            }
1378    
1379            public static String[] toStringArray(char[] array) {
1380                    String[] newArray = new String[array.length];
1381    
1382                    for (int i = 0; i < array.length; i++) {
1383                            newArray[i] = String.valueOf(array[i]);
1384                    }
1385    
1386                    return newArray;
1387            }
1388    
1389            public static String[] toStringArray(Date[] array, DateFormat dateFormat) {
1390                    String[] newArray = new String[array.length];
1391    
1392                    for (int i = 0; i < array.length; i++) {
1393                            newArray[i] = dateFormat.format(array[i]);
1394                    }
1395    
1396                    return newArray;
1397            }
1398    
1399            public static String[] toStringArray(double[] array) {
1400                    String[] newArray = new String[array.length];
1401    
1402                    for (int i = 0; i < array.length; i++) {
1403                            newArray[i] = String.valueOf(array[i]);
1404                    }
1405    
1406                    return newArray;
1407            }
1408    
1409            public static String[] toStringArray(float[] array) {
1410                    String[] newArray = new String[array.length];
1411    
1412                    for (int i = 0; i < array.length; i++) {
1413                            newArray[i] = String.valueOf(array[i]);
1414                    }
1415    
1416                    return newArray;
1417            }
1418    
1419            public static String[] toStringArray(int[] array) {
1420                    String[] newArray = new String[array.length];
1421    
1422                    for (int i = 0; i < array.length; i++) {
1423                            newArray[i] = String.valueOf(array[i]);
1424                    }
1425    
1426                    return newArray;
1427            }
1428    
1429            public static String[] toStringArray(JSONArray array) {
1430                    String[] newArray = new String[array.length()];
1431    
1432                    for (int i = 0; i < array.length(); i++) {
1433                            newArray[i] = array.getString(i);
1434                    }
1435    
1436                    return newArray;
1437            }
1438    
1439            public static String[] toStringArray(long[] array) {
1440                    String[] newArray = new String[array.length];
1441    
1442                    for (int i = 0; i < array.length; i++) {
1443                            newArray[i] = String.valueOf(array[i]);
1444                    }
1445    
1446                    return newArray;
1447            }
1448    
1449            public static String[] toStringArray(Object[] array) {
1450                    String[] newArray = new String[array.length];
1451    
1452                    for (int i = 0; i < array.length; i++) {
1453                            newArray[i] = String.valueOf(array[i]);
1454                    }
1455    
1456                    return newArray;
1457            }
1458    
1459            public static String[] toStringArray(short[] array) {
1460                    String[] newArray = new String[array.length];
1461    
1462                    for (int i = 0; i < array.length; i++) {
1463                            newArray[i] = String.valueOf(array[i]);
1464                    }
1465    
1466                    return newArray;
1467            }
1468    
1469            public static byte[] unique(byte[] array) {
1470                    List<Byte> list = new UniqueList<Byte>();
1471    
1472                    for (int i = 0; i < array.length; i++) {
1473                            list.add(array[i]);
1474                    }
1475    
1476                    return toArray(list.toArray(new Byte[list.size()]));
1477            }
1478    
1479            public static double[] unique(double[] array) {
1480                    List<Double> list = new UniqueList<Double>();
1481    
1482                    for (int i = 0; i < array.length; i++) {
1483                            list.add(array[i]);
1484                    }
1485    
1486                    return toArray(list.toArray(new Double[list.size()]));
1487            }
1488    
1489            public static float[] unique(float[] array) {
1490                    List<Float> list = new UniqueList<Float>();
1491    
1492                    for (int i = 0; i < array.length; i++) {
1493                            list.add(array[i]);
1494                    }
1495    
1496                    return toArray(list.toArray(new Float[list.size()]));
1497            }
1498    
1499            public static int[] unique(int[] array) {
1500                    List<Integer> list = new UniqueList<Integer>();
1501    
1502                    for (int i = 0; i < array.length; i++) {
1503                            list.add(array[i]);
1504                    }
1505    
1506                    return toArray(list.toArray(new Integer[list.size()]));
1507            }
1508    
1509            public static long[] unique(long[] array) {
1510                    List<Long> list = new UniqueList<Long>();
1511    
1512                    for (int i = 0; i < array.length; i++) {
1513                            list.add(array[i]);
1514                    }
1515    
1516                    return toArray(list.toArray(new Long[list.size()]));
1517            }
1518    
1519            public static short[] unique(short[] array) {
1520                    List<Short> list = new UniqueList<Short>();
1521    
1522                    for (int i = 0; i < array.length; i++) {
1523                            list.add(array[i]);
1524                    }
1525    
1526                    return toArray(list.toArray(new Short[list.size()]));
1527            }
1528    
1529    }