001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.util;
021
022
023 import java.lang.reflect.Array;
024
025
026 /**
027 * <p>
028 * Operations on arrays, primitive arrays (like <code>int[]</code>) and
029 * primitive wrapper arrays (like <code>Integer[]</code>).
030 * </p>
031 * <p>
032 * This class tries to handle <code>null</code> input gracefully. An exception
033 * will not be thrown for a <code>null</code> array input. However, an Object
034 * array that contains a <code>null</code> element may throw an exception.
035 * Each method documents its behaviour.
036 * </p>
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @since 2.0
040 */
041 public class ArrayUtils
042 {
043
044 /**
045 * An empty immutable <code>Object</code> array.
046 */
047 public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
048
049 /**
050 * An empty immutable <code>Class</code> array.
051 */
052 public static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
053
054 /**
055 * An empty immutable <code>String</code> array.
056 */
057 public static final String[] EMPTY_STRING_ARRAY = new String[0];
058
059 /**
060 * An empty immutable <code>long</code> array.
061 */
062 public static final long[] EMPTY_LONG_ARRAY = new long[0];
063
064 /**
065 * An empty immutable <code>Long</code> array.
066 */
067 public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
068
069 /**
070 * An empty immutable <code>int</code> array.
071 */
072 public static final int[] EMPTY_INT_ARRAY = new int[0];
073
074 /**
075 * An empty immutable <code>Integer</code> array.
076 */
077 public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
078
079 /**
080 * An empty immutable <code>short</code> array.
081 */
082 public static final short[] EMPTY_SHORT_ARRAY = new short[0];
083
084 /**
085 * An empty immutable <code>Short</code> array.
086 */
087 public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
088
089 /**
090 * An empty immutable <code>byte</code> array.
091 */
092 public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
093
094 /**
095 * An empty immutable <code>Byte</code> array.
096 */
097 public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
098
099 /**
100 * An empty immutable <code>double</code> array.
101 */
102 public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
103
104 /**
105 * An empty immutable <code>Double</code> array.
106 */
107 public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
108
109 /**
110 * An empty immutable <code>float</code> array.
111 */
112 public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
113
114 /**
115 * An empty immutable <code>Float</code> array.
116 */
117 public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
118
119 /**
120 * An empty immutable <code>boolean</code> array.
121 */
122 public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
123
124 /**
125 * An empty immutable <code>Boolean</code> array.
126 */
127 public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
128
129 /**
130 * An empty immutable <code>char</code> array.
131 */
132 public static final char[] EMPTY_CHAR_ARRAY = new char[0];
133
134 /**
135 * An empty immutable <code>Character</code> array.
136 */
137 public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
138
139
140 /**
141 * <p>
142 * ArrayUtils instances should NOT be constructed in standard programming.
143 * Instead, the class should be used as
144 * <code>ArrayUtils.clone(new int[] {2})</code>.
145 * </p>
146 * <p>
147 * This constructor is public to permit tools that require a JavaBean
148 * instance to operate.
149 * </p>
150 */
151 public ArrayUtils()
152 {
153 }
154
155
156 // Basic methods handling multi-dimensional arrays
157 // -----------------------------------------------------------------------
158 /**
159 * <p>
160 * Outputs an array as a String, treating <code>null</code> as an empty
161 * array.
162 * </p>
163 * <p>
164 * Multi-dimensional arrays are handled correctly, including
165 * multi-dimensional primitive arrays.
166 * </p>
167 * <p>
168 * The format is that of Java source code, for example <code>{a,b}</code>.
169 * </p>
170 *
171 * @param array
172 * the array to get a toString for, may be <code>null</code>
173 * @return a String representation of the array, '{}' if null array input
174 */
175 public static String toString( Object array )
176 {
177 return toString( array, "{}" );
178 }
179
180
181 /**
182 * <p>
183 * Outputs an array as a String handling <code>null</code>s.
184 * </p>
185 * <p>
186 * Multi-dimensional arrays are handled correctly, including
187 * multi-dimensional primitive arrays.
188 * </p>
189 * <p>
190 * The format is that of Java source code, for example <code>{a,b}</code>.
191 * </p>
192 *
193 * @param array
194 * the array to get a toString for, may be <code>null</code>
195 * @param stringIfNull
196 * the String to return if the array is <code>null</code>
197 * @return a String representation of the array
198 */
199 public static String toString( Object array, String stringIfNull )
200 {
201 if ( array == null )
202 {
203 return stringIfNull;
204 }
205
206 return new ToStringBuilder( array, ToStringStyle.SIMPLE_STYLE ).append( array ).toString();
207 }
208
209
210 /**
211 * <p>
212 * Get a hashCode for an array handling multi-dimensional arrays correctly.
213 * </p>
214 * <p>
215 * Multi-dimensional primitive arrays are also handled correctly by this
216 * method.
217 * </p>
218 *
219 * @param array
220 * the array to get a hashCode for, may be <code>null</code>
221 * @return a hashCode for the array, zero if null array input
222 */
223 public static int hashCode( Object array )
224 {
225 return new HashCodeBuilder().append( array ).toHashCode();
226 }
227
228
229 /**
230 * <p>
231 * Compares two arrays, using equals(), handling multi-dimensional arrays
232 * correctly.
233 * </p>
234 * <p>
235 * Multi-dimensional primitive arrays are also handled correctly by this
236 * method.
237 * </p>
238 *
239 * @param array1
240 * the left hand array to compare, may be <code>null</code>
241 * @param array2
242 * the right hand array to compare, may be <code>null</code>
243 * @return <code>true</code> if the arrays are equal
244 */
245 public static boolean isEquals( Object array1, Object array2 )
246 {
247 return new EqualsBuilder().append( array1, array2 ).isEquals();
248 }
249
250 // Clone
251 // -----------------------------------------------------------------------
252 /**
253 * <p>
254 * Shallow clones an array returning a typecast result and handling
255 * <code>null</code>.
256 * </p>
257 * <p>
258 * The objects in the array are not cloned, thus there is no special
259 * handling for multi-dimensional arrays.
260 * </p>
261 * <p>
262 * This method returns <code>null</code> if <code>null</code> array
263 * input.
264 * </p>
265 *
266 * @param array
267 * the array to shallow clone, may be <code>null</code>
268 * @return the cloned array, <code>null</code> if <code>null</code>
269 * input
270 */
271 public static Object[] clone( Object[] array )
272 {
273 if ( array == null )
274 {
275 return null;
276 }
277 return array.clone();
278 }
279
280
281 /**
282 * <p>
283 * Clones an array returning a typecast result and handling
284 * <code>null</code>.
285 * </p>
286 * <p>
287 * This method returns <code>null</code> if <code>null</code> array
288 * input.
289 * </p>
290 *
291 * @param array
292 * the array to clone, may be <code>null</code>
293 * @return the cloned array, <code>null</code> if <code>null</code>
294 * input
295 */
296 public static long[] clone( long[] array )
297 {
298 if ( array == null )
299 {
300 return null;
301 }
302 return array.clone();
303 }
304
305
306 /**
307 * <p>
308 * Clones an array returning a typecast result and handling
309 * <code>null</code>.
310 * </p>
311 * <p>
312 * This method returns <code>null</code> if <code>null</code> array
313 * input.
314 * </p>
315 *
316 * @param array
317 * the array to clone, may be <code>null</code>
318 * @return the cloned array, <code>null</code> if <code>null</code>
319 * input
320 */
321 public static int[] clone( int[] array )
322 {
323 if ( array == null )
324 {
325 return null;
326 }
327 return array.clone();
328 }
329
330
331 /**
332 * <p>
333 * Clones an array returning a typecast result and handling
334 * <code>null</code>.
335 * </p>
336 * <p>
337 * This method returns <code>null</code> if <code>null</code> array
338 * input.
339 * </p>
340 *
341 * @param array
342 * the array to clone, may be <code>null</code>
343 * @return the cloned array, <code>null</code> if <code>null</code>
344 * input
345 */
346 public static short[] clone( short[] array )
347 {
348 if ( array == null )
349 {
350 return null;
351 }
352 return array.clone();
353 }
354
355
356 /**
357 * <p>
358 * Clones an array returning a typecast result and handling
359 * <code>null</code>.
360 * </p>
361 * <p>
362 * This method returns <code>null</code> if <code>null</code> array
363 * input.
364 * </p>
365 *
366 * @param array
367 * the array to clone, may be <code>null</code>
368 * @return the cloned array, <code>null</code> if <code>null</code>
369 * input
370 */
371 public static char[] clone( char[] array )
372 {
373 if ( array == null )
374 {
375 return null;
376 }
377 return array.clone();
378 }
379
380
381 /**
382 * <p>
383 * Clones an array returning a typecast result and handling
384 * <code>null</code>.
385 * </p>
386 * <p>
387 * This method returns <code>null</code> if <code>null</code> array
388 * input.
389 * </p>
390 *
391 * @param array
392 * the array to clone, may be <code>null</code>
393 * @return the cloned array, <code>null</code> if <code>null</code>
394 * input
395 */
396 public static byte[] clone( byte[] array )
397 {
398 if ( array == null )
399 {
400 return null;
401 }
402 return array.clone();
403 }
404
405
406 /**
407 * <p>
408 * Clones an array returning a typecast result and handling
409 * <code>null</code>.
410 * </p>
411 * <p>
412 * This method returns <code>null</code> if <code>null</code> array
413 * input.
414 * </p>
415 *
416 * @param array
417 * the array to clone, may be <code>null</code>
418 * @return the cloned array, <code>null</code> if <code>null</code>
419 * input
420 */
421 public static double[] clone( double[] array )
422 {
423 if ( array == null )
424 {
425 return null;
426 }
427 return array.clone();
428 }
429
430
431 /**
432 * <p>
433 * Clones an array returning a typecast result and handling
434 * <code>null</code>.
435 * </p>
436 * <p>
437 * This method returns <code>null</code> if <code>null</code> array
438 * input.
439 * </p>
440 *
441 * @param array
442 * the array to clone, may be <code>null</code>
443 * @return the cloned array, <code>null</code> if <code>null</code>
444 * input
445 */
446 public static float[] clone( float[] array )
447 {
448 if ( array == null )
449 {
450 return null;
451 }
452 return array.clone();
453 }
454
455
456 /**
457 * <p>
458 * Clones an array returning a typecast result and handling
459 * <code>null</code>.
460 * </p>
461 * <p>
462 * This method returns <code>null</code> if <code>null</code> array
463 * input.
464 * </p>
465 *
466 * @param array
467 * the array to clone, may be <code>null</code>
468 * @return the cloned array, <code>null</code> if <code>null</code>
469 * input
470 */
471 public static boolean[] clone( boolean[] array )
472 {
473 if ( array == null )
474 {
475 return null;
476 }
477 return array.clone();
478 }
479
480
481 // Subarrays
482 // -----------------------------------------------------------------------
483 /**
484 * <p>
485 * Produces a new array containing the elements between the start and end
486 * indices.
487 * </p>
488 * <p>
489 * The start index is inclusive, the end index exclusive. Null array input
490 * produces null output.
491 * </p>
492 * <p>
493 * The component type of the subarray is always the same as that of the
494 * input array. Thus, if the input is an array of type <code>Date</code>,
495 * the following usage is envisaged:
496 * </p>
497 *
498 * <pre>
499 * Date[] someDates = ( Date[] ) ArrayUtils.subarray( allDates, 2, 5 );
500 * </pre>
501 *
502 * @param array
503 * the array
504 * @param startIndexInclusive
505 * the starting index. Undervalue (<0) is promoted to 0,
506 * overvalue (>array.length) results in an empty array.
507 * @param endIndexExclusive
508 * elements up to endIndex-1 are present in the returned
509 * subarray. Undervalue (< startIndex) produces empty array,
510 * overvalue (>array.length) is demoted to array length.
511 * @return a new array containing the elements between the start and end
512 * indices.
513 */
514 public static Object[] subarray( Object[] array, int startIndexInclusive, int endIndexExclusive )
515 {
516 if ( array == null )
517 {
518 return null;
519 }
520 if ( startIndexInclusive < 0 )
521 {
522 startIndexInclusive = 0;
523 }
524 if ( endIndexExclusive > array.length )
525 {
526 endIndexExclusive = array.length;
527 }
528 int newSize = endIndexExclusive - startIndexInclusive;
529 Class<?> type = array.getClass().getComponentType();
530 if ( newSize <= 0 )
531 {
532 return ( Object[] ) Array.newInstance( type, 0 );
533 }
534 Object[] subarray = ( Object[] ) Array.newInstance( type, newSize );
535 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
536 return subarray;
537 }
538
539
540 /**
541 * <p>
542 * Produces a new <code>long</code> array containing the elements between
543 * the start and end indices.
544 * </p>
545 * <p>
546 * The start index is inclusive, the end index exclusive. Null array input
547 * produces null output.
548 * </p>
549 *
550 * @param array
551 * the array
552 * @param startIndexInclusive
553 * the starting index. Undervalue (<0) is promoted to 0,
554 * overvalue (>array.length) results in an empty array.
555 * @param endIndexExclusive
556 * elements up to endIndex-1 are present in the returned
557 * subarray. Undervalue (< startIndex) produces empty array,
558 * overvalue (>array.length) is demoted to array length.
559 * @return a new array containing the elements between the start and end
560 * indices.
561 */
562 public static long[] subarray( long[] array, int startIndexInclusive, int endIndexExclusive )
563 {
564 if ( array == null )
565 {
566 return null;
567 }
568 if ( startIndexInclusive < 0 )
569 {
570 startIndexInclusive = 0;
571 }
572 if ( endIndexExclusive > array.length )
573 {
574 endIndexExclusive = array.length;
575 }
576 int newSize = endIndexExclusive - startIndexInclusive;
577 if ( newSize <= 0 )
578 {
579 return EMPTY_LONG_ARRAY;
580 }
581
582 long[] subarray = new long[newSize];
583 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
584 return subarray;
585 }
586
587
588 /**
589 * <p>
590 * Produces a new <code>int</code> array containing the elements between
591 * the start and end indices.
592 * </p>
593 * <p>
594 * The start index is inclusive, the end index exclusive. Null array input
595 * produces null output.
596 * </p>
597 *
598 * @param array
599 * the array
600 * @param startIndexInclusive
601 * the starting index. Undervalue (<0) is promoted to 0,
602 * overvalue (>array.length) results in an empty array.
603 * @param endIndexExclusive
604 * elements up to endIndex-1 are present in the returned
605 * subarray. Undervalue (< startIndex) produces empty array,
606 * overvalue (>array.length) is demoted to array length.
607 * @return a new array containing the elements between the start and end
608 * indices.
609 */
610 public static int[] subarray( int[] array, int startIndexInclusive, int endIndexExclusive )
611 {
612 if ( array == null )
613 {
614 return null;
615 }
616 if ( startIndexInclusive < 0 )
617 {
618 startIndexInclusive = 0;
619 }
620 if ( endIndexExclusive > array.length )
621 {
622 endIndexExclusive = array.length;
623 }
624 int newSize = endIndexExclusive - startIndexInclusive;
625 if ( newSize <= 0 )
626 {
627 return EMPTY_INT_ARRAY;
628 }
629
630 int[] subarray = new int[newSize];
631 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
632 return subarray;
633 }
634
635
636 /**
637 * <p>
638 * Produces a new <code>short</code> array containing the elements between
639 * the start and end indices.
640 * </p>
641 * <p>
642 * The start index is inclusive, the end index exclusive. Null array input
643 * produces null output.
644 * </p>
645 *
646 * @param array
647 * the array
648 * @param startIndexInclusive
649 * the starting index. Undervalue (<0) is promoted to 0,
650 * overvalue (>array.length) results in an empty array.
651 * @param endIndexExclusive
652 * elements up to endIndex-1 are present in the returned
653 * subarray. Undervalue (< startIndex) produces empty array,
654 * overvalue (>array.length) is demoted to array length.
655 * @return a new array containing the elements between the start and end
656 * indices.
657 */
658 public static short[] subarray( short[] array, int startIndexInclusive, int endIndexExclusive )
659 {
660 if ( array == null )
661 {
662 return null;
663 }
664 if ( startIndexInclusive < 0 )
665 {
666 startIndexInclusive = 0;
667 }
668 if ( endIndexExclusive > array.length )
669 {
670 endIndexExclusive = array.length;
671 }
672 int newSize = endIndexExclusive - startIndexInclusive;
673 if ( newSize <= 0 )
674 {
675 return EMPTY_SHORT_ARRAY;
676 }
677
678 short[] subarray = new short[newSize];
679 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
680 return subarray;
681 }
682
683
684 /**
685 * <p>
686 * Produces a new <code>char</code> array containing the elements between
687 * the start and end indices.
688 * </p>
689 * <p>
690 * The start index is inclusive, the end index exclusive. Null array input
691 * produces null output.
692 * </p>
693 *
694 * @param array
695 * the array
696 * @param startIndexInclusive
697 * the starting index. Undervalue (<0) is promoted to 0,
698 * overvalue (>array.length) results in an empty array.
699 * @param endIndexExclusive
700 * elements up to endIndex-1 are present in the returned
701 * subarray. Undervalue (< startIndex) produces empty array,
702 * overvalue (>array.length) is demoted to array length.
703 * @return a new array containing the elements between the start and end
704 * indices.
705 */
706 public static char[] subarray( char[] array, int startIndexInclusive, int endIndexExclusive )
707 {
708 if ( array == null )
709 {
710 return null;
711 }
712 if ( startIndexInclusive < 0 )
713 {
714 startIndexInclusive = 0;
715 }
716 if ( endIndexExclusive > array.length )
717 {
718 endIndexExclusive = array.length;
719 }
720 int newSize = endIndexExclusive - startIndexInclusive;
721 if ( newSize <= 0 )
722 {
723 return EMPTY_CHAR_ARRAY;
724 }
725
726 char[] subarray = new char[newSize];
727 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
728 return subarray;
729 }
730
731
732 /**
733 * <p>
734 * Produces a new <code>byte</code> array containing the elements between
735 * the start and end indices.
736 * </p>
737 * <p>
738 * The start index is inclusive, the end index exclusive. Null array input
739 * produces null output.
740 * </p>
741 *
742 * @param array
743 * the array
744 * @param startIndexInclusive
745 * the starting index. Undervalue (<0) is promoted to 0,
746 * overvalue (>array.length) results in an empty array.
747 * @param endIndexExclusive
748 * elements up to endIndex-1 are present in the returned
749 * subarray. Undervalue (< startIndex) produces empty array,
750 * overvalue (>array.length) is demoted to array length.
751 * @return a new array containing the elements between the start and end
752 * indices.
753 */
754 public static byte[] subarray( byte[] array, int startIndexInclusive, int endIndexExclusive )
755 {
756 if ( array == null )
757 {
758 return null;
759 }
760 if ( startIndexInclusive < 0 )
761 {
762 startIndexInclusive = 0;
763 }
764 if ( endIndexExclusive > array.length )
765 {
766 endIndexExclusive = array.length;
767 }
768 int newSize = endIndexExclusive - startIndexInclusive;
769 if ( newSize <= 0 )
770 {
771 return EMPTY_BYTE_ARRAY;
772 }
773
774 byte[] subarray = new byte[newSize];
775 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
776 return subarray;
777 }
778
779
780 /**
781 * <p>
782 * Produces a new <code>double</code> array containing the elements
783 * between the start and end indices.
784 * </p>
785 * <p>
786 * The start index is inclusive, the end index exclusive. Null array input
787 * produces null output.
788 * </p>
789 *
790 * @param array
791 * the array
792 * @param startIndexInclusive
793 * the starting index. Undervalue (<0) is promoted to 0,
794 * overvalue (>array.length) results in an empty array.
795 * @param endIndexExclusive
796 * elements up to endIndex-1 are present in the returned
797 * subarray. Undervalue (< startIndex) produces empty array,
798 * overvalue (>array.length) is demoted to array length.
799 * @return a new array containing the elements between the start and end
800 * indices.
801 */
802 public static double[] subarray( double[] array, int startIndexInclusive, int endIndexExclusive )
803 {
804 if ( array == null )
805 {
806 return null;
807 }
808 if ( startIndexInclusive < 0 )
809 {
810 startIndexInclusive = 0;
811 }
812 if ( endIndexExclusive > array.length )
813 {
814 endIndexExclusive = array.length;
815 }
816 int newSize = endIndexExclusive - startIndexInclusive;
817 if ( newSize <= 0 )
818 {
819 return EMPTY_DOUBLE_ARRAY;
820 }
821
822 double[] subarray = new double[newSize];
823 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
824 return subarray;
825 }
826
827
828 /**
829 * <p>
830 * Produces a new <code>float</code> array containing the elements between
831 * the start and end indices.
832 * </p>
833 * <p>
834 * The start index is inclusive, the end index exclusive. Null array input
835 * produces null output.
836 * </p>
837 *
838 * @param array
839 * the array
840 * @param startIndexInclusive
841 * the starting index. Undervalue (<0) is promoted to 0,
842 * overvalue (>array.length) results in an empty array.
843 * @param endIndexExclusive
844 * elements up to endIndex-1 are present in the returned
845 * subarray. Undervalue (< startIndex) produces empty array,
846 * overvalue (>array.length) is demoted to array length.
847 * @return a new array containing the elements between the start and end
848 * indices.
849 */
850 public static float[] subarray( float[] array, int startIndexInclusive, int endIndexExclusive )
851 {
852 if ( array == null )
853 {
854 return null;
855 }
856 if ( startIndexInclusive < 0 )
857 {
858 startIndexInclusive = 0;
859 }
860 if ( endIndexExclusive > array.length )
861 {
862 endIndexExclusive = array.length;
863 }
864 int newSize = endIndexExclusive - startIndexInclusive;
865 if ( newSize <= 0 )
866 {
867 return EMPTY_FLOAT_ARRAY;
868 }
869
870 float[] subarray = new float[newSize];
871 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
872 return subarray;
873 }
874
875
876 /**
877 * <p>
878 * Produces a new <code>boolean</code> array containing the elements
879 * between the start and end indices.
880 * </p>
881 * <p>
882 * The start index is inclusive, the end index exclusive. Null array input
883 * produces null output.
884 * </p>
885 *
886 * @param array
887 * the array
888 * @param startIndexInclusive
889 * the starting index. Undervalue (<0) is promoted to 0,
890 * overvalue (>array.length) results in an empty array.
891 * @param endIndexExclusive
892 * elements up to endIndex-1 are present in the returned
893 * subarray. Undervalue (< startIndex) produces empty array,
894 * overvalue (>array.length) is demoted to array length.
895 * @return a new array containing the elements between the start and end
896 * indices.
897 */
898 public static boolean[] subarray( boolean[] array, int startIndexInclusive, int endIndexExclusive )
899 {
900 if ( array == null )
901 {
902 return null;
903 }
904 if ( startIndexInclusive < 0 )
905 {
906 startIndexInclusive = 0;
907 }
908 if ( endIndexExclusive > array.length )
909 {
910 endIndexExclusive = array.length;
911 }
912 int newSize = endIndexExclusive - startIndexInclusive;
913 if ( newSize <= 0 )
914 {
915 return EMPTY_BOOLEAN_ARRAY;
916 }
917
918 boolean[] subarray = new boolean[newSize];
919 System.arraycopy( array, startIndexInclusive, subarray, 0, newSize );
920 return subarray;
921 }
922
923
924 // Is same length
925 // -----------------------------------------------------------------------
926 /**
927 * <p>
928 * Checks whether two arrays are the same length, treating <code>null</code>
929 * arrays as length <code>0</code>.
930 * <p>
931 * Any multi-dimensional aspects of the arrays are ignored.
932 * </p>
933 *
934 * @param array1
935 * the first array, may be <code>null</code>
936 * @param array2
937 * the second array, may be <code>null</code>
938 * @return <code>true</code> if length of arrays matches, treating
939 * <code>null</code> as an empty array
940 */
941 public static boolean isSameLength( Object[] array1, Object[] array2 )
942 {
943 if ( ( array1 == null && array2 != null && array2.length > 0 )
944 || ( array2 == null && array1 != null && array1.length > 0 )
945 || ( array1 != null && array2 != null && array1.length != array2.length ) )
946 {
947 return false;
948 }
949 return true;
950 }
951
952
953 /**
954 * <p>
955 * Checks whether two arrays are the same length, treating <code>null</code>
956 * arrays as length <code>0</code>.
957 * </p>
958 *
959 * @param array1
960 * the first array, may be <code>null</code>
961 * @param array2
962 * the second array, may be <code>null</code>
963 * @return <code>true</code> if length of arrays matches, treating
964 * <code>null</code> as an empty array
965 */
966 public static boolean isSameLength( long[] array1, long[] array2 )
967 {
968 if ( ( array1 == null && array2 != null && array2.length > 0 )
969 || ( array2 == null && array1 != null && array1.length > 0 )
970 || ( array1 != null && array2 != null && array1.length != array2.length ) )
971 {
972 return false;
973 }
974 return true;
975 }
976
977
978 /**
979 * <p>
980 * Checks whether two arrays are the same length, treating <code>null</code>
981 * arrays as length <code>0</code>.
982 * </p>
983 *
984 * @param array1
985 * the first array, may be <code>null</code>
986 * @param array2
987 * the second array, may be <code>null</code>
988 * @return <code>true</code> if length of arrays matches, treating
989 * <code>null</code> as an empty array
990 */
991 public static boolean isSameLength( int[] array1, int[] array2 )
992 {
993 if ( ( array1 == null && array2 != null && array2.length > 0 )
994 || ( array2 == null && array1 != null && array1.length > 0 )
995 || ( array1 != null && array2 != null && array1.length != array2.length ) )
996 {
997 return false;
998 }
999 return true;
1000 }
1001
1002
1003 /**
1004 * <p>
1005 * Checks whether two arrays are the same length, treating <code>null</code>
1006 * arrays as length <code>0</code>.
1007 * </p>
1008 *
1009 * @param array1
1010 * the first array, may be <code>null</code>
1011 * @param array2
1012 * the second array, may be <code>null</code>
1013 * @return <code>true</code> if length of arrays matches, treating
1014 * <code>null</code> as an empty array
1015 */
1016 public static boolean isSameLength( short[] array1, short[] array2 )
1017 {
1018 if ( ( array1 == null && array2 != null && array2.length > 0 )
1019 || ( array2 == null && array1 != null && array1.length > 0 )
1020 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1021 {
1022 return false;
1023 }
1024 return true;
1025 }
1026
1027
1028 /**
1029 * <p>
1030 * Checks whether two arrays are the same length, treating <code>null</code>
1031 * arrays as length <code>0</code>.
1032 * </p>
1033 *
1034 * @param array1
1035 * the first array, may be <code>null</code>
1036 * @param array2
1037 * the second array, may be <code>null</code>
1038 * @return <code>true</code> if length of arrays matches, treating
1039 * <code>null</code> as an empty array
1040 */
1041 public static boolean isSameLength( char[] array1, char[] array2 )
1042 {
1043 if ( ( array1 == null && array2 != null && array2.length > 0 )
1044 || ( array2 == null && array1 != null && array1.length > 0 )
1045 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1046 {
1047 return false;
1048 }
1049 return true;
1050 }
1051
1052
1053 /**
1054 * <p>
1055 * Checks whether two arrays are the same length, treating <code>null</code>
1056 * arrays as length <code>0</code>.
1057 * </p>
1058 *
1059 * @param array1
1060 * the first array, may be <code>null</code>
1061 * @param array2
1062 * the second array, may be <code>null</code>
1063 * @return <code>true</code> if length of arrays matches, treating
1064 * <code>null</code> as an empty array
1065 */
1066 public static boolean isSameLength( byte[] array1, byte[] array2 )
1067 {
1068 if ( ( array1 == null && array2 != null && array2.length > 0 )
1069 || ( array2 == null && array1 != null && array1.length > 0 )
1070 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1071 {
1072 return false;
1073 }
1074 return true;
1075 }
1076
1077
1078 /**
1079 * <p>
1080 * Checks whether two arrays are the same length, treating <code>null</code>
1081 * arrays as length <code>0</code>.
1082 * </p>
1083 *
1084 * @param array1
1085 * the first array, may be <code>null</code>
1086 * @param array2
1087 * the second array, may be <code>null</code>
1088 * @return <code>true</code> if length of arrays matches, treating
1089 * <code>null</code> as an empty array
1090 */
1091 public static boolean isSameLength( double[] array1, double[] array2 )
1092 {
1093 if ( ( array1 == null && array2 != null && array2.length > 0 )
1094 || ( array2 == null && array1 != null && array1.length > 0 )
1095 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1096 {
1097 return false;
1098 }
1099 return true;
1100 }
1101
1102
1103 /**
1104 * <p>
1105 * Checks whether two arrays are the same length, treating <code>null</code>
1106 * arrays as length <code>0</code>.
1107 * </p>
1108 *
1109 * @param array1
1110 * the first array, may be <code>null</code>
1111 * @param array2
1112 * the second array, may be <code>null</code>
1113 * @return <code>true</code> if length of arrays matches, treating
1114 * <code>null</code> as an empty array
1115 */
1116 public static boolean isSameLength( float[] array1, float[] array2 )
1117 {
1118 if ( ( array1 == null && array2 != null && array2.length > 0 )
1119 || ( array2 == null && array1 != null && array1.length > 0 )
1120 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1121 {
1122 return false;
1123 }
1124 return true;
1125 }
1126
1127
1128 /**
1129 * <p>
1130 * Checks whether two arrays are the same length, treating <code>null</code>
1131 * arrays as length <code>0</code>.
1132 * </p>
1133 *
1134 * @param array1
1135 * the first array, may be <code>null</code>
1136 * @param array2
1137 * the second array, may be <code>null</code>
1138 * @return <code>true</code> if length of arrays matches, treating
1139 * <code>null</code> as an empty array
1140 */
1141 public static boolean isSameLength( boolean[] array1, boolean[] array2 )
1142 {
1143 if ( ( array1 == null && array2 != null && array2.length > 0 )
1144 || ( array2 == null && array1 != null && array1.length > 0 )
1145 || ( array1 != null && array2 != null && array1.length != array2.length ) )
1146 {
1147 return false;
1148 }
1149 return true;
1150 }
1151
1152
1153 // -----------------------------------------------------------------------
1154 /**
1155 * <p>
1156 * Returns the length of the specified array. This method can deal with
1157 * <code>Object</code> arrays and with primitive arrays.
1158 * </p>
1159 * <p>
1160 * If the input array is <code>null</code>, <code>0</code> is returned.
1161 * </p>
1162 *
1163 * <pre>
1164 * ArrayUtils.getLength(null) = 0
1165 * ArrayUtils.getLength([]) = 0
1166 * ArrayUtils.getLength([null]) = 1
1167 * ArrayUtils.getLength([true, false]) = 2
1168 * ArrayUtils.getLength([1, 2, 3]) = 3
1169 * ArrayUtils.getLength(["a", "b", "c"]) = 3
1170 * </pre>
1171 *
1172 * @param array
1173 * the array to retrieve the length from, may be null
1174 * @return The length of the array, or <code>0</code> if the array is
1175 * <code>null</code>
1176 * @throws IllegalArgumentException
1177 * if the object arguement is not an array.
1178 */
1179 public static int getLength( Object array )
1180 {
1181 if ( array == null )
1182 {
1183 return 0;
1184 }
1185 else
1186 {
1187 return Array.getLength( array );
1188 }
1189 }
1190
1191
1192 /**
1193 * Returns the last index of the given array or -1 if empty or null. This
1194 * method can deal with <code>Object</code> arrays and with primitive
1195 * arrays. This value is one less than the size since arrays indices are
1196 * 0-based.
1197 * </p>
1198 *
1199 * <pre>
1200 * ArrayUtils.lastIndex(null) = -1
1201 * ArrayUtils.lastIndex([]) = -1
1202 * ArrayUtils.lastIndex([null]) = 0
1203 * ArrayUtils.lastIndex([true, false]) = 1
1204 * ArrayUtils.lastIndex([1, 2, 3]) = 2
1205 * ArrayUtils.lastIndex(["a", "b", "c"]) = 2
1206 * </pre>
1207 *
1208 * @param array
1209 * the array to return the last index for, may be null
1210 * @return the last index, -1 if empty or null
1211 * @throws IllegalArgumentException
1212 * if the object arguement is not an array.
1213 */
1214 public static int lastIndex( Object array )
1215 {
1216 return ArrayUtils.getLength( array ) - 1;
1217 }
1218
1219
1220 /**
1221 * <p>
1222 * Checks whether two arrays are the same type taking into account
1223 * multi-dimensional arrays.
1224 * </p>
1225 *
1226 * @param array1
1227 * the first array, must not be <code>null</code>
1228 * @param array2
1229 * the second array, must not be <code>null</code>
1230 * @return <code>true</code> if type of arrays matches
1231 * @throws IllegalArgumentException
1232 * if either array is <code>null</code>
1233 */
1234 public static boolean isSameType( Object array1, Object array2 )
1235 {
1236 if ( array1 == null || array2 == null )
1237 {
1238 throw new IllegalArgumentException( "The Array must not be null" );
1239 }
1240 return array1.getClass().getName().equals( array2.getClass().getName() );
1241 }
1242
1243
1244 // Reverse
1245 // -----------------------------------------------------------------------
1246 /**
1247 * <p>
1248 * Reverses the order of the given array.
1249 * </p>
1250 * <p>
1251 * There is no special handling for multi-dimensional arrays.
1252 * </p>
1253 * <p>
1254 * This method does nothing if <code>null</code> array input.
1255 * </p>
1256 *
1257 * @param array
1258 * the array to reverse, may be <code>null</code>
1259 */
1260 public static void reverse( Object[] array )
1261 {
1262 if ( array == null )
1263 {
1264 return;
1265 }
1266 int i = 0;
1267 int j = array.length - 1;
1268 Object tmp;
1269 while ( j > i )
1270 {
1271 tmp = array[j];
1272 array[j] = array[i];
1273 array[i] = tmp;
1274 j--;
1275 i++;
1276 }
1277 }
1278
1279
1280 /**
1281 * <p>
1282 * Reverses the order of the given array.
1283 * </p>
1284 * <p>
1285 * This method does nothing if <code>null</code> array input.
1286 * </p>
1287 *
1288 * @param array
1289 * the array to reverse, may be <code>null</code>
1290 */
1291 public static void reverse( long[] array )
1292 {
1293 if ( array == null )
1294 {
1295 return;
1296 }
1297 int i = 0;
1298 int j = array.length - 1;
1299 long tmp;
1300 while ( j > i )
1301 {
1302 tmp = array[j];
1303 array[j] = array[i];
1304 array[i] = tmp;
1305 j--;
1306 i++;
1307 }
1308 }
1309
1310
1311 /**
1312 * <p>
1313 * Reverses the order of the given array.
1314 * </p>
1315 * <p>
1316 * This method does nothing if <code>null</code> array input.
1317 * </p>
1318 *
1319 * @param array
1320 * the array to reverse, may be <code>null</code>
1321 */
1322 public static void reverse( int[] array )
1323 {
1324 if ( array == null )
1325 {
1326 return;
1327 }
1328 int i = 0;
1329 int j = array.length - 1;
1330 int tmp;
1331 while ( j > i )
1332 {
1333 tmp = array[j];
1334 array[j] = array[i];
1335 array[i] = tmp;
1336 j--;
1337 i++;
1338 }
1339 }
1340
1341
1342 /**
1343 * <p>
1344 * Reverses the order of the given array.
1345 * </p>
1346 * <p>
1347 * This method does nothing if <code>null</code> array input.
1348 * </p>
1349 *
1350 * @param array
1351 * the array to reverse, may be <code>null</code>
1352 */
1353 public static void reverse( short[] array )
1354 {
1355 if ( array == null )
1356 {
1357 return;
1358 }
1359 int i = 0;
1360 int j = array.length - 1;
1361 short tmp;
1362 while ( j > i )
1363 {
1364 tmp = array[j];
1365 array[j] = array[i];
1366 array[i] = tmp;
1367 j--;
1368 i++;
1369 }
1370 }
1371
1372
1373 /**
1374 * <p>
1375 * Reverses the order of the given array.
1376 * </p>
1377 * <p>
1378 * This method does nothing if <code>null</code> array input.
1379 * </p>
1380 *
1381 * @param array
1382 * the array to reverse, may be <code>null</code>
1383 */
1384 public static void reverse( char[] array )
1385 {
1386 if ( array == null )
1387 {
1388 return;
1389 }
1390 int i = 0;
1391 int j = array.length - 1;
1392 char tmp;
1393 while ( j > i )
1394 {
1395 tmp = array[j];
1396 array[j] = array[i];
1397 array[i] = tmp;
1398 j--;
1399 i++;
1400 }
1401 }
1402
1403
1404 /**
1405 * <p>
1406 * Reverses the order of the given array.
1407 * </p>
1408 * <p>
1409 * This method does nothing if <code>null</code> array input.
1410 * </p>
1411 *
1412 * @param array
1413 * the array to reverse, may be <code>null</code>
1414 */
1415 public static void reverse( byte[] array )
1416 {
1417 if ( array == null )
1418 {
1419 return;
1420 }
1421 int i = 0;
1422 int j = array.length - 1;
1423 byte tmp;
1424 while ( j > i )
1425 {
1426 tmp = array[j];
1427 array[j] = array[i];
1428 array[i] = tmp;
1429 j--;
1430 i++;
1431 }
1432 }
1433
1434
1435 /**
1436 * <p>
1437 * Reverses the order of the given array.
1438 * </p>
1439 * <p>
1440 * This method does nothing if <code>null</code> array input.
1441 * </p>
1442 *
1443 * @param array
1444 * the array to reverse, may be <code>null</code>
1445 */
1446 public static void reverse( double[] array )
1447 {
1448 if ( array == null )
1449 {
1450 return;
1451 }
1452 int i = 0;
1453 int j = array.length - 1;
1454 double tmp;
1455 while ( j > i )
1456 {
1457 tmp = array[j];
1458 array[j] = array[i];
1459 array[i] = tmp;
1460 j--;
1461 i++;
1462 }
1463 }
1464
1465
1466 /**
1467 * <p>
1468 * Reverses the order of the given array.
1469 * </p>
1470 * <p>
1471 * This method does nothing if <code>null</code> array input.
1472 * </p>
1473 *
1474 * @param array
1475 * the array to reverse, may be <code>null</code>
1476 */
1477 public static void reverse( float[] array )
1478 {
1479 if ( array == null )
1480 {
1481 return;
1482 }
1483 int i = 0;
1484 int j = array.length - 1;
1485 float tmp;
1486 while ( j > i )
1487 {
1488 tmp = array[j];
1489 array[j] = array[i];
1490 array[i] = tmp;
1491 j--;
1492 i++;
1493 }
1494 }
1495
1496
1497 /**
1498 * <p>
1499 * Reverses the order of the given array.
1500 * </p>
1501 * <p>
1502 * This method does nothing if <code>null</code> array input.
1503 * </p>
1504 *
1505 * @param array
1506 * the array to reverse, may be <code>null</code>
1507 */
1508 public static void reverse( boolean[] array )
1509 {
1510 if ( array == null )
1511 {
1512 return;
1513 }
1514 int i = 0;
1515 int j = array.length - 1;
1516 boolean tmp;
1517 while ( j > i )
1518 {
1519 tmp = array[j];
1520 array[j] = array[i];
1521 array[i] = tmp;
1522 j--;
1523 i++;
1524 }
1525 }
1526
1527
1528 // IndexOf search
1529 // ----------------------------------------------------------------------
1530
1531 // Object IndexOf
1532 // -----------------------------------------------------------------------
1533 /**
1534 * <p>
1535 * Find the index of the given object in the array.
1536 * </p>
1537 * <p>
1538 * This method returns <code>-1</code> if <code>null</code> array input.
1539 * </p>
1540 *
1541 * @param array
1542 * the array to search through for the object, may be
1543 * <code>null</code>
1544 * @param objectToFind
1545 * the object to find, may be <code>null</code>
1546 * @return the index of the object within the array, <code>-1</code> if
1547 * not found or <code>null</code> array input
1548 */
1549 public static int indexOf( Object[] array, Object objectToFind )
1550 {
1551 return indexOf( array, objectToFind, 0 );
1552 }
1553
1554
1555 /**
1556 * <p>
1557 * Find the index of the given object in the array starting at the given
1558 * index.
1559 * </p>
1560 * <p>
1561 * This method returns <code>-1</code> if <code>null</code> array input.
1562 * </p>
1563 * <p>
1564 * A negative startIndex is treated as zero. A startIndex larger than the
1565 * array length will return <code>-1</code>.
1566 * </p>
1567 *
1568 * @param array
1569 * the array to search through for the object, may be
1570 * <code>null</code>
1571 * @param objectToFind
1572 * the object to find, may be <code>null</code>
1573 * @param startIndex
1574 * the index to start searching at
1575 * @return the index of the object within the array starting at the index,
1576 * <code>-1</code> if not found or <code>null</code> array input
1577 */
1578 public static int indexOf( Object[] array, Object objectToFind, int startIndex )
1579 {
1580 if ( array == null )
1581 {
1582 return -1;
1583 }
1584 if ( startIndex < 0 )
1585 {
1586 startIndex = 0;
1587 }
1588 if ( objectToFind == null )
1589 {
1590 for ( int i = startIndex; i < array.length; i++ )
1591 {
1592 if ( array[i] == null )
1593 {
1594 return i;
1595 }
1596 }
1597 }
1598 else
1599 {
1600 for ( int i = startIndex; i < array.length; i++ )
1601 {
1602 if ( objectToFind.equals( array[i] ) )
1603 {
1604 return i;
1605 }
1606 }
1607 }
1608 return -1;
1609 }
1610
1611
1612 /**
1613 * <p>
1614 * Find the last index of the given object within the array.
1615 * </p>
1616 * <p>
1617 * This method returns <code>-1</code> if <code>null</code> array input.
1618 * </p>
1619 *
1620 * @param array
1621 * the array to travers backwords looking for the object, may be
1622 * <code>null</code>
1623 * @param objectToFind
1624 * the object to find, may be <code>null</code>
1625 * @return the last index of the object within the array, <code>-1</code>
1626 * if not found or <code>null</code> array input
1627 */
1628 public static int lastIndexOf( Object[] array, Object objectToFind )
1629 {
1630 return lastIndexOf( array, objectToFind, Integer.MAX_VALUE );
1631 }
1632
1633
1634 /**
1635 * <p>
1636 * Find the last index of the given object in the array starting at the
1637 * given index.
1638 * </p>
1639 * <p>
1640 * This method returns <code>-1</code> if <code>null</code> array input.
1641 * </p>
1642 * <p>
1643 * A negative startIndex will return <code>-1</code>. A startIndex larger
1644 * than the array length will search from the end of the array.
1645 * </p>
1646 *
1647 * @param array
1648 * the array to traverse for looking for the object, may be
1649 * <code>null</code>
1650 * @param objectToFind
1651 * the object to find, may be <code>null</code>
1652 * @param startIndex
1653 * the start index to travers backwards from
1654 * @return the last index of the object within the array, <code>-1</code>
1655 * if not found or <code>null</code> array input
1656 */
1657 public static int lastIndexOf( Object[] array, Object objectToFind, int startIndex )
1658 {
1659 if ( array == null )
1660 {
1661 return -1;
1662 }
1663 if ( startIndex < 0 )
1664 {
1665 return -1;
1666 }
1667 else if ( startIndex >= array.length )
1668 {
1669 startIndex = array.length - 1;
1670 }
1671 if ( objectToFind == null )
1672 {
1673 for ( int i = startIndex; i >= 0; i-- )
1674 {
1675 if ( array[i] == null )
1676 {
1677 return i;
1678 }
1679 }
1680 }
1681 else
1682 {
1683 for ( int i = startIndex; i >= 0; i-- )
1684 {
1685 if ( objectToFind.equals( array[i] ) )
1686 {
1687 return i;
1688 }
1689 }
1690 }
1691 return -1;
1692 }
1693
1694
1695 /**
1696 * <p>
1697 * Checks if the object is in the given array.
1698 * </p>
1699 * <p>
1700 * The method returns <code>false</code> if a <code>null</code> array is
1701 * passed in.
1702 * </p>
1703 *
1704 * @param array
1705 * the array to search through
1706 * @param objectToFind
1707 * the object to find
1708 * @return <code>true</code> if the array contains the object
1709 */
1710 public static boolean contains( Object[] array, Object objectToFind )
1711 {
1712 return ( indexOf( array, objectToFind ) != -1 );
1713 }
1714
1715
1716 // long IndexOf
1717 // -----------------------------------------------------------------------
1718 /**
1719 * <p>
1720 * Find the index of the given value in the array.
1721 * </p>
1722 * <p>
1723 * This method returns <code>-1</code> if <code>null</code> array input.
1724 * </p>
1725 *
1726 * @param array
1727 * the array to search through for the object, may be
1728 * <code>null</code>
1729 * @param valueToFind
1730 * the value to find
1731 * @return the index of the value within the array, <code>-1</code> if not
1732 * found or <code>null</code> array input
1733 */
1734 public static int indexOf( long[] array, long valueToFind )
1735 {
1736 return indexOf( array, valueToFind, 0 );
1737 }
1738
1739
1740 /**
1741 * <p>
1742 * Find the index of the given value in the array starting at the given
1743 * index.
1744 * </p>
1745 * <p>
1746 * This method returns <code>-1</code> if <code>null</code> array input.
1747 * </p>
1748 * <p>
1749 * A negative startIndex is treated as zero. A startIndex larger than the
1750 * array length will return -1.
1751 * </p>
1752 *
1753 * @param array
1754 * the array to search through for the object, may be
1755 * <code>null</code>
1756 * @param valueToFind
1757 * the value to find
1758 * @param startIndex
1759 * the index to start searching at
1760 * @return the index of the value within the array, <code>-1</code> if not
1761 * found or <code>null</code> array input
1762 */
1763 public static int indexOf( long[] array, long valueToFind, int startIndex )
1764 {
1765 if ( array == null )
1766 {
1767 return -1;
1768 }
1769 if ( startIndex < 0 )
1770 {
1771 startIndex = 0;
1772 }
1773 for ( int i = startIndex; i < array.length; i++ )
1774 {
1775 if ( valueToFind == array[i] )
1776 {
1777 return i;
1778 }
1779 }
1780 return -1;
1781 }
1782
1783
1784 /**
1785 * <p>
1786 * Find the last index of the given value within the array.
1787 * </p>
1788 * <p>
1789 * This method returns <code>-1</code> if <code>null</code> array input.
1790 * </p>
1791 *
1792 * @param array
1793 * the array to travers backwords looking for the object, may be
1794 * <code>null</code>
1795 * @param valueToFind
1796 * the object to find
1797 * @return the last index of the value within the array, <code>-1</code>
1798 * if not found or <code>null</code> array input
1799 */
1800 public static int lastIndexOf( long[] array, long valueToFind )
1801 {
1802 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
1803 }
1804
1805
1806 /**
1807 * <p>
1808 * Find the last index of the given value in the array starting at the given
1809 * index.
1810 * </p>
1811 * <p>
1812 * This method returns <code>-1</code> if <code>null</code> array input.
1813 * </p>
1814 * <p>
1815 * A negative startIndex will return -1. A startIndex larger than the array
1816 * length will search from the end of the array.
1817 * </p>
1818 *
1819 * @param array
1820 * the array to traverse for looking for the object, may be
1821 * <code>null</code>
1822 * @param valueToFind
1823 * the value to find
1824 * @param startIndex
1825 * the start index to travers backwards from
1826 * @return the last index of the value within the array, <code>-1</code>
1827 * if not found or <code>null</code> array input
1828 */
1829 public static int lastIndexOf( long[] array, long valueToFind, int startIndex )
1830 {
1831 if ( array == null )
1832 {
1833 return -1;
1834 }
1835 if ( startIndex < 0 )
1836 {
1837 return -1;
1838 }
1839 else if ( startIndex >= array.length )
1840 {
1841 startIndex = array.length - 1;
1842 }
1843 for ( int i = startIndex; i >= 0; i-- )
1844 {
1845 if ( valueToFind == array[i] )
1846 {
1847 return i;
1848 }
1849 }
1850 return -1;
1851 }
1852
1853
1854 /**
1855 * <p>
1856 * Checks if the value is in the given array.
1857 * </p>
1858 * <p>
1859 * The method returns <code>false</code> if a <code>null</code> array is
1860 * passed in.
1861 * </p>
1862 *
1863 * @param array
1864 * the array to search through
1865 * @param valueToFind
1866 * the value to find
1867 * @return <code>true</code> if the array contains the object
1868 */
1869 public static boolean contains( long[] array, long valueToFind )
1870 {
1871 return ( indexOf( array, valueToFind ) != -1 );
1872 }
1873
1874
1875 // int IndexOf
1876 // -----------------------------------------------------------------------
1877 /**
1878 * <p>
1879 * Find the index of the given value in the array.
1880 * </p>
1881 * <p>
1882 * This method returns <code>-1</code> if <code>null</code> array input.
1883 * </p>
1884 *
1885 * @param array
1886 * the array to search through for the object, may be
1887 * <code>null</code>
1888 * @param valueToFind
1889 * the value to find
1890 * @return the index of the value within the array, <code>-1</code> if not
1891 * found or <code>null</code> array input
1892 */
1893 public static int indexOf( int[] array, int valueToFind )
1894 {
1895 return indexOf( array, valueToFind, 0 );
1896 }
1897
1898
1899 /**
1900 * <p>
1901 * Find the index of the given value in the array starting at the given
1902 * index.
1903 * </p>
1904 * <p>
1905 * This method returns <code>-1</code> if <code>null</code> array input.
1906 * </p>
1907 * <p>
1908 * A negative startIndex is treated as zero. A startIndex larger than the
1909 * array length will return -1.
1910 * </p>
1911 *
1912 * @param array
1913 * the array to search through for the object, may be
1914 * <code>null</code>
1915 * @param valueToFind
1916 * the value to find
1917 * @param startIndex
1918 * the index to start searching at
1919 * @return the index of the value within the array, <code>-1</code> if not
1920 * found or <code>null</code> array input
1921 */
1922 public static int indexOf( int[] array, int valueToFind, int startIndex )
1923 {
1924 if ( array == null )
1925 {
1926 return -1;
1927 }
1928 if ( startIndex < 0 )
1929 {
1930 startIndex = 0;
1931 }
1932 for ( int i = startIndex; i < array.length; i++ )
1933 {
1934 if ( valueToFind == array[i] )
1935 {
1936 return i;
1937 }
1938 }
1939 return -1;
1940 }
1941
1942
1943 /**
1944 * <p>
1945 * Find the last index of the given value within the array.
1946 * </p>
1947 * <p>
1948 * This method returns <code>-1</code> if <code>null</code> array input.
1949 * </p>
1950 *
1951 * @param array
1952 * the array to travers backwords looking for the object, may be
1953 * <code>null</code>
1954 * @param valueToFind
1955 * the object to find
1956 * @return the last index of the value within the array, <code>-1</code>
1957 * if not found or <code>null</code> array input
1958 */
1959 public static int lastIndexOf( int[] array, int valueToFind )
1960 {
1961 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
1962 }
1963
1964
1965 /**
1966 * <p>
1967 * Find the last index of the given value in the array starting at the given
1968 * index.
1969 * </p>
1970 * <p>
1971 * This method returns <code>-1</code> if <code>null</code> array input.
1972 * </p>
1973 * <p>
1974 * A negative startIndex will return -1. A startIndex larger than the array
1975 * length will search from the end of the array.
1976 * </p>
1977 *
1978 * @param array
1979 * the array to traverse for looking for the object, may be
1980 * <code>null</code>
1981 * @param valueToFind
1982 * the value to find
1983 * @param startIndex
1984 * the start index to travers backwards from
1985 * @return the last index of the value within the array, <code>-1</code>
1986 * if not found or <code>null</code> array input
1987 */
1988 public static int lastIndexOf( int[] array, int valueToFind, int startIndex )
1989 {
1990 if ( array == null )
1991 {
1992 return -1;
1993 }
1994 if ( startIndex < 0 )
1995 {
1996 return -1;
1997 }
1998 else if ( startIndex >= array.length )
1999 {
2000 startIndex = array.length - 1;
2001 }
2002 for ( int i = startIndex; i >= 0; i-- )
2003 {
2004 if ( valueToFind == array[i] )
2005 {
2006 return i;
2007 }
2008 }
2009 return -1;
2010 }
2011
2012
2013 /**
2014 * <p>
2015 * Checks if the value is in the given array.
2016 * </p>
2017 * <p>
2018 * The method returns <code>false</code> if a <code>null</code> array is
2019 * passed in.
2020 * </p>
2021 *
2022 * @param array
2023 * the array to search through
2024 * @param valueToFind
2025 * the value to find
2026 * @return <code>true</code> if the array contains the object
2027 */
2028 public static boolean contains( int[] array, int valueToFind )
2029 {
2030 return ( indexOf( array, valueToFind ) != -1 );
2031 }
2032
2033
2034 // short IndexOf
2035 // -----------------------------------------------------------------------
2036 /**
2037 * <p>
2038 * Find the index of the given value in the array.
2039 * </p>
2040 * <p>
2041 * This method returns <code>-1</code> if <code>null</code> array input.
2042 * </p>
2043 *
2044 * @param array
2045 * the array to search through for the object, may be
2046 * <code>null</code>
2047 * @param valueToFind
2048 * the value to find
2049 * @return the index of the value within the array, <code>-1</code> if not
2050 * found or <code>null</code> array input
2051 */
2052 public static int indexOf( short[] array, short valueToFind )
2053 {
2054 return indexOf( array, valueToFind, 0 );
2055 }
2056
2057
2058 /**
2059 * <p>
2060 * Find the index of the given value in the array starting at the given
2061 * index.
2062 * </p>
2063 * <p>
2064 * This method returns <code>-1</code> if <code>null</code> array input.
2065 * </p>
2066 * <p>
2067 * A negative startIndex is treated as zero. A startIndex larger than the
2068 * array length will return -1.
2069 * </p>
2070 *
2071 * @param array
2072 * the array to search through for the object, may be
2073 * <code>null</code>
2074 * @param valueToFind
2075 * the value to find
2076 * @param startIndex
2077 * the index to start searching at
2078 * @return the index of the value within the array, <code>-1</code> if not
2079 * found or <code>null</code> array input
2080 */
2081 public static int indexOf( short[] array, short valueToFind, int startIndex )
2082 {
2083 if ( array == null )
2084 {
2085 return -1;
2086 }
2087 if ( startIndex < 0 )
2088 {
2089 startIndex = 0;
2090 }
2091 for ( int i = startIndex; i < array.length; i++ )
2092 {
2093 if ( valueToFind == array[i] )
2094 {
2095 return i;
2096 }
2097 }
2098 return -1;
2099 }
2100
2101
2102 /**
2103 * <p>
2104 * Find the last index of the given value within the array.
2105 * </p>
2106 * <p>
2107 * This method returns <code>-1</code> if <code>null</code> array input.
2108 * </p>
2109 *
2110 * @param array
2111 * the array to travers backwords looking for the object, may be
2112 * <code>null</code>
2113 * @param valueToFind
2114 * the object to find
2115 * @return the last index of the value within the array, <code>-1</code>
2116 * if not found or <code>null</code> array input
2117 */
2118 public static int lastIndexOf( short[] array, short valueToFind )
2119 {
2120 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
2121 }
2122
2123
2124 /**
2125 * <p>
2126 * Find the last index of the given value in the array starting at the given
2127 * index.
2128 * </p>
2129 * <p>
2130 * This method returns <code>-1</code> if <code>null</code> array input.
2131 * </p>
2132 * <p>
2133 * A negative startIndex will return -1. A startIndex larger than the array
2134 * length will search from the end of the array.
2135 * </p>
2136 *
2137 * @param array
2138 * the array to traverse for looking for the object, may be
2139 * <code>null</code>
2140 * @param valueToFind
2141 * the value to find
2142 * @param startIndex
2143 * the start index to travers backwards from
2144 * @return the last index of the value within the array, <code>-1</code>
2145 * if not found or <code>null</code> array input
2146 */
2147 public static int lastIndexOf( short[] array, short valueToFind, int startIndex )
2148 {
2149 if ( array == null )
2150 {
2151 return -1;
2152 }
2153 if ( startIndex < 0 )
2154 {
2155 return -1;
2156 }
2157 else if ( startIndex >= array.length )
2158 {
2159 startIndex = array.length - 1;
2160 }
2161 for ( int i = startIndex; i >= 0; i-- )
2162 {
2163 if ( valueToFind == array[i] )
2164 {
2165 return i;
2166 }
2167 }
2168 return -1;
2169 }
2170
2171
2172 /**
2173 * <p>
2174 * Checks if the value is in the given array.
2175 * </p>
2176 * <p>
2177 * The method returns <code>false</code> if a <code>null</code> array is
2178 * passed in.
2179 * </p>
2180 *
2181 * @param array
2182 * the array to search through
2183 * @param valueToFind
2184 * the value to find
2185 * @return <code>true</code> if the array contains the object
2186 */
2187 public static boolean contains( short[] array, short valueToFind )
2188 {
2189 return ( indexOf( array, valueToFind ) != -1 );
2190 }
2191
2192
2193 // char IndexOf
2194 // -----------------------------------------------------------------------
2195 /**
2196 * <p>
2197 * Find the index of the given value in the array.
2198 * </p>
2199 * <p>
2200 * This method returns <code>-1</code> if <code>null</code> array input.
2201 * </p>
2202 *
2203 * @param array
2204 * the array to search through for the object, may be
2205 * <code>null</code>
2206 * @param valueToFind
2207 * the value to find
2208 * @return the index of the value within the array, <code>-1</code> if not
2209 * found or <code>null</code> array input
2210 */
2211 public static int indexOf( char[] array, char valueToFind )
2212 {
2213 return indexOf( array, valueToFind, 0 );
2214 }
2215
2216
2217 /**
2218 * <p>
2219 * Find the index of the given value in the array starting at the given
2220 * index.
2221 * </p>
2222 * <p>
2223 * This method returns <code>-1</code> if <code>null</code> array input.
2224 * </p>
2225 * <p>
2226 * A negative startIndex is treated as zero. A startIndex larger than the
2227 * array length will return -1.
2228 * </p>
2229 *
2230 * @param array
2231 * the array to search through for the object, may be
2232 * <code>null</code>
2233 * @param valueToFind
2234 * the value to find
2235 * @param startIndex
2236 * the index to start searching at
2237 * @return the index of the value within the array, <code>-1</code> if not
2238 * found or <code>null</code> array input
2239 */
2240 public static int indexOf( char[] array, char valueToFind, int startIndex )
2241 {
2242 if ( array == null )
2243 {
2244 return -1;
2245 }
2246 if ( startIndex < 0 )
2247 {
2248 startIndex = 0;
2249 }
2250 for ( int i = startIndex; i < array.length; i++ )
2251 {
2252 if ( valueToFind == array[i] )
2253 {
2254 return i;
2255 }
2256 }
2257 return -1;
2258 }
2259
2260
2261 /**
2262 * <p>
2263 * Find the last index of the given value within the array.
2264 * </p>
2265 * <p>
2266 * This method returns <code>-1</code> if <code>null</code> array input.
2267 * </p>
2268 *
2269 * @param array
2270 * the array to travers backwords looking for the object, may be
2271 * <code>null</code>
2272 * @param valueToFind
2273 * the object to find
2274 * @return the last index of the value within the array, <code>-1</code>
2275 * if not found or <code>null</code> array input
2276 */
2277 public static int lastIndexOf( char[] array, char valueToFind )
2278 {
2279 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
2280 }
2281
2282
2283 /**
2284 * <p>
2285 * Find the last index of the given value in the array starting at the given
2286 * index.
2287 * </p>
2288 * <p>
2289 * This method returns <code>-1</code> if <code>null</code> array input.
2290 * </p>
2291 * <p>
2292 * A negative startIndex will return -1. A startIndex larger than the array
2293 * length will search from the end of the array.
2294 * </p>
2295 *
2296 * @param array
2297 * the array to traverse for looking for the object, may be
2298 * <code>null</code>
2299 * @param valueToFind
2300 * the value to find
2301 * @param startIndex
2302 * the start index to travers backwards from
2303 * @return the last index of the value within the array, <code>-1</code>
2304 * if not found or <code>null</code> array input
2305 */
2306 public static int lastIndexOf( char[] array, char valueToFind, int startIndex )
2307 {
2308 if ( array == null )
2309 {
2310 return -1;
2311 }
2312 if ( startIndex < 0 )
2313 {
2314 return -1;
2315 }
2316 else if ( startIndex >= array.length )
2317 {
2318 startIndex = array.length - 1;
2319 }
2320 for ( int i = startIndex; i >= 0; i-- )
2321 {
2322 if ( valueToFind == array[i] )
2323 {
2324 return i;
2325 }
2326 }
2327 return -1;
2328 }
2329
2330
2331 /**
2332 * <p>
2333 * Checks if the value is in the given array.
2334 * </p>
2335 * <p>
2336 * The method returns <code>false</code> if a <code>null</code> array is
2337 * passed in.
2338 * </p>
2339 *
2340 * @param array
2341 * the array to search through
2342 * @param valueToFind
2343 * the value to find
2344 * @return <code>true</code> if the array contains the object
2345 */
2346 public static boolean contains( char[] array, char valueToFind )
2347 {
2348 return ( indexOf( array, valueToFind ) != -1 );
2349 }
2350
2351
2352 // byte IndexOf
2353 // -----------------------------------------------------------------------
2354 /**
2355 * <p>
2356 * Find the index of the given value in the array.
2357 * </p>
2358 * <p>
2359 * This method returns <code>-1</code> if <code>null</code> array input.
2360 * </p>
2361 *
2362 * @param array
2363 * the array to search through for the object, may be
2364 * <code>null</code>
2365 * @param valueToFind
2366 * the value to find
2367 * @return the index of the value within the array, <code>-1</code> if not
2368 * found or <code>null</code> array input
2369 */
2370 public static int indexOf( byte[] array, byte valueToFind )
2371 {
2372 return indexOf( array, valueToFind, 0 );
2373 }
2374
2375
2376 /**
2377 * <p>
2378 * Find the index of the given value in the array starting at the given
2379 * index.
2380 * </p>
2381 * <p>
2382 * This method returns <code>-1</code> if <code>null</code> array input.
2383 * </p>
2384 * <p>
2385 * A negative startIndex is treated as zero. A startIndex larger than the
2386 * array length will return -1.
2387 * </p>
2388 *
2389 * @param array
2390 * the array to search through for the object, may be
2391 * <code>null</code>
2392 * @param valueToFind
2393 * the value to find
2394 * @param startIndex
2395 * the index to start searching at
2396 * @return the index of the value within the array, <code>-1</code> if not
2397 * found or <code>null</code> array input
2398 */
2399 public static int indexOf( byte[] array, byte valueToFind, int startIndex )
2400 {
2401 if ( array == null )
2402 {
2403 return -1;
2404 }
2405 if ( startIndex < 0 )
2406 {
2407 startIndex = 0;
2408 }
2409 for ( int i = startIndex; i < array.length; i++ )
2410 {
2411 if ( valueToFind == array[i] )
2412 {
2413 return i;
2414 }
2415 }
2416 return -1;
2417 }
2418
2419
2420 /**
2421 * <p>
2422 * Find the last index of the given value within the array.
2423 * </p>
2424 * <p>
2425 * This method returns <code>-1</code> if <code>null</code> array input.
2426 * </p>
2427 *
2428 * @param array
2429 * the array to travers backwords looking for the object, may be
2430 * <code>null</code>
2431 * @param valueToFind
2432 * the object to find
2433 * @return the last index of the value within the array, <code>-1</code>
2434 * if not found or <code>null</code> array input
2435 */
2436 public static int lastIndexOf( byte[] array, byte valueToFind )
2437 {
2438 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
2439 }
2440
2441
2442 /**
2443 * <p>
2444 * Find the last index of the given value in the array starting at the given
2445 * index.
2446 * </p>
2447 * <p>
2448 * This method returns <code>-1</code> if <code>null</code> array input.
2449 * </p>
2450 * <p>
2451 * A negative startIndex will return -1. A startIndex larger than the array
2452 * length will search from the end of the array.
2453 * </p>
2454 *
2455 * @param array
2456 * the array to traverse for looking for the object, may be
2457 * <code>null</code>
2458 * @param valueToFind
2459 * the value to find
2460 * @param startIndex
2461 * the start index to travers backwards from
2462 * @return the last index of the value within the array, <code>-1</code>
2463 * if not found or <code>null</code> array input
2464 */
2465 public static int lastIndexOf( byte[] array, byte valueToFind, int startIndex )
2466 {
2467 if ( array == null )
2468 {
2469 return -1;
2470 }
2471 if ( startIndex < 0 )
2472 {
2473 return -1;
2474 }
2475 else if ( startIndex >= array.length )
2476 {
2477 startIndex = array.length - 1;
2478 }
2479 for ( int i = startIndex; i >= 0; i-- )
2480 {
2481 if ( valueToFind == array[i] )
2482 {
2483 return i;
2484 }
2485 }
2486 return -1;
2487 }
2488
2489
2490 /**
2491 * <p>
2492 * Checks if the value is in the given array.
2493 * </p>
2494 * <p>
2495 * The method returns <code>false</code> if a <code>null</code> array is
2496 * passed in.
2497 * </p>
2498 *
2499 * @param array
2500 * the array to search through
2501 * @param valueToFind
2502 * the value to find
2503 * @return <code>true</code> if the array contains the object
2504 */
2505 public static boolean contains( byte[] array, byte valueToFind )
2506 {
2507 return ( indexOf( array, valueToFind ) != -1 );
2508 }
2509
2510
2511 // double IndexOf
2512 // -----------------------------------------------------------------------
2513 /**
2514 * <p>
2515 * Find the index of the given value in the array.
2516 * </p>
2517 * <p>
2518 * This method returns <code>-1</code> if <code>null</code> array input.
2519 * </p>
2520 *
2521 * @param array
2522 * the array to search through for the object, may be
2523 * <code>null</code>
2524 * @param valueToFind
2525 * the value to find
2526 * @return the index of the value within the array, <code>-1</code> if not
2527 * found or <code>null</code> array input
2528 */
2529 public static int indexOf( double[] array, double valueToFind )
2530 {
2531 return indexOf( array, valueToFind, 0 );
2532 }
2533
2534
2535 /**
2536 * <p>
2537 * Find the index of the given value within a given tolerance in the array.
2538 * This method will return the index of the first value which falls between
2539 * the region defined by valueToFind - tolerance and valueToFind +
2540 * tolerance.
2541 * </p>
2542 * <p>
2543 * This method returns <code>-1</code> if <code>null</code> array input.
2544 * </p>
2545 *
2546 * @param array
2547 * the array to search through for the object, may be
2548 * <code>null</code>
2549 * @param valueToFind
2550 * the value to find
2551 * @param tolerance
2552 * tolerance of the search
2553 * @return the index of the value within the array, <code>-1</code> if not
2554 * found or <code>null</code> array input
2555 */
2556 public static int indexOf( double[] array, double valueToFind, double tolerance )
2557 {
2558 return indexOf( array, valueToFind, 0, tolerance );
2559 }
2560
2561
2562 /**
2563 * <p>
2564 * Find the index of the given value in the array starting at the given
2565 * index.
2566 * </p>
2567 * <p>
2568 * This method returns <code>-1</code> if <code>null</code> array input.
2569 * </p>
2570 * <p>
2571 * A negative startIndex is treated as zero. A startIndex larger than the
2572 * array length will return -1.
2573 * </p>
2574 *
2575 * @param array
2576 * the array to search through for the object, may be
2577 * <code>null</code>
2578 * @param valueToFind
2579 * the value to find
2580 * @param startIndex
2581 * the index to start searching at
2582 * @return the index of the value within the array, <code>-1</code> if not
2583 * found or <code>null</code> array input
2584 */
2585 public static int indexOf( double[] array, double valueToFind, int startIndex )
2586 {
2587 if ( ArrayUtils.isEmpty( array ) )
2588 {
2589 return -1;
2590 }
2591 if ( startIndex < 0 )
2592 {
2593 startIndex = 0;
2594 }
2595 for ( int i = startIndex; i < array.length; i++ )
2596 {
2597 if ( valueToFind == array[i] )
2598 {
2599 return i;
2600 }
2601 }
2602 return -1;
2603 }
2604
2605
2606 /**
2607 * <p>
2608 * Find the index of the given value in the array starting at the given
2609 * index. This method will return the index of the first value which falls
2610 * between the region defined by valueToFind - tolerance and valueToFind +
2611 * tolerance.
2612 * </p>
2613 * <p>
2614 * This method returns <code>-1</code> if <code>null</code> array input.
2615 * </p>
2616 * <p>
2617 * A negative startIndex is treated as zero. A startIndex larger than the
2618 * array length will return -1.
2619 * </p>
2620 *
2621 * @param array
2622 * the array to search through for the object, may be
2623 * <code>null</code>
2624 * @param valueToFind
2625 * the value to find
2626 * @param startIndex
2627 * the index to start searching at
2628 * @param tolerance
2629 * tolerance of the search
2630 * @return the index of the value within the array, <code>-1</code> if not
2631 * found or <code>null</code> array input
2632 */
2633 public static int indexOf( double[] array, double valueToFind, int startIndex, double tolerance )
2634 {
2635 if ( ArrayUtils.isEmpty( array ) )
2636 {
2637 return -1;
2638 }
2639 if ( startIndex < 0 )
2640 {
2641 startIndex = 0;
2642 }
2643 double min = valueToFind - tolerance;
2644 double max = valueToFind + tolerance;
2645 for ( int i = startIndex; i < array.length; i++ )
2646 {
2647 if ( array[i] >= min && array[i] <= max )
2648 {
2649 return i;
2650 }
2651 }
2652 return -1;
2653 }
2654
2655
2656 /**
2657 * <p>
2658 * Find the last index of the given value within the array.
2659 * </p>
2660 * <p>
2661 * This method returns <code>-1</code> if <code>null</code> array input.
2662 * </p>
2663 *
2664 * @param array
2665 * the array to travers backwords looking for the object, may be
2666 * <code>null</code>
2667 * @param valueToFind
2668 * the object to find
2669 * @return the last index of the value within the array, <code>-1</code>
2670 * if not found or <code>null</code> array input
2671 */
2672 public static int lastIndexOf( double[] array, double valueToFind )
2673 {
2674 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
2675 }
2676
2677
2678 /**
2679 * <p>
2680 * Find the last index of the given value within a given tolerance in the
2681 * array. This method will return the index of the last value which falls
2682 * between the region defined by valueToFind - tolerance and valueToFind +
2683 * tolerance.
2684 * </p>
2685 * <p>
2686 * This method returns <code>-1</code> if <code>null</code> array input.
2687 * </p>
2688 *
2689 * @param array
2690 * the array to search through for the object, may be
2691 * <code>null</code>
2692 * @param valueToFind
2693 * the value to find
2694 * @param tolerance
2695 * tolerance of the search
2696 * @return the index of the value within the array, <code>-1</code> if not
2697 * found or <code>null</code> array input
2698 */
2699 public static int lastIndexOf( double[] array, double valueToFind, double tolerance )
2700 {
2701 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE, tolerance );
2702 }
2703
2704
2705 /**
2706 * <p>
2707 * Find the last index of the given value in the array starting at the given
2708 * index.
2709 * </p>
2710 * <p>
2711 * This method returns <code>-1</code> if <code>null</code> array input.
2712 * </p>
2713 * <p>
2714 * A negative startIndex will return -1. A startIndex larger than the array
2715 * length will search from the end of the array.
2716 * </p>
2717 *
2718 * @param array
2719 * the array to traverse for looking for the object, may be
2720 * <code>null</code>
2721 * @param valueToFind
2722 * the value to find
2723 * @param startIndex
2724 * the start index to travers backwards from
2725 * @return the last index of the value within the array, <code>-1</code>
2726 * if not found or <code>null</code> array input
2727 */
2728 public static int lastIndexOf( double[] array, double valueToFind, int startIndex )
2729 {
2730 if ( ArrayUtils.isEmpty( array ) )
2731 {
2732 return -1;
2733 }
2734 if ( startIndex < 0 )
2735 {
2736 return -1;
2737 }
2738 else if ( startIndex >= array.length )
2739 {
2740 startIndex = array.length - 1;
2741 }
2742 for ( int i = startIndex; i >= 0; i-- )
2743 {
2744 if ( valueToFind == array[i] )
2745 {
2746 return i;
2747 }
2748 }
2749 return -1;
2750 }
2751
2752
2753 /**
2754 * <p>
2755 * Find the last index of the given value in the array starting at the given
2756 * index. This method will return the index of the last value which falls
2757 * between the region defined by valueToFind - tolerance and valueToFind +
2758 * tolerance.
2759 * </p>
2760 * <p>
2761 * This method returns <code>-1</code> if <code>null</code> array input.
2762 * </p>
2763 * <p>
2764 * A negative startIndex will return -1. A startIndex larger than the array
2765 * length will search from the end of the array.
2766 * </p>
2767 *
2768 * @param array
2769 * the array to traverse for looking for the object, may be
2770 * <code>null</code>
2771 * @param valueToFind
2772 * the value to find
2773 * @param startIndex
2774 * the start index to travers backwards from
2775 * @param tolerance
2776 * search for value within plus/minus this amount
2777 * @return the last index of the value within the array, <code>-1</code>
2778 * if not found or <code>null</code> array input
2779 */
2780 public static int lastIndexOf( double[] array, double valueToFind, int startIndex, double tolerance )
2781 {
2782 if ( ArrayUtils.isEmpty( array ) )
2783 {
2784 return -1;
2785 }
2786 if ( startIndex < 0 )
2787 {
2788 return -1;
2789 }
2790 else if ( startIndex >= array.length )
2791 {
2792 startIndex = array.length - 1;
2793 }
2794 double min = valueToFind - tolerance;
2795 double max = valueToFind + tolerance;
2796 for ( int i = startIndex; i >= 0; i-- )
2797 {
2798 if ( array[i] >= min && array[i] <= max )
2799 {
2800 return i;
2801 }
2802 }
2803 return -1;
2804 }
2805
2806
2807 /**
2808 * <p>
2809 * Checks if the value is in the given array.
2810 * </p>
2811 * <p>
2812 * The method returns <code>false</code> if a <code>null</code> array is
2813 * passed in.
2814 * </p>
2815 *
2816 * @param array
2817 * the array to search through
2818 * @param valueToFind
2819 * the value to find
2820 * @return <code>true</code> if the array contains the object
2821 */
2822 public static boolean contains( double[] array, double valueToFind )
2823 {
2824 return ( indexOf( array, valueToFind ) != -1 );
2825 }
2826
2827
2828 /**
2829 * <p>
2830 * Checks if a value falling within the given tolerance is in the given
2831 * array. If the array contains a value within the inclusive range defined
2832 * by (value - tolerance) to (value + tolerance).
2833 * </p>
2834 * <p>
2835 * The method returns <code>false</code> if a <code>null</code> array is
2836 * passed in.
2837 * </p>
2838 *
2839 * @param array
2840 * the array to search
2841 * @param valueToFind
2842 * the value to find
2843 * @param tolerance
2844 * the array contains the tolerance of the search
2845 * @return true if value falling within tolerance is in array
2846 */
2847 public static boolean contains( double[] array, double valueToFind, double tolerance )
2848 {
2849 return ( indexOf( array, valueToFind, 0, tolerance ) != -1 );
2850 }
2851
2852
2853 // float IndexOf
2854 // -----------------------------------------------------------------------
2855 /**
2856 * <p>
2857 * Find the index of the given value in the array.
2858 * </p>
2859 * <p>
2860 * This method returns <code>-1</code> if <code>null</code> array input.
2861 * </p>
2862 *
2863 * @param array
2864 * the array to search through for the object, may be
2865 * <code>null</code>
2866 * @param valueToFind
2867 * the value to find
2868 * @return the index of the value within the array, <code>-1</code> if not
2869 * found or <code>null</code> array input
2870 */
2871 public static int indexOf( float[] array, float valueToFind )
2872 {
2873 return indexOf( array, valueToFind, 0 );
2874 }
2875
2876
2877 /**
2878 * <p>
2879 * Find the index of the given value in the array starting at the given
2880 * index.
2881 * </p>
2882 * <p>
2883 * This method returns <code>-1</code> if <code>null</code> array input.
2884 * </p>
2885 * <p>
2886 * A negative startIndex is treated as zero. A startIndex larger than the
2887 * array length will return -1.
2888 * </p>
2889 *
2890 * @param array
2891 * the array to search through for the object, may be
2892 * <code>null</code>
2893 * @param valueToFind
2894 * the value to find
2895 * @param startIndex
2896 * the index to start searching at
2897 * @return the index of the value within the array, <code>-1</code> if not
2898 * found or <code>null</code> array input
2899 */
2900 public static int indexOf( float[] array, float valueToFind, int startIndex )
2901 {
2902 if ( ArrayUtils.isEmpty( array ) )
2903 {
2904 return -1;
2905 }
2906 if ( startIndex < 0 )
2907 {
2908 startIndex = 0;
2909 }
2910 for ( int i = startIndex; i < array.length; i++ )
2911 {
2912 if ( valueToFind == array[i] )
2913 {
2914 return i;
2915 }
2916 }
2917 return -1;
2918 }
2919
2920
2921 /**
2922 * <p>
2923 * Find the last index of the given value within the array.
2924 * </p>
2925 * <p>
2926 * This method returns <code>-1</code> if <code>null</code> array input.
2927 * </p>
2928 *
2929 * @param array
2930 * the array to travers backwords looking for the object, may be
2931 * <code>null</code>
2932 * @param valueToFind
2933 * the object to find
2934 * @return the last index of the value within the array, <code>-1</code>
2935 * if not found or <code>null</code> array input
2936 */
2937 public static int lastIndexOf( float[] array, float valueToFind )
2938 {
2939 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
2940 }
2941
2942
2943 /**
2944 * <p>
2945 * Find the last index of the given value in the array starting at the given
2946 * index.
2947 * </p>
2948 * <p>
2949 * This method returns <code>-1</code> if <code>null</code> array input.
2950 * </p>
2951 * <p>
2952 * A negative startIndex will return -1. A startIndex larger than the array
2953 * length will search from the end of the array.
2954 * </p>
2955 *
2956 * @param array
2957 * the array to traverse for looking for the object, may be
2958 * <code>null</code>
2959 * @param valueToFind
2960 * the value to find
2961 * @param startIndex
2962 * the start index to travers backwards from
2963 * @return the last index of the value within the array, <code>-1</code>
2964 * if not found or <code>null</code> array input
2965 */
2966 public static int lastIndexOf( float[] array, float valueToFind, int startIndex )
2967 {
2968 if ( ArrayUtils.isEmpty( array ) )
2969 {
2970 return -1;
2971 }
2972 if ( startIndex < 0 )
2973 {
2974 return -1;
2975 }
2976 else if ( startIndex >= array.length )
2977 {
2978 startIndex = array.length - 1;
2979 }
2980 for ( int i = startIndex; i >= 0; i-- )
2981 {
2982 if ( valueToFind == array[i] )
2983 {
2984 return i;
2985 }
2986 }
2987 return -1;
2988 }
2989
2990
2991 /**
2992 * <p>
2993 * Checks if the value is in the given array.
2994 * </p>
2995 * <p>
2996 * The method returns <code>false</code> if a <code>null</code> array is
2997 * passed in.
2998 * </p>
2999 *
3000 * @param array
3001 * the array to search through
3002 * @param valueToFind
3003 * the value to find
3004 * @return <code>true</code> if the array contains the object
3005 */
3006 public static boolean contains( float[] array, float valueToFind )
3007 {
3008 return ( indexOf( array, valueToFind ) != -1 );
3009 }
3010
3011
3012 // boolean IndexOf
3013 // -----------------------------------------------------------------------
3014 /**
3015 * <p>
3016 * Find the index of the given value in the array.
3017 * </p>
3018 * <p>
3019 * This method returns <code>-1</code> if <code>null</code> array input.
3020 * </p>
3021 *
3022 * @param array
3023 * the array to search through for the object, may be
3024 * <code>null</code>
3025 * @param valueToFind
3026 * the value to find
3027 * @return the index of the value within the array, <code>-1</code> if not
3028 * found or <code>null</code> array input
3029 */
3030 public static int indexOf( boolean[] array, boolean valueToFind )
3031 {
3032 return indexOf( array, valueToFind, 0 );
3033 }
3034
3035
3036 /**
3037 * <p>
3038 * Find the index of the given value in the array starting at the given
3039 * index.
3040 * </p>
3041 * <p>
3042 * This method returns <code>-1</code> if <code>null</code> array input.
3043 * </p>
3044 * <p>
3045 * A negative startIndex is treated as zero. A startIndex larger than the
3046 * array length will return -1.
3047 * </p>
3048 *
3049 * @param array
3050 * the array to search through for the object, may be
3051 * <code>null</code>
3052 * @param valueToFind
3053 * the value to find
3054 * @param startIndex
3055 * the index to start searching at
3056 * @return the index of the value within the array, <code>-1</code> if not
3057 * found or <code>null</code> array input
3058 */
3059 public static int indexOf( boolean[] array, boolean valueToFind, int startIndex )
3060 {
3061 if ( ArrayUtils.isEmpty( array ) )
3062 {
3063 return -1;
3064 }
3065 if ( startIndex < 0 )
3066 {
3067 startIndex = 0;
3068 }
3069 for ( int i = startIndex; i < array.length; i++ )
3070 {
3071 if ( valueToFind == array[i] )
3072 {
3073 return i;
3074 }
3075 }
3076 return -1;
3077 }
3078
3079
3080 /**
3081 * <p>
3082 * Find the last index of the given value within the array.
3083 * </p>
3084 * <p>
3085 * This method returns <code>-1</code> if <code>null</code> array input.
3086 * </p>
3087 *
3088 * @param array
3089 * the array to travers backwords looking for the object, may be
3090 * <code>null</code>
3091 * @param valueToFind
3092 * the object to find
3093 * @return the last index of the value within the array, <code>-1</code>
3094 * if not found or <code>null</code> array input
3095 */
3096 public static int lastIndexOf( boolean[] array, boolean valueToFind )
3097 {
3098 return lastIndexOf( array, valueToFind, Integer.MAX_VALUE );
3099 }
3100
3101
3102 /**
3103 * <p>
3104 * Find the last index of the given value in the array starting at the given
3105 * index.
3106 * </p>
3107 * <p>
3108 * This method returns <code>-1</code> if <code>null</code> array input.
3109 * </p>
3110 * <p>
3111 * A negative startIndex will return -1. A startIndex larger than the array
3112 * length will search from the end of the array.
3113 * </p>
3114 *
3115 * @param array
3116 * the array to traverse for looking for the object, may be
3117 * <code>null</code>
3118 * @param valueToFind
3119 * the value to find
3120 * @param startIndex
3121 * the start index to travers backwards from
3122 * @return the last index of the value within the array, <code>-1</code>
3123 * if not found or <code>null</code> array input
3124 */
3125 public static int lastIndexOf( boolean[] array, boolean valueToFind, int startIndex )
3126 {
3127 if ( ArrayUtils.isEmpty( array ) )
3128 {
3129 return -1;
3130 }
3131 if ( startIndex < 0 )
3132 {
3133 return -1;
3134 }
3135 else if ( startIndex >= array.length )
3136 {
3137 startIndex = array.length - 1;
3138 }
3139 for ( int i = startIndex; i >= 0; i-- )
3140 {
3141 if ( valueToFind == array[i] )
3142 {
3143 return i;
3144 }
3145 }
3146 return -1;
3147 }
3148
3149
3150 /**
3151 * <p>
3152 * Checks if the value is in the given array.
3153 * </p>
3154 * <p>
3155 * The method returns <code>false</code> if a <code>null</code> array is
3156 * passed in.
3157 * </p>
3158 *
3159 * @param array
3160 * the array to search through
3161 * @param valueToFind
3162 * the value to find
3163 * @return <code>true</code> if the array contains the object
3164 */
3165 public static boolean contains( boolean[] array, boolean valueToFind )
3166 {
3167 return ( indexOf( array, valueToFind ) != -1 );
3168 }
3169
3170
3171 // Primitive/Object array converters
3172 // ----------------------------------------------------------------------
3173
3174 // Long array converters
3175 // ----------------------------------------------------------------------
3176 /**
3177 * <p>
3178 * Converts an array of object Longs to primitives.
3179 * </p>
3180 * <p>
3181 * This method returns <code>null</code> if <code>null</code> array
3182 * input.
3183 * </p>
3184 *
3185 * @param array
3186 * a <code>Long</code> array, may be <code>null</code>
3187 * @return a <code>long</code> array, <code>null</code> if null array
3188 * input
3189 * @throws NullPointerException
3190 * if array content is <code>null</code>
3191 */
3192 public static long[] toPrimitive( Long[] array )
3193 {
3194 if ( array == null )
3195 {
3196 return null;
3197 }
3198 else if ( array.length == 0 )
3199 {
3200 return EMPTY_LONG_ARRAY;
3201 }
3202 final long[] result = new long[array.length];
3203 for ( int i = 0; i < array.length; i++ )
3204 {
3205 result[i] = array[i].longValue();
3206 }
3207 return result;
3208 }
3209
3210
3211 /**
3212 * <p>
3213 * Converts an array of object Long to primitives handling <code>null</code>.
3214 * </p>
3215 * <p>
3216 * This method returns <code>null</code> if <code>null</code> array
3217 * input.
3218 * </p>
3219 *
3220 * @param array
3221 * a <code>Long</code> array, may be <code>null</code>
3222 * @param valueForNull
3223 * the value to insert if <code>null</code> found
3224 * @return a <code>long</code> array, <code>null</code> if null array
3225 * input
3226 */
3227 public static long[] toPrimitive( Long[] array, long valueForNull )
3228 {
3229 if ( array == null )
3230 {
3231 return null;
3232 }
3233 else if ( array.length == 0 )
3234 {
3235 return EMPTY_LONG_ARRAY;
3236 }
3237 final long[] result = new long[array.length];
3238 for ( int i = 0; i < array.length; i++ )
3239 {
3240 Long b = array[i];
3241 result[i] = ( b == null ? valueForNull : b.longValue() );
3242 }
3243 return result;
3244 }
3245
3246
3247 /**
3248 * <p>
3249 * Converts an array of primitive longs to objects.
3250 * </p>
3251 * <p>
3252 * This method returns <code>null</code> if <code>null</code> array
3253 * input.
3254 * </p>
3255 *
3256 * @param array
3257 * a <code>long</code> array
3258 * @return a <code>Long</code> array, <code>null</code> if null array
3259 * input
3260 */
3261 public static Long[] toObject( long[] array )
3262 {
3263 if ( array == null )
3264 {
3265 return null;
3266 }
3267 else if ( array.length == 0 )
3268 {
3269 return EMPTY_LONG_OBJECT_ARRAY;
3270 }
3271 final Long[] result = new Long[array.length];
3272 for ( int i = 0; i < array.length; i++ )
3273 {
3274 result[i] = new Long( array[i] );
3275 }
3276 return result;
3277 }
3278
3279
3280 // Int array converters
3281 // ----------------------------------------------------------------------
3282 /**
3283 * <p>
3284 * Converts an array of object Integers to primitives.
3285 * </p>
3286 * <p>
3287 * This method returns <code>null</code> if <code>null</code> array
3288 * input.
3289 * </p>
3290 *
3291 * @param array
3292 * a <code>Integer</code> array, may be <code>null</code>
3293 * @return an <code>int</code> array, <code>null</code> if null array
3294 * input
3295 * @throws NullPointerException
3296 * if array content is <code>null</code>
3297 */
3298 public static int[] toPrimitive( Integer[] array )
3299 {
3300 if ( array == null )
3301 {
3302 return null;
3303 }
3304 else if ( array.length == 0 )
3305 {
3306 return EMPTY_INT_ARRAY;
3307 }
3308 final int[] result = new int[array.length];
3309 for ( int i = 0; i < array.length; i++ )
3310 {
3311 result[i] = array[i].intValue();
3312 }
3313 return result;
3314 }
3315
3316
3317 /**
3318 * <p>
3319 * Converts an array of object Integer to primitives handling
3320 * <code>null</code>.
3321 * </p>
3322 * <p>
3323 * This method returns <code>null</code> if <code>null</code> array
3324 * input.
3325 * </p>
3326 *
3327 * @param array
3328 * a <code>Integer</code> array, may be <code>null</code>
3329 * @param valueForNull
3330 * the value to insert if <code>null</code> found
3331 * @return an <code>int</code> array, <code>null</code> if null array
3332 * input
3333 */
3334 public static int[] toPrimitive( Integer[] array, int valueForNull )
3335 {
3336 if ( array == null )
3337 {
3338 return null;
3339 }
3340 else if ( array.length == 0 )
3341 {
3342 return EMPTY_INT_ARRAY;
3343 }
3344 final int[] result = new int[array.length];
3345 for ( int i = 0; i < array.length; i++ )
3346 {
3347 Integer b = array[i];
3348 result[i] = ( b == null ? valueForNull : b.intValue() );
3349 }
3350 return result;
3351 }
3352
3353
3354 /**
3355 * <p>
3356 * Converts an array of primitive ints to objects.
3357 * </p>
3358 * <p>
3359 * This method returns <code>null</code> if <code>null</code> array
3360 * input.
3361 * </p>
3362 *
3363 * @param array
3364 * an <code>int</code> array
3365 * @return an <code>Integer</code> array, <code>null</code> if null
3366 * array input
3367 */
3368 public static Integer[] toObject( int[] array )
3369 {
3370 if ( array == null )
3371 {
3372 return null;
3373 }
3374 else if ( array.length == 0 )
3375 {
3376 return EMPTY_INTEGER_OBJECT_ARRAY;
3377 }
3378 final Integer[] result = new Integer[array.length];
3379 for ( int i = 0; i < array.length; i++ )
3380 {
3381 result[i] = new Integer( array[i] );
3382 }
3383 return result;
3384 }
3385
3386
3387 // Short array converters
3388 // ----------------------------------------------------------------------
3389 /**
3390 * <p>
3391 * Converts an array of object Shorts to primitives.
3392 * </p>
3393 * <p>
3394 * This method returns <code>null</code> if <code>null</code> array
3395 * input.
3396 * </p>
3397 *
3398 * @param array
3399 * a <code>Short</code> array, may be <code>null</code>
3400 * @return a <code>byte</code> array, <code>null</code> if null array
3401 * input
3402 * @throws NullPointerException
3403 * if array content is <code>null</code>
3404 */
3405 public static short[] toPrimitive( Short[] array )
3406 {
3407 if ( array == null )
3408 {
3409 return null;
3410 }
3411 else if ( array.length == 0 )
3412 {
3413 return EMPTY_SHORT_ARRAY;
3414 }
3415 final short[] result = new short[array.length];
3416 for ( int i = 0; i < array.length; i++ )
3417 {
3418 result[i] = array[i].shortValue();
3419 }
3420 return result;
3421 }
3422
3423
3424 /**
3425 * <p>
3426 * Converts an array of object Short to primitives handling
3427 * <code>null</code>.
3428 * </p>
3429 * <p>
3430 * This method returns <code>null</code> if <code>null</code> array
3431 * input.
3432 * </p>
3433 *
3434 * @param array
3435 * a <code>Short</code> array, may be <code>null</code>
3436 * @param valueForNull
3437 * the value to insert if <code>null</code> found
3438 * @return a <code>byte</code> array, <code>null</code> if null array
3439 * input
3440 */
3441 public static short[] toPrimitive( Short[] array, short valueForNull )
3442 {
3443 if ( array == null )
3444 {
3445 return null;
3446 }
3447 else if ( array.length == 0 )
3448 {
3449 return EMPTY_SHORT_ARRAY;
3450 }
3451 final short[] result = new short[array.length];
3452 for ( int i = 0; i < array.length; i++ )
3453 {
3454 Short b = array[i];
3455 result[i] = ( b == null ? valueForNull : b.shortValue() );
3456 }
3457 return result;
3458 }
3459
3460
3461 /**
3462 * <p>
3463 * Converts an array of primitive shorts to objects.
3464 * </p>
3465 * <p>
3466 * This method returns <code>null</code> if <code>null</code> array
3467 * input.
3468 * </p>
3469 *
3470 * @param array
3471 * a <code>short</code> array
3472 * @return a <code>Short</code> array, <code>null</code> if null array
3473 * input
3474 */
3475 public static Short[] toObject( short[] array )
3476 {
3477 if ( array == null )
3478 {
3479 return null;
3480 }
3481 else if ( array.length == 0 )
3482 {
3483 return EMPTY_SHORT_OBJECT_ARRAY;
3484 }
3485 final Short[] result = new Short[array.length];
3486 for ( int i = 0; i < array.length; i++ )
3487 {
3488 result[i] = new Short( array[i] );
3489 }
3490 return result;
3491 }
3492
3493
3494 // Byte array converters
3495 // ----------------------------------------------------------------------
3496 /**
3497 * <p>
3498 * Converts an array of object Bytes to primitives.
3499 * </p>
3500 * <p>
3501 * This method returns <code>null</code> if <code>null</code> array
3502 * input.
3503 * </p>
3504 *
3505 * @param array
3506 * a <code>Byte</code> array, may be <code>null</code>
3507 * @return a <code>byte</code> array, <code>null</code> if null array
3508 * input
3509 * @throws NullPointerException
3510 * if array content is <code>null</code>
3511 */
3512 public static byte[] toPrimitive( Byte[] array )
3513 {
3514 if ( array == null )
3515 {
3516 return null;
3517 }
3518 else if ( array.length == 0 )
3519 {
3520 return EMPTY_BYTE_ARRAY;
3521 }
3522 final byte[] result = new byte[array.length];
3523 for ( int i = 0; i < array.length; i++ )
3524 {
3525 result[i] = array[i].byteValue();
3526 }
3527 return result;
3528 }
3529
3530
3531 /**
3532 * <p>
3533 * Converts an array of object Bytes to primitives handling
3534 * <code>null</code>.
3535 * </p>
3536 * <p>
3537 * This method returns <code>null</code> if <code>null</code> array
3538 * input.
3539 * </p>
3540 *
3541 * @param array
3542 * a <code>Byte</code> array, may be <code>null</code>
3543 * @param valueForNull
3544 * the value to insert if <code>null</code> found
3545 * @return a <code>byte</code> array, <code>null</code> if null array
3546 * input
3547 */
3548 public static byte[] toPrimitive( Byte[] array, byte valueForNull )
3549 {
3550 if ( array == null )
3551 {
3552 return null;
3553 }
3554 else if ( array.length == 0 )
3555 {
3556 return EMPTY_BYTE_ARRAY;
3557 }
3558 final byte[] result = new byte[array.length];
3559 for ( int i = 0; i < array.length; i++ )
3560 {
3561 Byte b = array[i];
3562 result[i] = ( b == null ? valueForNull : b.byteValue() );
3563 }
3564 return result;
3565 }
3566
3567
3568 /**
3569 * <p>
3570 * Converts an array of primitive bytes to objects.
3571 * </p>
3572 * <p>
3573 * This method returns <code>null</code> if <code>null</code> array
3574 * input.
3575 * </p>
3576 *
3577 * @param array
3578 * a <code>byte</code> array
3579 * @return a <code>Byte</code> array, <code>null</code> if null array
3580 * input
3581 */
3582 public static Byte[] toObject( byte[] array )
3583 {
3584 if ( array == null )
3585 {
3586 return null;
3587 }
3588 else if ( array.length == 0 )
3589 {
3590 return EMPTY_BYTE_OBJECT_ARRAY;
3591 }
3592 final Byte[] result = new Byte[array.length];
3593 for ( int i = 0; i < array.length; i++ )
3594 {
3595 result[i] = new Byte( array[i] );
3596 }
3597 return result;
3598 }
3599
3600
3601 // Double array converters
3602 // ----------------------------------------------------------------------
3603 /**
3604 * <p>
3605 * Converts an array of object Doubles to primitives.
3606 * </p>
3607 * <p>
3608 * This method returns <code>null</code> if <code>null</code> array
3609 * input.
3610 * </p>
3611 *
3612 * @param array
3613 * a <code>Double</code> array, may be <code>null</code>
3614 * @return a <code>double</code> array, <code>null</code> if null array
3615 * input
3616 * @throws NullPointerException
3617 * if array content is <code>null</code>
3618 */
3619 public static double[] toPrimitive( Double[] array )
3620 {
3621 if ( array == null )
3622 {
3623 return null;
3624 }
3625 else if ( array.length == 0 )
3626 {
3627 return EMPTY_DOUBLE_ARRAY;
3628 }
3629 final double[] result = new double[array.length];
3630 for ( int i = 0; i < array.length; i++ )
3631 {
3632 result[i] = array[i].doubleValue();
3633 }
3634 return result;
3635 }
3636
3637
3638 /**
3639 * <p>
3640 * Converts an array of object Doubles to primitives handling
3641 * <code>null</code>.
3642 * </p>
3643 * <p>
3644 * This method returns <code>null</code> if <code>null</code> array
3645 * input.
3646 * </p>
3647 *
3648 * @param array
3649 * a <code>Double</code> array, may be <code>null</code>
3650 * @param valueForNull
3651 * the value to insert if <code>null</code> found
3652 * @return a <code>double</code> array, <code>null</code> if null array
3653 * input
3654 */
3655 public static double[] toPrimitive( Double[] array, double valueForNull )
3656 {
3657 if ( array == null )
3658 {
3659 return null;
3660 }
3661 else if ( array.length == 0 )
3662 {
3663 return EMPTY_DOUBLE_ARRAY;
3664 }
3665 final double[] result = new double[array.length];
3666 for ( int i = 0; i < array.length; i++ )
3667 {
3668 Double b = array[i];
3669 result[i] = ( b == null ? valueForNull : b.doubleValue() );
3670 }
3671 return result;
3672 }
3673
3674
3675 /**
3676 * <p>
3677 * Converts an array of primitive doubles to objects.
3678 * </p>
3679 * <p>
3680 * This method returns <code>null</code> if <code>null</code> array
3681 * input.
3682 * </p>
3683 *
3684 * @param array
3685 * a <code>double</code> array
3686 * @return a <code>Double</code> array, <code>null</code> if null array
3687 * input
3688 */
3689 public static Double[] toObject( double[] array )
3690 {
3691 if ( array == null )
3692 {
3693 return null;
3694 }
3695 else if ( array.length == 0 )
3696 {
3697 return EMPTY_DOUBLE_OBJECT_ARRAY;
3698 }
3699 final Double[] result = new Double[array.length];
3700 for ( int i = 0; i < array.length; i++ )
3701 {
3702 result[i] = Double.valueOf( array[i] );
3703 }
3704 return result;
3705 }
3706
3707
3708 // Float array converters
3709 // ----------------------------------------------------------------------
3710 /**
3711 * <p>
3712 * Converts an array of object Floats to primitives.
3713 * </p>
3714 * <p>
3715 * This method returns <code>null</code> if <code>null</code> array
3716 * input.
3717 * </p>
3718 *
3719 * @param array
3720 * a <code>Float</code> array, may be <code>null</code>
3721 * @return a <code>float</code> array, <code>null</code> if null array
3722 * input
3723 * @throws NullPointerException
3724 * if array content is <code>null</code>
3725 */
3726 public static float[] toPrimitive( Float[] array )
3727 {
3728 if ( array == null )
3729 {
3730 return null;
3731 }
3732 else if ( array.length == 0 )
3733 {
3734 return EMPTY_FLOAT_ARRAY;
3735 }
3736 final float[] result = new float[array.length];
3737 for ( int i = 0; i < array.length; i++ )
3738 {
3739 result[i] = array[i].floatValue();
3740 }
3741 return result;
3742 }
3743
3744
3745 /**
3746 * <p>
3747 * Converts an array of object Floats to primitives handling
3748 * <code>null</code>.
3749 * </p>
3750 * <p>
3751 * This method returns <code>null</code> if <code>null</code> array
3752 * input.
3753 * </p>
3754 *
3755 * @param array
3756 * a <code>Float</code> array, may be <code>null</code>
3757 * @param valueForNull
3758 * the value to insert if <code>null</code> found
3759 * @return a <code>float</code> array, <code>null</code> if null array
3760 * input
3761 */
3762 public static float[] toPrimitive( Float[] array, float valueForNull )
3763 {
3764 if ( array == null )
3765 {
3766 return null;
3767 }
3768 else if ( array.length == 0 )
3769 {
3770 return EMPTY_FLOAT_ARRAY;
3771 }
3772 final float[] result = new float[array.length];
3773 for ( int i = 0; i < array.length; i++ )
3774 {
3775 Float b = array[i];
3776 result[i] = ( b == null ? valueForNull : b.floatValue() );
3777 }
3778 return result;
3779 }
3780
3781
3782 /**
3783 * <p>
3784 * Converts an array of primitive floats to objects.
3785 * </p>
3786 * <p>
3787 * This method returns <code>null</code> if <code>null</code> array
3788 * input.
3789 * </p>
3790 *
3791 * @param array
3792 * a <code>float</code> array
3793 * @return a <code>Float</code> array, <code>null</code> if null array
3794 * input
3795 */
3796 public static Float[] toObject( float[] array )
3797 {
3798 if ( array == null )
3799 {
3800 return null;
3801 }
3802 else if ( array.length == 0 )
3803 {
3804 return EMPTY_FLOAT_OBJECT_ARRAY;
3805 }
3806 final Float[] result = new Float[array.length];
3807 for ( int i = 0; i < array.length; i++ )
3808 {
3809 result[i] = Float.valueOf( array[i] );
3810 }
3811 return result;
3812 }
3813
3814
3815 // Boolean array converters
3816 // ----------------------------------------------------------------------
3817 /**
3818 * <p>
3819 * Converts an array of object Booleans to primitives.
3820 * </p>
3821 * <p>
3822 * This method returns <code>null</code> if <code>null</code> array
3823 * input.
3824 * </p>
3825 *
3826 * @param array
3827 * a <code>Boolean</code> array, may be <code>null</code>
3828 * @return a <code>boolean</code> array, <code>null</code> if null array
3829 * input
3830 * @throws NullPointerException
3831 * if array content is <code>null</code>
3832 */
3833 public static boolean[] toPrimitive( Boolean[] array )
3834 {
3835 if ( array == null )
3836 {
3837 return null;
3838 }
3839 else if ( array.length == 0 )
3840 {
3841 return EMPTY_BOOLEAN_ARRAY;
3842 }
3843 final boolean[] result = new boolean[array.length];
3844 for ( int i = 0; i < array.length; i++ )
3845 {
3846 result[i] = array[i].booleanValue();
3847 }
3848 return result;
3849 }
3850
3851
3852 /**
3853 * <p>
3854 * Converts an array of object Booleans to primitives handling
3855 * <code>null</code>.
3856 * </p>
3857 * <p>
3858 * This method returns <code>null</code> if <code>null</code> array
3859 * input.
3860 * </p>
3861 *
3862 * @param array
3863 * a <code>Boolean</code> array, may be <code>null</code>
3864 * @param valueForNull
3865 * the value to insert if <code>null</code> found
3866 * @return a <code>boolean</code> array, <code>null</code> if null array
3867 * input
3868 */
3869 public static boolean[] toPrimitive( Boolean[] array, boolean valueForNull )
3870 {
3871 if ( array == null )
3872 {
3873 return null;
3874 }
3875 else if ( array.length == 0 )
3876 {
3877 return EMPTY_BOOLEAN_ARRAY;
3878 }
3879 final boolean[] result = new boolean[array.length];
3880 for ( int i = 0; i < array.length; i++ )
3881 {
3882 Boolean b = array[i];
3883 result[i] = ( b == null ? valueForNull : b.booleanValue() );
3884 }
3885 return result;
3886 }
3887
3888
3889 /**
3890 * <p>
3891 * Converts an array of primitive booleans to objects.
3892 * </p>
3893 * <p>
3894 * This method returns <code>null</code> if <code>null</code> array
3895 * input.
3896 * </p>
3897 *
3898 * @param array
3899 * a <code>boolean</code> array
3900 * @return a <code>Boolean</code> array, <code>null</code> if null array
3901 * input
3902 */
3903 public static Boolean[] toObject( boolean[] array )
3904 {
3905 if ( array == null )
3906 {
3907 return null;
3908 }
3909 else if ( array.length == 0 )
3910 {
3911 return EMPTY_BOOLEAN_OBJECT_ARRAY;
3912 }
3913 final Boolean[] result = new Boolean[array.length];
3914 for ( int i = 0; i < array.length; i++ )
3915 {
3916 result[i] = ( array[i] ? Boolean.TRUE : Boolean.FALSE );
3917 }
3918 return result;
3919 }
3920
3921
3922 // ----------------------------------------------------------------------
3923 /**
3924 * <p>
3925 * Checks if an array of Objects is empty or <code>null</code>.
3926 * </p>
3927 *
3928 * @param array
3929 * the array to test
3930 * @return <code>true</code> if the array is empty or <code>null</code>
3931 * @since 2.1
3932 */
3933 public static boolean isEmpty( Object[] array )
3934 {
3935 if ( array == null || array.length == 0 )
3936 {
3937 return true;
3938 }
3939 return false;
3940 }
3941
3942
3943 /**
3944 * <p>
3945 * Checks if an array of primitive longs is empty or <code>null</code>.
3946 * </p>
3947 *
3948 * @param array
3949 * the array to test
3950 * @return <code>true</code> if the array is empty or <code>null</code>
3951 * @since 2.1
3952 */
3953 public static boolean isEmpty( long[] array )
3954 {
3955 if ( array == null || array.length == 0 )
3956 {
3957 return true;
3958 }
3959 return false;
3960 }
3961
3962
3963 /**
3964 * <p>
3965 * Checks if an array of primitive ints is empty or <code>null</code>.
3966 * </p>
3967 *
3968 * @param array
3969 * the array to test
3970 * @return <code>true</code> if the array is empty or <code>null</code>
3971 * @since 2.1
3972 */
3973 public static boolean isEmpty( int[] array )
3974 {
3975 if ( array == null || array.length == 0 )
3976 {
3977 return true;
3978 }
3979 return false;
3980 }
3981
3982
3983 /**
3984 * <p>
3985 * Checks if an array of primitive shorts is empty or <code>null</code>.
3986 * </p>
3987 *
3988 * @param array
3989 * the array to test
3990 * @return <code>true</code> if the array is empty or <code>null</code>
3991 * @since 2.1
3992 */
3993 public static boolean isEmpty( short[] array )
3994 {
3995 if ( array == null || array.length == 0 )
3996 {
3997 return true;
3998 }
3999 return false;
4000 }
4001
4002
4003 /**
4004 * <p>
4005 * Checks if an array of primitive chars is empty or <code>null</code>.
4006 * </p>
4007 *
4008 * @param array
4009 * the array to test
4010 * @return <code>true</code> if the array is empty or <code>null</code>
4011 * @since 2.1
4012 */
4013 public static boolean isEmpty( char[] array )
4014 {
4015 if ( array == null || array.length == 0 )
4016 {
4017 return true;
4018 }
4019 return false;
4020 }
4021
4022
4023 /**
4024 * <p>
4025 * Checks if an array of primitive bytes is empty or <code>null</code>.
4026 * </p>
4027 *
4028 * @param array
4029 * the array to test
4030 * @return <code>true</code> if the array is empty or <code>null</code>
4031 * @since 2.1
4032 */
4033 public static boolean isEmpty( byte[] array )
4034 {
4035 if ( array == null || array.length == 0 )
4036 {
4037 return true;
4038 }
4039 return false;
4040 }
4041
4042
4043 /**
4044 * <p>
4045 * Checks if an array of primitive doubles is empty or <code>null</code>.
4046 * </p>
4047 *
4048 * @param array
4049 * the array to test
4050 * @return <code>true</code> if the array is empty or <code>null</code>
4051 * @since 2.1
4052 */
4053 public static boolean isEmpty( double[] array )
4054 {
4055 if ( array == null || array.length == 0 )
4056 {
4057 return true;
4058 }
4059 return false;
4060 }
4061
4062
4063 /**
4064 * <p>
4065 * Checks if an array of primitive floats is empty or <code>null</code>.
4066 * </p>
4067 *
4068 * @param array
4069 * the array to test
4070 * @return <code>true</code> if the array is empty or <code>null</code>
4071 * @since 2.1
4072 */
4073 public static boolean isEmpty( float[] array )
4074 {
4075 if ( array == null || array.length == 0 )
4076 {
4077 return true;
4078 }
4079 return false;
4080 }
4081
4082
4083 /**
4084 * <p>
4085 * Checks if an array of primitive booleans is empty or <code>null</code>.
4086 * </p>
4087 *
4088 * @param array
4089 * the array to test
4090 * @return <code>true</code> if the array is empty or <code>null</code>
4091 * @since 2.1
4092 */
4093 public static boolean isEmpty( boolean[] array )
4094 {
4095 if ( array == null || array.length == 0 )
4096 {
4097 return true;
4098 }
4099 return false;
4100 }
4101
4102
4103 /**
4104 * <p>
4105 * Adds all the elements of the given arrays into a new array.
4106 * </p>
4107 * <p>
4108 * The new array contains all of the element of <code>array1</code>
4109 * followed by all of the elements <code>array2</code>. When an array is
4110 * returned, it is always a new array.
4111 * </p>
4112 *
4113 * <pre>
4114 * ArrayUtils.addAll(null, null) = null
4115 * ArrayUtils.addAll(array1, null) = cloned copy of array1
4116 * ArrayUtils.addAll(null, array2) = cloned copy of array2
4117 * ArrayUtils.addAll([], []) = []
4118 * ArrayUtils.addAll([null], [null]) = [null, null]
4119 * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
4120 * </pre>
4121 *
4122 * @param array1
4123 * the first array whose elements are added to the new array, may
4124 * be <code>null</code>
4125 * @param array2
4126 * the second array whose elements are added to the new array,
4127 * may be <code>null</code>
4128 * @return The new array, <code>null</code> if <code>null</code> array
4129 * inputs. The type of the new array is the type of the first array.
4130 * @since 2.1
4131 */
4132 public static Object[] addAll( Object[] array1, Object[] array2 )
4133 {
4134 if ( array1 == null )
4135 {
4136 return clone( array2 );
4137 }
4138 else if ( array2 == null )
4139 {
4140 return clone( array1 );
4141 }
4142 else
4143 {
4144 Object[] joinedArray = ( Object[] ) Array.newInstance( array1.getClass().getComponentType(), array1.length
4145 + array2.length );
4146 System.arraycopy( array1, 0, joinedArray, 0, array1.length );
4147 System.arraycopy( array2, 0, joinedArray, array1.length, array2.length );
4148 return joinedArray;
4149 }
4150 }
4151
4152
4153 /**
4154 * <p>
4155 * Copies the given array and adds the given element at the end of the new
4156 * array.
4157 * </p>
4158 * <p>
4159 * The new array contains the same elements of the input array plus the
4160 * given element in the last position. The component type of the new array
4161 * is the same as that of the input array.
4162 * </p>
4163 * <p>
4164 * If the input array is <code>null</code>, a new one element array is
4165 * returned whose component type is the same as the element.
4166 * </p>
4167 *
4168 * <pre>
4169 * ArrayUtils.add(null, null) = [null]
4170 * ArrayUtils.add(null, "a") = ["a"]
4171 * ArrayUtils.add(["a"], null) = ["a", null]
4172 * ArrayUtils.add(["a"], "b") = ["a", "b"]
4173 * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
4174 * </pre>
4175 *
4176 * @param array
4177 * the array to "add" the element to, may be <code>null</code>
4178 * @param element
4179 * the object to add
4180 * @return A new array containing the existing elements plus the new element
4181 * @since 2.1
4182 */
4183 public static Object[] add( Object[] array, Object element )
4184 {
4185 Object newArray = copyArrayGrow1( array, element != null ? element.getClass() : Object.class );
4186 Array.set( newArray, lastIndex( newArray ), element );
4187 return ( Object[] ) newArray;
4188 }
4189
4190
4191 /**
4192 * <p>
4193 * Copies the given array and adds the given element at the end of the new
4194 * array.
4195 * </p>
4196 * <p>
4197 * The new array contains the same elements of the input array plus the
4198 * given element in the last position. The component type of the new array
4199 * is the same as that of the input array.
4200 * </p>
4201 * <p>
4202 * If the input array is <code>null</code>, a new one element array is
4203 * returned whose component type is the same as the element.
4204 * </p>
4205 *
4206 * <pre>
4207 * ArrayUtils.add(null, true) = [true]
4208 * ArrayUtils.add([true], false) = [true, false]
4209 * ArrayUtils.add([true, false], true) = [true, false, true]
4210 * </pre>
4211 *
4212 * @param array
4213 * the array to copy and add the element to, may be
4214 * <code>null</code>
4215 * @param element
4216 * the object to add at the last index of the new array
4217 * @return A new array containing the existing elements plus the new element
4218 * @since 2.1
4219 */
4220 public static boolean[] add( boolean[] array, boolean element )
4221 {
4222 boolean[] newArray = ( boolean[] ) copyArrayGrow1( array, Boolean.TYPE );
4223 newArray[lastIndex( newArray )] = element;
4224 return newArray;
4225 }
4226
4227
4228 /**
4229 * <p>
4230 * Copies the given array and adds the given element at the end of the new
4231 * array.
4232 * </p>
4233 * <p>
4234 * The new array contains the same elements of the input array plus the
4235 * given element in the last position. The component type of the new array
4236 * is the same as that of the input array.
4237 * </p>
4238 * <p>
4239 * If the input array is <code>null</code>, a new one element array is
4240 * returned whose component type is the same as the element.
4241 * </p>
4242 *
4243 * <pre>
4244 * ArrayUtils.add(null, 0) = [0]
4245 * ArrayUtils.add([1], 0) = [1, 0]
4246 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4247 * </pre>
4248 *
4249 * @param array
4250 * the array to copy and add the element to, may be
4251 * <code>null</code>
4252 * @param element
4253 * the object to add at the last index of the new array
4254 * @return A new array containing the existing elements plus the new element
4255 * @since 2.1
4256 */
4257 public static byte[] add( byte[] array, byte element )
4258 {
4259 byte[] newArray = ( byte[] ) copyArrayGrow1( array, Byte.TYPE );
4260 newArray[lastIndex( newArray )] = element;
4261 return newArray;
4262 }
4263
4264
4265 /**
4266 * <p>
4267 * Copies the given array and adds the given element at the end of the new
4268 * array.
4269 * </p>
4270 * <p>
4271 * The new array contains the same elements of the input array plus the
4272 * given element in the last position. The component type of the new array
4273 * is the same as that of the input array.
4274 * </p>
4275 * <p>
4276 * If the input array is <code>null</code>, a new one element array is
4277 * returned whose component type is the same as the element.
4278 * </p>
4279 *
4280 * <pre>
4281 * ArrayUtils.add(null, '0') = ['0']
4282 * ArrayUtils.add(['1'], '0') = ['1', '0']
4283 * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
4284 * </pre>
4285 *
4286 * @param array
4287 * the array to copy and add the element to, may be
4288 * <code>null</code>
4289 * @param element
4290 * the object to add at the last index of the new array
4291 * @return A new array containing the existing elements plus the new element
4292 * @since 2.1
4293 */
4294 public static char[] add( char[] array, char element )
4295 {
4296 char[] newArray = ( char[] ) copyArrayGrow1( array, Character.TYPE );
4297 newArray[lastIndex( newArray )] = element;
4298 return newArray;
4299 }
4300
4301
4302 /**
4303 * <p>
4304 * Copies the given array and adds the given element at the end of the new
4305 * array.
4306 * </p>
4307 * <p>
4308 * The new array contains the same elements of the input array plus the
4309 * given element in the last position. The component type of the new array
4310 * is the same as that of the input array.
4311 * </p>
4312 * <p>
4313 * If the input array is <code>null</code>, a new one element array is
4314 * returned whose component type is the same as the element.
4315 * </p>
4316 *
4317 * <pre>
4318 * ArrayUtils.add(null, 0) = [0]
4319 * ArrayUtils.add([1], 0) = [1, 0]
4320 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4321 * </pre>
4322 *
4323 * @param array
4324 * the array to copy and add the element to, may be
4325 * <code>null</code>
4326 * @param element
4327 * the object to add at the last index of the new array
4328 * @return A new array containing the existing elements plus the new element
4329 * @since 2.1
4330 */
4331 public static double[] add( double[] array, double element )
4332 {
4333 double[] newArray = ( double[] ) copyArrayGrow1( array, Double.TYPE );
4334 newArray[lastIndex( newArray )] = element;
4335 return newArray;
4336 }
4337
4338
4339 /**
4340 * <p>
4341 * Copies the given array and adds the given element at the end of the new
4342 * array.
4343 * </p>
4344 * <p>
4345 * The new array contains the same elements of the input array plus the
4346 * given element in the last position. The component type of the new array
4347 * is the same as that of the input array.
4348 * </p>
4349 * <p>
4350 * If the input array is <code>null</code>, a new one element array is
4351 * returned whose component type is the same as the element.
4352 * </p>
4353 *
4354 * <pre>
4355 * ArrayUtils.add(null, 0) = [0]
4356 * ArrayUtils.add([1], 0) = [1, 0]
4357 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4358 * </pre>
4359 *
4360 * @param array
4361 * the array to copy and add the element to, may be
4362 * <code>null</code>
4363 * @param element
4364 * the object to add at the last index of the new array
4365 * @return A new array containing the existing elements plus the new element
4366 * @since 2.1
4367 */
4368 public static float[] add( float[] array, float element )
4369 {
4370 float[] newArray = ( float[] ) copyArrayGrow1( array, Float.TYPE );
4371 newArray[lastIndex( newArray )] = element;
4372 return newArray;
4373 }
4374
4375
4376 /**
4377 * <p>
4378 * Copies the given array and adds the given element at the end of the new
4379 * array.
4380 * </p>
4381 * <p>
4382 * The new array contains the same elements of the input array plus the
4383 * given element in the last position. The component type of the new array
4384 * is the same as that of the input array.
4385 * </p>
4386 * <p>
4387 * If the input array is <code>null</code>, a new one element array is
4388 * returned whose component type is the same as the element.
4389 * </p>
4390 *
4391 * <pre>
4392 * ArrayUtils.add(null, 0) = [0]
4393 * ArrayUtils.add([1], 0) = [1, 0]
4394 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4395 * </pre>
4396 *
4397 * @param array
4398 * the array to copy and add the element to, may be
4399 * <code>null</code>
4400 * @param element
4401 * the object to add at the last index of the new array
4402 * @return A new array containing the existing elements plus the new element
4403 * @since 2.1
4404 */
4405 public static int[] add( int[] array, int element )
4406 {
4407 int[] newArray = ( int[] ) copyArrayGrow1( array, Integer.TYPE );
4408 newArray[lastIndex( newArray )] = element;
4409 return newArray;
4410 }
4411
4412
4413 /**
4414 * <p>
4415 * Copies the given array and adds the given element at the end of the new
4416 * array.
4417 * </p>
4418 * <p>
4419 * The new array contains the same elements of the input array plus the
4420 * given element in the last position. The component type of the new array
4421 * is the same as that of the input array.
4422 * </p>
4423 * <p>
4424 * If the input array is <code>null</code>, a new one element array is
4425 * returned whose component type is the same as the element.
4426 * </p>
4427 *
4428 * <pre>
4429 * ArrayUtils.add(null, 0) = [0]
4430 * ArrayUtils.add([1], 0) = [1, 0]
4431 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4432 * </pre>
4433 *
4434 * @param array
4435 * the array to copy and add the element to, may be
4436 * <code>null</code>
4437 * @param element
4438 * the object to add at the last index of the new array
4439 * @return A new array containing the existing elements plus the new element
4440 * @since 2.1
4441 */
4442 public static long[] add( long[] array, long element )
4443 {
4444 long[] newArray = ( long[] ) copyArrayGrow1( array, Long.TYPE );
4445 newArray[lastIndex( newArray )] = element;
4446 return newArray;
4447 }
4448
4449
4450 /**
4451 * <p>
4452 * Copies the given array and adds the given element at the end of the new
4453 * array.
4454 * </p>
4455 * <p>
4456 * The new array contains the same elements of the input array plus the
4457 * given element in the last position. The component type of the new array
4458 * is the same as that of the input array.
4459 * </p>
4460 * <p>
4461 * If the input array is <code>null</code>, a new one element array is
4462 * returned whose component type is the same as the element.
4463 * </p>
4464 *
4465 * <pre>
4466 * ArrayUtils.add(null, 0) = [0]
4467 * ArrayUtils.add([1], 0) = [1, 0]
4468 * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
4469 * </pre>
4470 *
4471 * @param array
4472 * the array to copy and add the element to, may be
4473 * <code>null</code>
4474 * @param element
4475 * the object to add at the last index of the new array
4476 * @return A new array containing the existing elements plus the new element
4477 * @since 2.1
4478 */
4479 public static short[] add( short[] array, short element )
4480 {
4481 short[] newArray = ( short[] ) copyArrayGrow1( array, Short.TYPE );
4482 newArray[lastIndex( newArray )] = element;
4483 return newArray;
4484 }
4485
4486
4487 /**
4488 * Returns a copy of the given array of size 1 greater than the argument.
4489 * The last value of the array is left to the default value.
4490 *
4491 * @param array
4492 * The array to copy, must not be <code>null</code>.
4493 * @param newArrayComponentType
4494 * If <code>array</code> is <code>null</code>, create a size
4495 * 1 array of this type.
4496 * @return A new copy of the array of size 1 greater than the input.
4497 */
4498 private static Object copyArrayGrow1( Object array, Class<?> newArrayComponentType )
4499 {
4500 if ( array != null )
4501 {
4502 int arrayLength = Array.getLength( array );
4503 Object newArray = Array.newInstance( array.getClass().getComponentType(), arrayLength + 1 );
4504 System.arraycopy( array, 0, newArray, 0, arrayLength );
4505 return newArray;
4506 }
4507 else
4508 {
4509 return Array.newInstance( newArrayComponentType, 1 );
4510 }
4511 }
4512
4513
4514 /**
4515 * <p>
4516 * Inserts the specified element at the specified position in the array.
4517 * Shifts the element currently at that position (if any) and any subsequent
4518 * elements to the right (adds one to their indices).
4519 * </p>
4520 * <p>
4521 * This method returns a new array with the same elements of the input array
4522 * plus the given element on the specified position. The component type of
4523 * the returned array is always the same as that of the input array.
4524 * </p>
4525 * <p>
4526 * If the input array is <code>null</code>, a new one element array is
4527 * returned whose component type is the same as the element.
4528 * </p>
4529 *
4530 * <pre>
4531 * ArrayUtils.add(null, 0, null) = [null]
4532 * ArrayUtils.add(null, 0, "a") = ["a"]
4533 * ArrayUtils.add(["a"], 1, null) = ["a", null]
4534 * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
4535 * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
4536 * </pre>
4537 *
4538 * @param array
4539 * the array to add the element to, may be <code>null</code>
4540 * @param index
4541 * the position of the new object
4542 * @param element
4543 * the object to add
4544 * @return A new array containing the existing elements and the new element
4545 * @throws IndexOutOfBoundsException
4546 * if the index is out of range (index < 0 || index >
4547 * array.length).
4548 */
4549 public static Object[] add( Object[] array, int index, Object element )
4550 {
4551 if ( array == null )
4552 {
4553 if ( index != 0 )
4554 {
4555 throw new IndexOutOfBoundsException( "Index: " + index + ", Length: 0" );
4556 }
4557 Object joinedArray = Array.newInstance( element != null ? element.getClass() : Object.class, 1 );
4558 Array.set( joinedArray, 0, element );
4559 return ( Object[] ) joinedArray;
4560 }
4561 int length = array.length;
4562 if ( index > length || index < 0 )
4563 {
4564 throw new IndexOutOfBoundsException( "Index: " + index + ", Length: " + length );
4565 }
4566 Object result = Array.newInstance( array.getClass().getComponentType(), length + 1 );
4567 System.arraycopy( array, 0, result, 0, index );
4568 Array.set( result, index, element );
4569 if ( index < length )
4570 {
4571 System.arraycopy( array, index, result, index + 1, length - index );
4572 }
4573 return ( Object[] ) result;
4574 }
4575
4576
4577 /**
4578 * <p>
4579 * Removes the element at the specified position from the specified array.
4580 * All subsequent elements are shifted to the left (substracts one from
4581 * their indices).
4582 * </p>
4583 * <p>
4584 * This method returns a new array with the same elements of the input array
4585 * except the element on the specified position. The component type of the
4586 * returned array is always the same as that of the input array.
4587 * </p>
4588 * <p>
4589 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4590 * will be thrown, because in that case no valid index can be specified.
4591 * </p>
4592 *
4593 * <pre>
4594 * ArrayUtils.remove(["a"], 0) = []
4595 * ArrayUtils.remove(["a", "b"], 0) = ["b"]
4596 * ArrayUtils.remove(["a", "b"], 1) = ["a"]
4597 * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
4598 * </pre>
4599 *
4600 * @param array
4601 * the array to remove the element from, may not be
4602 * <code>null</code>
4603 * @param index
4604 * the position of the element to be removed
4605 * @return A new array containing the existing elements except the element
4606 * at the specified position.
4607 * @throws IndexOutOfBoundsException
4608 * if the index is out of range (index < 0 || index >=
4609 * array.length), or if the array is <code>null</code>.
4610 * @since 2.1
4611 */
4612 public static Object[] remove( Object[] array, int index )
4613 {
4614 return ( Object[] ) remove( ( Object ) array, index );
4615 }
4616
4617
4618 /**
4619 * <p>
4620 * Removes the first occurrence of the specified element from the specified
4621 * array. All subsequent elements are shifted to the left (substracts one
4622 * from their indices). If the array doesn't contains such an element, no
4623 * elements are removed from the array.
4624 * </p>
4625 * <p>
4626 * This method returns a new array with the same elements of the input array
4627 * except the first occurrence of the specified element. The component type
4628 * of the returned array is always the same as that of the input array.
4629 * </p>
4630 *
4631 * <pre>
4632 * ArrayUtils.removeElement(null, "a") = null
4633 * ArrayUtils.removeElement([], "a") = []
4634 * ArrayUtils.removeElement(["a"], "b") = ["a"]
4635 * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
4636 * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
4637 * </pre>
4638 *
4639 * @param array
4640 * the array to remove the element from, may be <code>null</code>
4641 * @param element
4642 * the element to be removed
4643 * @return A new array containing the existing elements except the first
4644 * occurrence of the specified element.
4645 * @since 2.1
4646 */
4647 public static Object[] removeElement( Object[] array, Object element )
4648 {
4649 int index = indexOf( array, element );
4650 if ( index == -1 )
4651 {
4652 return clone( array );
4653 }
4654 return remove( array, index );
4655 }
4656
4657
4658 /**
4659 * <p>
4660 * Removes the element at the specified position from the specified array.
4661 * All subsequent elements are shifted to the left (substracts one from
4662 * their indices).
4663 * </p>
4664 * <p>
4665 * This method returns a new array with the same elements of the input array
4666 * except the element on the specified position. The component type of the
4667 * returned array is always the same as that of the input array.
4668 * </p>
4669 * <p>
4670 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4671 * will be thrown, because in that case no valid index can be specified.
4672 * </p>
4673 *
4674 * <pre>
4675 * ArrayUtils.remove([true], 0) = []
4676 * ArrayUtils.remove([true, false], 0) = [false]
4677 * ArrayUtils.remove([true, false], 1) = [true]
4678 * ArrayUtils.remove([true, true, false], 1) = [true, false]
4679 * </pre>
4680 *
4681 * @param array
4682 * the array to remove the element from, may not be
4683 * <code>null</code>
4684 * @param index
4685 * the position of the element to be removed
4686 * @return A new array containing the existing elements except the element
4687 * at the specified position.
4688 * @throws IndexOutOfBoundsException
4689 * if the index is out of range (index < 0 || index >=
4690 * array.length), or if the array is <code>null</code>.
4691 * @since 2.1
4692 */
4693 public static boolean[] remove( boolean[] array, int index )
4694 {
4695 return ( boolean[] ) remove( ( Object ) array, index );
4696 }
4697
4698
4699 /**
4700 * <p>
4701 * Removes the first occurrence of the specified element from the specified
4702 * array. All subsequent elements are shifted to the left (substracts one
4703 * from their indices). If the array doesn't contains such an element, no
4704 * elements are removed from the array.
4705 * </p>
4706 * <p>
4707 * This method returns a new array with the same elements of the input array
4708 * except the first occurrence of the specified element. The component type
4709 * of the returned array is always the same as that of the input array.
4710 * </p>
4711 *
4712 * <pre>
4713 * ArrayUtils.removeElement(null, true) = null
4714 * ArrayUtils.removeElement([], true) = []
4715 * ArrayUtils.removeElement([true], false) = [true]
4716 * ArrayUtils.removeElement([true, false], false) = [true]
4717 * ArrayUtils.removeElement([true, false, true], true) = [false, true]
4718 * </pre>
4719 *
4720 * @param array
4721 * the array to remove the element from, may be <code>null</code>
4722 * @param element
4723 * the element to be removed
4724 * @return A new array containing the existing elements except the first
4725 * occurrence of the specified element.
4726 * @since 2.1
4727 */
4728 public static boolean[] removeElement( boolean[] array, boolean element )
4729 {
4730 int index = indexOf( array, element );
4731 if ( index == -1 )
4732 {
4733 return clone( array );
4734 }
4735 return remove( array, index );
4736 }
4737
4738
4739 /**
4740 * <p>
4741 * Removes the element at the specified position from the specified array.
4742 * All subsequent elements are shifted to the left (substracts one from
4743 * their indices).
4744 * </p>
4745 * <p>
4746 * This method returns a new array with the same elements of the input array
4747 * except the element on the specified position. The component type of the
4748 * returned array is always the same as that of the input array.
4749 * </p>
4750 * <p>
4751 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4752 * will be thrown, because in that case no valid index can be specified.
4753 * </p>
4754 *
4755 * <pre>
4756 * ArrayUtils.remove([1], 0) = []
4757 * ArrayUtils.remove([1, 0], 0) = [0]
4758 * ArrayUtils.remove([1, 0], 1) = [1]
4759 * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
4760 * </pre>
4761 *
4762 * @param array
4763 * the array to remove the element from, may not be
4764 * <code>null</code>
4765 * @param index
4766 * the position of the element to be removed
4767 * @return A new array containing the existing elements except the element
4768 * at the specified position.
4769 * @throws IndexOutOfBoundsException
4770 * if the index is out of range (index < 0 || index >=
4771 * array.length), or if the array is <code>null</code>.
4772 * @since 2.1
4773 */
4774 public static byte[] remove( byte[] array, int index )
4775 {
4776 return ( byte[] ) remove( ( Object ) array, index );
4777 }
4778
4779
4780 /**
4781 * <p>
4782 * Removes the first occurrence of the specified element from the specified
4783 * array. All subsequent elements are shifted to the left (substracts one
4784 * from their indices). If the array doesn't contains such an element, no
4785 * elements are removed from the array.
4786 * </p>
4787 * <p>
4788 * This method returns a new array with the same elements of the input array
4789 * except the first occurrence of the specified element. The component type
4790 * of the returned array is always the same as that of the input array.
4791 * </p>
4792 *
4793 * <pre>
4794 * ArrayUtils.removeElement(null, 1) = null
4795 * ArrayUtils.removeElement([], 1) = []
4796 * ArrayUtils.removeElement([1], 0) = [1]
4797 * ArrayUtils.removeElement([1, 0], 0) = [1]
4798 * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
4799 * </pre>
4800 *
4801 * @param array
4802 * the array to remove the element from, may be <code>null</code>
4803 * @param element
4804 * the element to be removed
4805 * @return A new array containing the existing elements except the first
4806 * occurrence of the specified element.
4807 * @since 2.1
4808 */
4809 public static byte[] removeElement( byte[] array, byte element )
4810 {
4811 int index = indexOf( array, element );
4812 if ( index == -1 )
4813 {
4814 return clone( array );
4815 }
4816 return remove( array, index );
4817 }
4818
4819
4820 /**
4821 * <p>
4822 * Removes the element at the specified position from the specified array.
4823 * All subsequent elements are shifted to the left (substracts one from
4824 * their indices).
4825 * </p>
4826 * <p>
4827 * This method returns a new array with the same elements of the input array
4828 * except the element on the specified position. The component type of the
4829 * returned array is always the same as that of the input array.
4830 * </p>
4831 * <p>
4832 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4833 * will be thrown, because in that case no valid index can be specified.
4834 * </p>
4835 *
4836 * <pre>
4837 * ArrayUtils.remove(['a'], 0) = []
4838 * ArrayUtils.remove(['a', 'b'], 0) = ['b']
4839 * ArrayUtils.remove(['a', 'b'], 1) = ['a']
4840 * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
4841 * </pre>
4842 *
4843 * @param array
4844 * the array to remove the element from, may not be
4845 * <code>null</code>
4846 * @param index
4847 * the position of the element to be removed
4848 * @return A new array containing the existing elements except the element
4849 * at the specified position.
4850 * @throws IndexOutOfBoundsException
4851 * if the index is out of range (index < 0 || index >=
4852 * array.length), or if the array is <code>null</code>.
4853 * @since 2.1
4854 */
4855 public static char[] remove( char[] array, int index )
4856 {
4857 return ( char[] ) remove( ( Object ) array, index );
4858 }
4859
4860
4861 /**
4862 * <p>
4863 * Removes the first occurrence of the specified element from the specified
4864 * array. All subsequent elements are shifted to the left (substracts one
4865 * from their indices). If the array doesn't contains such an element, no
4866 * elements are removed from the array.
4867 * </p>
4868 * <p>
4869 * This method returns a new array with the same elements of the input array
4870 * except the first occurrence of the specified element. The component type
4871 * of the returned array is always the same as that of the input array.
4872 * </p>
4873 *
4874 * <pre>
4875 * ArrayUtils.removeElement(null, 'a') = null
4876 * ArrayUtils.removeElement([], 'a') = []
4877 * ArrayUtils.removeElement(['a'], 'b') = ['a']
4878 * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
4879 * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
4880 * </pre>
4881 *
4882 * @param array
4883 * the array to remove the element from, may be <code>null</code>
4884 * @param element
4885 * the element to be removed
4886 * @return A new array containing the existing elements except the first
4887 * occurrence of the specified element.
4888 * @since 2.1
4889 */
4890 public static char[] removeElement( char[] array, char element )
4891 {
4892 int index = indexOf( array, element );
4893 if ( index == -1 )
4894 {
4895 return clone( array );
4896 }
4897 return remove( array, index );
4898 }
4899
4900
4901 /**
4902 * <p>
4903 * Removes the element at the specified position from the specified array.
4904 * All subsequent elements are shifted to the left (substracts one from
4905 * their indices).
4906 * </p>
4907 * <p>
4908 * This method returns a new array with the same elements of the input array
4909 * except the element on the specified position. The component type of the
4910 * returned array is always the same as that of the input array.
4911 * </p>
4912 * <p>
4913 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4914 * will be thrown, because in that case no valid index can be specified.
4915 * </p>
4916 *
4917 * <pre>
4918 * ArrayUtils.remove([1.1], 0) = []
4919 * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
4920 * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
4921 * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
4922 * </pre>
4923 *
4924 * @param array
4925 * the array to remove the element from, may not be
4926 * <code>null</code>
4927 * @param index
4928 * the position of the element to be removed
4929 * @return A new array containing the existing elements except the element
4930 * at the specified position.
4931 * @throws IndexOutOfBoundsException
4932 * if the index is out of range (index < 0 || index >=
4933 * array.length), or if the array is <code>null</code>.
4934 * @since 2.1
4935 */
4936 public static double[] remove( double[] array, int index )
4937 {
4938 return ( double[] ) remove( ( Object ) array, index );
4939 }
4940
4941
4942 /**
4943 * <p>
4944 * Removes the first occurrence of the specified element from the specified
4945 * array. All subsequent elements are shifted to the left (substracts one
4946 * from their indices). If the array doesn't contains such an element, no
4947 * elements are removed from the array.
4948 * </p>
4949 * <p>
4950 * This method returns a new array with the same elements of the input array
4951 * except the first occurrence of the specified element. The component type
4952 * of the returned array is always the same as that of the input array.
4953 * </p>
4954 *
4955 * <pre>
4956 * ArrayUtils.removeElement(null, 1.1) = null
4957 * ArrayUtils.removeElement([], 1.1) = []
4958 * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
4959 * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
4960 * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
4961 * </pre>
4962 *
4963 * @param array
4964 * the array to remove the element from, may be <code>null</code>
4965 * @param element
4966 * the element to be removed
4967 * @return A new array containing the existing elements except the first
4968 * occurrence of the specified element.
4969 * @since 2.1
4970 */
4971 public static double[] removeElement( double[] array, double element )
4972 {
4973 int index = indexOf( array, element );
4974 if ( index == -1 )
4975 {
4976 return clone( array );
4977 }
4978 return remove( array, index );
4979 }
4980
4981
4982 /**
4983 * <p>
4984 * Removes the element at the specified position from the specified array.
4985 * All subsequent elements are shifted to the left (substracts one from
4986 * their indices).
4987 * </p>
4988 * <p>
4989 * This method returns a new array with the same elements of the input array
4990 * except the element on the specified position. The component type of the
4991 * returned array is always the same as that of the input array.
4992 * </p>
4993 * <p>
4994 * If the input array is <code>null</code>, an IndexOutOfBoundsException
4995 * will be thrown, because in that case no valid index can be specified.
4996 * </p>
4997 *
4998 * <pre>
4999 * ArrayUtils.remove([1.1], 0) = []
5000 * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
5001 * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
5002 * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
5003 * </pre>
5004 *
5005 * @param array
5006 * the array to remove the element from, may not be
5007 * <code>null</code>
5008 * @param index
5009 * the position of the element to be removed
5010 * @return A new array containing the existing elements except the element
5011 * at the specified position.
5012 * @throws IndexOutOfBoundsException
5013 * if the index is out of range (index < 0 || index >=
5014 * array.length), or if the array is <code>null</code>.
5015 * @since 2.1
5016 */
5017 public static float[] remove( float[] array, int index )
5018 {
5019 return ( float[] ) remove( ( Object ) array, index );
5020 }
5021
5022
5023 /**
5024 * <p>
5025 * Removes the first occurrence of the specified element from the specified
5026 * array. All subsequent elements are shifted to the left (substracts one
5027 * from their indices). If the array doesn't contains such an element, no
5028 * elements are removed from the array.
5029 * </p>
5030 * <p>
5031 * This method returns a new array with the same elements of the input array
5032 * except the first occurrence of the specified element. The component type
5033 * of the returned array is always the same as that of the input array.
5034 * </p>
5035 *
5036 * <pre>
5037 * ArrayUtils.removeElement(null, 1.1) = null
5038 * ArrayUtils.removeElement([], 1.1) = []
5039 * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
5040 * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
5041 * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
5042 * </pre>
5043 *
5044 * @param array
5045 * the array to remove the element from, may be <code>null</code>
5046 * @param element
5047 * the element to be removed
5048 * @return A new array containing the existing elements except the first
5049 * occurrence of the specified element.
5050 * @since 2.1
5051 */
5052 public static float[] removeElement( float[] array, float element )
5053 {
5054 int index = indexOf( array, element );
5055 if ( index == -1 )
5056 {
5057 return clone( array );
5058 }
5059 return remove( array, index );
5060 }
5061
5062
5063 /**
5064 * <p>
5065 * Removes the element at the specified position from the specified array.
5066 * All subsequent elements are shifted to the left (substracts one from
5067 * their indices).
5068 * </p>
5069 * <p>
5070 * This method returns a new array with the same elements of the input array
5071 * except the element on the specified position. The component type of the
5072 * returned array is always the same as that of the input array.
5073 * </p>
5074 * <p>
5075 * If the input array is <code>null</code>, an IndexOutOfBoundsException
5076 * will be thrown, because in that case no valid index can be specified.
5077 * </p>
5078 *
5079 * <pre>
5080 * ArrayUtils.remove([1], 0) = []
5081 * ArrayUtils.remove([2, 6], 0) = [6]
5082 * ArrayUtils.remove([2, 6], 1) = [2]
5083 * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
5084 * </pre>
5085 *
5086 * @param array
5087 * the array to remove the element from, may not be
5088 * <code>null</code>
5089 * @param index
5090 * the position of the element to be removed
5091 * @return A new array containing the existing elements except the element
5092 * at the specified position.
5093 * @throws IndexOutOfBoundsException
5094 * if the index is out of range (index < 0 || index >=
5095 * array.length), or if the array is <code>null</code>.
5096 * @since 2.1
5097 */
5098 public static int[] remove( int[] array, int index )
5099 {
5100 return ( int[] ) remove( ( Object ) array, index );
5101 }
5102
5103
5104 /**
5105 * <p>
5106 * Removes the first occurrence of the specified element from the specified
5107 * array. All subsequent elements are shifted to the left (substracts one
5108 * from their indices). If the array doesn't contains such an element, no
5109 * elements are removed from the array.
5110 * </p>
5111 * <p>
5112 * This method returns a new array with the same elements of the input array
5113 * except the first occurrence of the specified element. The component type
5114 * of the returned array is always the same as that of the input array.
5115 * </p>
5116 *
5117 * <pre>
5118 * ArrayUtils.removeElement(null, 1) = null
5119 * ArrayUtils.removeElement([], 1) = []
5120 * ArrayUtils.removeElement([1], 2) = [1]
5121 * ArrayUtils.removeElement([1, 3], 1) = [3]
5122 * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
5123 * </pre>
5124 *
5125 * @param array
5126 * the array to remove the element from, may be <code>null</code>
5127 * @param element
5128 * the element to be removed
5129 * @return A new array containing the existing elements except the first
5130 * occurrence of the specified element.
5131 * @since 2.1
5132 */
5133 public static int[] removeElement( int[] array, int element )
5134 {
5135 int index = indexOf( array, element );
5136 if ( index == -1 )
5137 {
5138 return clone( array );
5139 }
5140 return remove( array, index );
5141 }
5142
5143
5144 /**
5145 * <p>
5146 * Removes the element at the specified position from the specified array.
5147 * All subsequent elements are shifted to the left (substracts one from
5148 * their indices).
5149 * </p>
5150 * <p>
5151 * This method returns a new array with the same elements of the input array
5152 * except the element on the specified position. The component type of the
5153 * returned array is always the same as that of the input array.
5154 * </p>
5155 * <p>
5156 * If the input array is <code>null</code>, an IndexOutOfBoundsException
5157 * will be thrown, because in that case no valid index can be specified.
5158 * </p>
5159 *
5160 * <pre>
5161 * ArrayUtils.remove([1], 0) = []
5162 * ArrayUtils.remove([2, 6], 0) = [6]
5163 * ArrayUtils.remove([2, 6], 1) = [2]
5164 * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
5165 * </pre>
5166 *
5167 * @param array
5168 * the array to remove the element from, may not be
5169 * <code>null</code>
5170 * @param index
5171 * the position of the element to be removed
5172 * @return A new array containing the existing elements except the element
5173 * at the specified position.
5174 * @throws IndexOutOfBoundsException
5175 * if the index is out of range (index < 0 || index >=
5176 * array.length), or if the array is <code>null</code>.
5177 * @since 2.1
5178 */
5179 public static long[] remove( long[] array, int index )
5180 {
5181 return ( long[] ) remove( ( Object ) array, index );
5182 }
5183
5184
5185 /**
5186 * <p>
5187 * Removes the first occurrence of the specified element from the specified
5188 * array. All subsequent elements are shifted to the left (substracts one
5189 * from their indices). If the array doesn't contains such an element, no
5190 * elements are removed from the array.
5191 * </p>
5192 * <p>
5193 * This method returns a new array with the same elements of the input array
5194 * except the first occurrence of the specified element. The component type
5195 * of the returned array is always the same as that of the input array.
5196 * </p>
5197 *
5198 * <pre>
5199 * ArrayUtils.removeElement(null, 1) = null
5200 * ArrayUtils.removeElement([], 1) = []
5201 * ArrayUtils.removeElement([1], 2) = [1]
5202 * ArrayUtils.removeElement([1, 3], 1) = [3]
5203 * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
5204 * </pre>
5205 *
5206 * @param array
5207 * the array to remove the element from, may be <code>null</code>
5208 * @param element
5209 * the element to be removed
5210 * @return A new array containing the existing elements except the first
5211 * occurrence of the specified element.
5212 * @since 2.1
5213 */
5214 public static long[] removeElement( long[] array, long element )
5215 {
5216 int index = indexOf( array, element );
5217 if ( index == -1 )
5218 {
5219 return clone( array );
5220 }
5221 return remove( array, index );
5222 }
5223
5224
5225 /**
5226 * <p>
5227 * Removes the element at the specified position from the specified array.
5228 * All subsequent elements are shifted to the left (substracts one from
5229 * their indices).
5230 * </p>
5231 * <p>
5232 * This method returns a new array with the same elements of the input array
5233 * except the element on the specified position. The component type of the
5234 * returned array is always the same as that of the input array.
5235 * </p>
5236 * <p>
5237 * If the input array is <code>null</code>, an IndexOutOfBoundsException
5238 * will be thrown, because in that case no valid index can be specified.
5239 * </p>
5240 *
5241 * <pre>
5242 * ArrayUtils.remove([1], 0) = []
5243 * ArrayUtils.remove([2, 6], 0) = [6]
5244 * ArrayUtils.remove([2, 6], 1) = [2]
5245 * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
5246 * </pre>
5247 *
5248 * @param array
5249 * the array to remove the element from, may not be
5250 * <code>null</code>
5251 * @param index
5252 * the position of the element to be removed
5253 * @return A new array containing the existing elements except the element
5254 * at the specified position.
5255 * @throws IndexOutOfBoundsException
5256 * if the index is out of range (index < 0 || index >=
5257 * array.length), or if the array is <code>null</code>.
5258 * @since 2.1
5259 */
5260 public static short[] remove( short[] array, int index )
5261 {
5262 return ( short[] ) remove( ( Object ) array, index );
5263 }
5264
5265
5266 /**
5267 * <p>
5268 * Removes the first occurrence of the specified element from the specified
5269 * array. All subsequent elements are shifted to the left (substracts one
5270 * from their indices). If the array doesn't contains such an element, no
5271 * elements are removed from the array.
5272 * </p>
5273 * <p>
5274 * This method returns a new array with the same elements of the input array
5275 * except the first occurrence of the specified element. The component type
5276 * of the returned array is always the same as that of the input array.
5277 * </p>
5278 *
5279 * <pre>
5280 * ArrayUtils.removeElement(null, 1) = null
5281 * ArrayUtils.removeElement([], 1) = []
5282 * ArrayUtils.removeElement([1], 2) = [1]
5283 * ArrayUtils.removeElement([1, 3], 1) = [3]
5284 * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
5285 * </pre>
5286 *
5287 * @param array
5288 * the array to remove the element from, may be <code>null</code>
5289 * @param element
5290 * the element to be removed
5291 * @return A new array containing the existing elements except the first
5292 * occurrence of the specified element.
5293 * @since 2.1
5294 */
5295 public static short[] removeElement( short[] array, short element )
5296 {
5297 int index = indexOf( array, element );
5298 if ( index == -1 )
5299 {
5300 return clone( array );
5301 }
5302 return remove( array, index );
5303 }
5304
5305
5306 /**
5307 * <p>
5308 * Removes the element at the specified position from the specified array.
5309 * All subsequent elements are shifted to the left (substracts one from
5310 * their indices).
5311 * </p>
5312 * <p>
5313 * This method returns a new array with the same elements of the input array
5314 * except the element on the specified position. The component type of the
5315 * returned array is always the same as that of the input array.
5316 * </p>
5317 * <p>
5318 * If the input array is <code>null</code>, an IndexOutOfBoundsException
5319 * will be thrown, because in that case no valid index can be specified.
5320 * </p>
5321 *
5322 * @param array
5323 * the array to remove the element from, may not be
5324 * <code>null</code>
5325 * @param index
5326 * the position of the element to be removed
5327 * @return A new array containing the existing elements except the element
5328 * at the specified position.
5329 * @throws IndexOutOfBoundsException
5330 * if the index is out of range (index < 0 || index >=
5331 * array.length), or if the array is <code>null</code>.
5332 * @since 2.1
5333 */
5334 private static Object remove( Object array, int index )
5335 {
5336 int length = getLength( array );
5337 if ( index < 0 || index >= length )
5338 {
5339 throw new IndexOutOfBoundsException( "Index: " + index + ", Length: " + length );
5340 }
5341
5342 Object result = Array.newInstance( array.getClass().getComponentType(), length - 1 );
5343 System.arraycopy( array, 0, result, 0, index );
5344 if ( index < length - 1 )
5345 {
5346 System.arraycopy( array, index + 1, result, index, length - index - 1 );
5347 }
5348
5349 return result;
5350 }
5351
5352 }