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