From 90ecd2e1e7680eceb11621e718fd4fbcf4acc2a7 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Fri, 30 Jan 2004 02:12:22 +0000 Subject: [PATCH] Sort members (Eclipse defaults). git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137764 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ArrayUtils.java | 4204 ++++++++--------- 1 file changed, 2102 insertions(+), 2102 deletions(-) diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index 06b256721..e39fea849 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -81,46 +81,17 @@ * @author Gary Gregory * @author Ashwin S * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.36 2004/01/30 01:51:36 ggregory Exp $ + * @version $Id: ArrayUtils.java,v 1.37 2004/01/30 02:12:22 ggregory Exp $ */ public class ArrayUtils { - /** - * An empty immutable Object array. + * An empty immutable boolean array. */ - public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; /** - * An empty immutable Class array. + * An empty immutable Boolean array. */ - public static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; - /** - * An empty immutable String array. - */ - public static final String[] EMPTY_STRING_ARRAY = new String[0]; - /** - * An empty immutable long array. - */ - public static final long[] EMPTY_LONG_ARRAY = new long[0]; - /** - * An empty immutable Long array. - */ - public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; - /** - * An empty immutable int array. - */ - public static final int[] EMPTY_INT_ARRAY = new int[0]; - /** - * An empty immutable Integer array. - */ - public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0]; - /** - * An empty immutable short array. - */ - public static final short[] EMPTY_SHORT_ARRAY = new short[0]; - /** - * An empty immutable Short array. - */ - public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; + public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0]; /** * An empty immutable byte array. */ @@ -129,6 +100,18 @@ public class ArrayUtils { * An empty immutable Byte array. */ public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0]; + /** + * An empty immutable char array. + */ + public static final char[] EMPTY_CHAR_ARRAY = new char[0]; + /** + * An empty immutable Character array. + */ + public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0]; + /** + * An empty immutable Class array. + */ + public static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; /** * An empty immutable double array. */ @@ -146,167 +129,126 @@ public class ArrayUtils { */ public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0]; /** - * An empty immutable boolean array. + * An empty immutable int array. */ - public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; + public static final int[] EMPTY_INT_ARRAY = new int[0]; /** - * An empty immutable Boolean array. + * An empty immutable Integer array. */ - public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0]; + public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0]; /** - * An empty immutable char array. + * An empty immutable long array. */ - public static final char[] EMPTY_CHAR_ARRAY = new char[0]; + public static final long[] EMPTY_LONG_ARRAY = new long[0]; /** - * An empty immutable Character array. + * An empty immutable Long array. */ - public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0]; + public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; /** - *

ArrayUtils instances should NOT be constructed in standard programming. - * Instead, the class should be used as ArrayUtils.clone(new int[] {2}).

- * - *

This constructor is public to permit tools that require a JavaBean instance - * to operate.

+ * An empty immutable Object array. */ - public ArrayUtils() { - } - - // Basic methods handling multi-dimensional arrays - //----------------------------------------------------------------------- + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; /** - *

Outputs an array as a String, treating null as an empty array.

- * - *

Multi-dimensional arrays are handled correctly, including - * multi-dimensional primitive arrays.

- * - *

The format is that of Java source code, for example {a,b}.

- * - * @param array the array to get a toString for, may be null - * @return a String representation of the array, '{}' if null array input + * An empty immutable short array. */ - public static String toString(final Object array) { - return toString(array, "{}"); - } + public static final short[] EMPTY_SHORT_ARRAY = new short[0]; + /** + * An empty immutable Short array. + */ + public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; + /** + * An empty immutable String array. + */ + public static final String[] EMPTY_STRING_ARRAY = new String[0]; /** - *

Outputs an array as a String handling nulls.

+ *

Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices).

* - *

Multi-dimensional arrays are handled correctly, including - * multi-dimensional primitive arrays.

+ *

This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array.

* - *

The format is that of Java source code, for example {a,b}.

+ *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

* - * @param array the array to get a toString for, may be null - * @param stringIfNull the String to return if the array is null - * @return a String representation of the array - */ - public static String toString(final Object array, final String stringIfNull) { - if (array == null) { - return stringIfNull; - } - return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString(); - } - - /** - *

Get a hashCode for an array handling multi-dimensional arrays correctly.

- * - *

Multi-dimensional primitive arrays are also handled correctly by this method.

- * - * @param array the array to get a hashCode for, may be null - * @return a hashCode for the array, zero if null array input - */ - public static int hashCode(final Object array) { - return new HashCodeBuilder().append(array).toHashCode(); - } - - /** - *

Compares two arrays, using equals(), handling multi-dimensional arrays - * correctly.

- * - *

Multi-dimensional primitive arrays are also handled correctly by this method.

- * - * @param array1 the left hand array to compare, may be null - * @param array2 the right hand array to compare, may be null - * @return true if the arrays are equal - */ - public static boolean isEquals(final Object array1, final Object array2) { - return new EqualsBuilder().append(array1, array2).isEquals(); - } - - // To map - //----------------------------------------------------------------------- - /** - *

Converts the given array into a {@link java.util.Map}. Each element of the array - * must be either a {@link java.util.Map.Entry} or an Array, containing at least two - * elements, where the first element is used as key and the second as - * value.

- * - *

This method can be used to initialize:

*
-     * // Create a Map mapping colors.
-     * Map colorMap = MapUtils.toMap(new String[][] {{
-     *     {"RED", "#FF0000"},
-     *     {"GREEN", "#00FF00"},
-     *     {"BLUE", "#0000FF"}});
+     * ArrayUtils.add(null, 0, null)      = [null]
+     * ArrayUtils.add(null, 0, "a")       = ["a"]
+     * ArrayUtils.add(["a"], 1, null)     = ["a", null]
+     * ArrayUtils.add(["a"], 1, "b")      = ["a", "b"]
+     * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
      * 
* - *

This method returns null if null array input.

- * - * @param array an array whose elements are either a {@link java.util.Map.Entry} or - * an Array containing at least two elements, may be null - * @return a Map that was created from the array - * @throws IllegalArgumentException if one element of this Array is - * itself an Array containing less then two elements - * @throws IllegalArgumentException if the array contains elements other - * than {@link java.util.Map.Entry} and an Array + * @param array the array to add the element to, may be null + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). */ - public static Map toMap(final Object[] array) { + public static Object[] add(final Object[] array, final int index, final Object element) { if (array == null) { - return null; - } - final Map map = new HashMap((int) (array.length * 1.5)); - for (int i = 0; i < array.length; i++) { - Object object = array[i]; - if (object instanceof Map.Entry) { - Map.Entry entry = (Map.Entry) object; - map.put(entry.getKey(), entry.getValue()); - } else if (object instanceof Object[]) { - Object[] entry = (Object[]) object; - if (entry.length < 2) { - throw new IllegalArgumentException("Array element " + i + ", '" - + object - + "', has a length less than 2"); - } - map.put(entry[0], entry[1]); - } else { - throw new IllegalArgumentException("Array element " + i + ", '" - + object - + "', is neither of type Map.Entry nor an Array"); + if (index != 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0"); } + Object joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1); + Array.set(joinedArray, 0, element); + return (Object[]) joinedArray; } - return map; + int length = array.length; + if (index > length || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); + } + Object result = Array.newInstance(array.getClass().getComponentType(), length + 1); + System.arraycopy(array, 0, result, 0, index); + Array.set(result, index, element); + if (index < length) { + System.arraycopy(array, index, result, index + 1, length - index); + } + return (Object[]) result; } - // Clone - //----------------------------------------------------------------------- /** - *

Shallow clones an array returning a typecast result and handling - * null.

+ *

Adds the element to the end of the array.

* - *

The objects in the array are not cloned, thus there is no special - * handling for multi-dimensional arrays.

+ *

The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array.

+ * + *

If the input array is null, a new one element array is returned + * whose component type is the same as the element.

* - *

This method returns null if null array input.

+ *
+     * ArrayUtils.add(null, null)      = [null]
+     * ArrayUtils.add(null, "a")       = ["a"]
+     * ArrayUtils.add(["a"], null)     = ["a", null]
+     * ArrayUtils.add(["a"], "b")      = ["a", "b"]
+     * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
+     * 
* - * @param array the array to shallow clone, may be null - * @return the cloned array, null if null input + * @param array the array to "add" the element to, may be null + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @since 2.1 */ - public static Object[] clone(final Object[] array) { - if (array == null) { - return null; + public static Object[] add(Object[] array, Object element) { + Object joinedArray; + int elementPos; + if (array != null) { + joinedArray = Array.newInstance(array.getClass().getComponentType(), array.length + 1); + System.arraycopy(array, 0, joinedArray, 0, array.length); + elementPos = array.length; + } else { + // null input array, use the element type + joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1); + elementPos = 0; } - return (Object[]) array.clone(); + Array.set(joinedArray, elementPos, element); + return (Object[]) joinedArray; } /** @@ -318,59 +260,11 @@ public static Object[] clone(final Object[] array) { * @param array the array to clone, may be null * @return the cloned array, null if null input */ - public static long[] clone(final long[] array) { + public static boolean[] clone(final boolean[] array) { if (array == null) { return null; } - return (long[]) array.clone(); - } - - /** - *

Clones an array returning a typecast result and handling - * null.

- * - *

This method returns null if null array input.

- * - * @param array the array to clone, may be null - * @return the cloned array, null if null input - */ - public static int[] clone(int[] array) { - if (array == null) { - return null; - } - return (int[]) array.clone(); - } - - /** - *

Clones an array returning a typecast result and handling - * null.

- * - *

This method returns null if null array input.

- * - * @param array the array to clone, may be null - * @return the cloned array, null if null input - */ - public static short[] clone(final short[] array) { - if (array == null) { - return null; - } - return (short[]) array.clone(); - } - - /** - *

Clones an array returning a typecast result and handling - * null.

- * - *

This method returns null if null array input.

- * - * @param array the array to clone, may be null - * @return the cloned array, null if null input - */ - public static char[] clone(final char[] array) { - if (array == null) { - return null; - } - return (char[]) array.clone(); + return (boolean[]) array.clone(); } /** @@ -389,6 +283,22 @@ public static byte[] clone(final byte[] array) { return (byte[]) array.clone(); } + /** + *

Clones an array returning a typecast result and handling + * null.

+ * + *

This method returns null if null array input.

+ * + * @param array the array to clone, may be null + * @return the cloned array, null if null input + */ + public static char[] clone(final char[] array) { + if (array == null) { + return null; + } + return (char[]) array.clone(); + } + /** *

Clones an array returning a typecast result and handling * null.

@@ -430,968 +340,64 @@ public static float[] clone(final float[] array) { * @param array the array to clone, may be null * @return the cloned array, null if null input */ - public static boolean[] clone(final boolean[] array) { + public static int[] clone(int[] array) { if (array == null) { return null; } - return (boolean[]) array.clone(); + return (int[]) array.clone(); } - // Subarrays + /** + *

Clones an array returning a typecast result and handling + * null.

+ * + *

This method returns null if null array input.

+ * + * @param array the array to clone, may be null + * @return the cloned array, null if null input + */ + public static long[] clone(final long[] array) { + if (array == null) { + return null; + } + return (long[]) array.clone(); + } + + // Clone //----------------------------------------------------------------------- /** - *

Produces a new array containing the elements between - * the start and end indices.

+ *

Shallow clones an array returning a typecast result and handling + * null.

* - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - *

The component type of the subarray is always the same as - * that of the input array. Thus, if the input is an array of type - * Date, the following usage is envisaged:

- * - *
-     * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
-     * 
- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. + *

The objects in the array are not cloned, thus there is no special + * handling for multi-dimensional arrays.

+ * + *

This method returns null if null array input.

+ * + * @param array the array to shallow clone, may be null + * @return the cloned array, null if null input */ - public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { + public static Object[] clone(final Object[] array) { if (array == null) { return null; } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - Class type = array.getClass().getComponentType(); - if (newSize <= 0) { - return (Object[]) Array.newInstance(type, 0); - } - Object[] subarray = (Object[]) Array.newInstance(type, newSize); - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; + return (Object[]) array.clone(); } /** - *

Produces a new long array containing the elements - * between the start and end indices.

+ *

Clones an array returning a typecast result and handling + * null.

* - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. + *

This method returns null if null array input.

+ * + * @param array the array to clone, may be null + * @return the cloned array, null if null input */ - public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) { + public static short[] clone(final short[] array) { if (array == null) { return null; } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_LONG_ARRAY; - } - - long[] subarray = new long[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new int array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_INT_ARRAY; - } - - int[] subarray = new int[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new short array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_SHORT_ARRAY; - } - - short[] subarray = new short[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new char array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_CHAR_ARRAY; - } - - char[] subarray = new char[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new byte array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_BYTE_ARRAY; - } - - byte[] subarray = new byte[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new double array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_DOUBLE_ARRAY; - } - - double[] subarray = new double[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new float array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_FLOAT_ARRAY; - } - - float[] subarray = new float[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - /** - *

Produces a new boolean array containing the elements - * between the start and end indices.

- * - *

The start index is inclusive, the end index exclusive. - * Null array input produces null output.

- * - * @param array the array - * @param startIndexInclusive the starting index. Undervalue (<0) - * is promoted to 0, overvalue (>array.length) results - * in an empty array. - * @param endIndexExclusive elements up to endIndex-1 are present in the - * returned subarray. Undervalue (< startIndex) produces - * empty array, overvalue (>array.length) is demoted to - * array length. - * @return a new array containing the elements between - * the start and end indices. - */ - public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) { - if (array == null) { - return null; - } - if (startIndexInclusive < 0) { - startIndexInclusive = 0; - } - if (endIndexExclusive > array.length) { - endIndexExclusive = array.length; - } - int newSize = endIndexExclusive - startIndexInclusive; - if (newSize <= 0) { - return EMPTY_BOOLEAN_ARRAY; - } - - boolean[] subarray = new boolean[newSize]; - System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); - return subarray; - } - - // Is same length - //----------------------------------------------------------------------- - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0. - * - *

Any multi-dimensional aspects of the arrays are ignored.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final Object[] array1, final Object[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final long[] array1, final long[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final int[] array1, final int[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final short[] array1, final short[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final char[] array1, final char[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final byte[] array1, final byte[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final double[] array1, final double[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final float[] array1, final float[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same length, treating - * null arrays as length 0.

- * - * @param array1 the first array, may be null - * @param array2 the second array, may be null - * @return true if length of arrays matches, treating - * null as an empty array - */ - public static boolean isSameLength(final boolean[] array1, final boolean[] array2) { - if ((array1 == null && array2 != null && array2.length > 0) || - (array2 == null && array1 != null && array1.length > 0) || - (array1 != null && array2 != null && array1.length != array2.length)) { - return false; - } - return true; - } - - /** - *

Checks whether two arrays are the same type taking into account - * multi-dimensional arrays.

- * - * @param array1 the first array, must not be null - * @param array2 the second array, must not be null - * @return true if type of arrays matches - * @throws IllegalArgumentException if either array is null - */ - public static boolean isSameType(final Object array1, final Object array2) { - if (array1 == null || array2 == null) { - throw new IllegalArgumentException("The Array must not be null"); - } - return array1.getClass().getName().equals(array2.getClass().getName()); - } - - // Reverse - //----------------------------------------------------------------------- - /** - *

Reverses the order of the given array.

- * - *

There is no special handling for multi-dimensional arrays.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final Object[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - Object tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final long[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - long tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final int[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - int tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final short[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - short tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final char[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - char tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final byte[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - byte tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final double[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - double tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final float[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - float tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - /** - *

Reverses the order of the given array.

- * - *

This method does nothing if null array input.

- * - * @param array the array to reverse, may be null - */ - public static void reverse(final boolean[] array) { - if (array == null) { - return; - } - int i = 0; - int j = array.length - 1; - boolean tmp; - while (j > i) { - tmp = array[j]; - array[j] = array[i]; - array[i] = tmp; - j--; - i++; - } - } - - // IndexOf search - // ---------------------------------------------------------------------- - - // Object IndexOf - //----------------------------------------------------------------------- - /** - *

Find the index of the given object in the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to search through for the object, may be null - * @param objectToFind the object to find, may be null - * @return the index of the object within the array, - * -1 if not found or null array input - */ - public static int indexOf(final Object[] array, final Object objectToFind) { - return indexOf(array, objectToFind, 0); - } - - /** - *

Find the index of the given object in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return -1.

- * - * @param array the array to search through for the object, may be null - * @param objectToFind the object to find, may be null - * @param startIndex the index to start searching at - * @return the index of the object within the array starting at the index, - * -1 if not found or null array input - */ - public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - startIndex = 0; - } - if (objectToFind == null) { - for (int i = startIndex; i < array.length; i++) { - if (array[i] == null) { - return i; - } - } - } else { - for (int i = startIndex; i < array.length; i++) { - if (objectToFind.equals(array[i])) { - return i; - } - } - } - return -1; - } - - /** - *

Find the last index of the given object within the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to travers backwords looking for the object, may be null - * @param objectToFind the object to find, may be null - * @return the last index of the object within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final Object[] array, final Object objectToFind) { - return lastIndexOf(array, objectToFind, Integer.MAX_VALUE); - } - - /** - *

Find the last index of the given object in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex will return -1. A startIndex larger than - * the array length will search from the end of the array.

- * - * @param array the array to traverse for looking for the object, may be null - * @param objectToFind the object to find, may be null - * @param startIndex the start index to travers backwards from - * @return the last index of the object within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - return -1; - } else if (startIndex >= array.length) { - startIndex = array.length - 1; - } - if (objectToFind == null) { - for (int i = startIndex; i >= 0; i--) { - if (array[i] == null) { - return i; - } - } - } else { - for (int i = startIndex; i >= 0; i--) { - if (objectToFind.equals(array[i])) { - return i; - } - } - } - return -1; - } - - /** - *

Checks if the object is in the given array.

- * - *

The method returns false if a null array is passed in.

- * - * @param array the array to search through - * @param objectToFind the object to find - * @return true if the array contains the object - */ - public static boolean contains(final Object[] array, final Object objectToFind) { - return (indexOf(array, objectToFind) != -1); - } - - // long IndexOf - //----------------------------------------------------------------------- - /** - *

Find the index of the given value in the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final long[] array, final long valueToFind) { - return indexOf(array, valueToFind, 0); - } - - /** - *

Find the index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return -1.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @param startIndex the index to start searching at - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final long[] array, final long valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - startIndex = 0; - } - for (int i = startIndex; i < array.length; i++) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; - } - - /** - *

Find the last index of the given value within the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to travers backwords looking for the object, may be null - * @param valueToFind the object to find - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final long[] array, final long valueToFind) { - return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); - } - - /** - *

Find the last index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex will return -1. A startIndex larger than the array - * length will search from the end of the array.

- * - * @param array the array to traverse for looking for the object, may be null - * @param valueToFind the value to find - * @param startIndex the start index to travers backwards from - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - return -1; - } else if (startIndex >= array.length) { - startIndex = array.length - 1; - } - for (int i = startIndex; i >= 0; i--) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; + return (short[]) array.clone(); } /** @@ -1403,98 +409,64 @@ public static int lastIndexOf(final long[] array, final long valueToFind, int st * @param valueToFind the value to find * @return true if the array contains the object */ - public static boolean contains(final long[] array, final long valueToFind) { + public static boolean contains(final boolean[] array, final boolean valueToFind) { return (indexOf(array, valueToFind) != -1); } - // int IndexOf - //----------------------------------------------------------------------- /** - *

Find the index of the given value in the array.

+ *

Checks if the value is in the given array.

* - *

This method returns -1 if null array input.

+ *

The method returns false if a null array is passed in.

* - * @param array the array to search through for the object, may be null + * @param array the array to search through * @param valueToFind the value to find - * @return the index of the value within the array, - * -1 if not found or null array input + * @return true if the array contains the object */ - public static int indexOf(final int[] array, final int valueToFind) { - return indexOf(array, valueToFind, 0); + public static boolean contains(final byte[] array, final byte valueToFind) { + return (indexOf(array, valueToFind) != -1); } /** - *

Find the index of the given value in the array starting at the given index.

+ *

Checks if the value is in the given array.

* - *

This method returns -1 if null array input.

- * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return -1.

+ *

The method returns false if a null array is passed in.

* - * @param array the array to search through for the object, may be null + * @param array the array to search through * @param valueToFind the value to find - * @param startIndex the index to start searching at - * @return the index of the value within the array, - * -1 if not found or null array input + * @return true if the array contains the object */ - public static int indexOf(final int[] array, final int valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - startIndex = 0; - } - for (int i = startIndex; i < array.length; i++) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; + public static boolean contains(final double[] array, final double valueToFind) { + return (indexOf(array, valueToFind) != -1); } /** - *

Find the last index of the given value within the array.

+ *

Checks if a value falling within the given tolerance is in the + * given array. If the array contains a value within the inclusive range + * defined by (value - tolerance) to (value + tolerance).

* - *

This method returns -1 if null array input.

- * - * @param array the array to travers backwords looking for the object, may be null - * @param valueToFind the object to find - * @return the last index of the value within the array, - * -1 if not found or null array input + *

The method returns false if a null array + * is passed in.

+ * + * @param array the array to search + * @param valueToFind the value to find + * @param tolerance the array contains the tolerance of the search + * @return true if value falling within tolerance is in array */ - public static int lastIndexOf(final int[] array, final int valueToFind) { - return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + public static boolean contains(final double[] array, final double valueToFind, final double tolerance) { + return (indexOf(array, valueToFind, 0, tolerance) != -1); } /** - *

Find the last index of the given value in the array starting at the given index.

+ *

Checks if the value is in the given array.

* - *

This method returns -1 if null array input.

- * - *

A negative startIndex will return -1. A startIndex larger than the array - * length will search from the end of the array.

+ *

The method returns false if a null array is passed in.

* - * @param array the array to traverse for looking for the object, may be null + * @param array the array to search through * @param valueToFind the value to find - * @param startIndex the start index to travers backwards from - * @return the last index of the value within the array, - * -1 if not found or null array input + * @return true if the array contains the object */ - public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - return -1; - } else if (startIndex >= array.length) { - startIndex = array.length - 1; - } - for (int i = startIndex; i >= 0; i--) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; + public static boolean contains(final float[] array, final float valueToFind) { + return (indexOf(array, valueToFind) != -1); } /** @@ -1510,7 +482,58 @@ public static boolean contains(final int[] array, final int valueToFind) { return (indexOf(array, valueToFind) != -1); } - // short IndexOf + /** + *

Checks if the value is in the given array.

+ * + *

The method returns false if a null array is passed in.

+ * + * @param array the array to search through + * @param valueToFind the value to find + * @return true if the array contains the object + */ + public static boolean contains(final long[] array, final long valueToFind) { + return (indexOf(array, valueToFind) != -1); + } + + /** + *

Checks if the object is in the given array.

+ * + *

The method returns false if a null array is passed in.

+ * + * @param array the array to search through + * @param objectToFind the object to find + * @return true if the array contains the object + */ + public static boolean contains(final Object[] array, final Object objectToFind) { + return (indexOf(array, objectToFind) != -1); + } + + /** + *

Checks if the value is in the given array.

+ * + *

The method returns false if a null array is passed in.

+ * + * @param array the array to search through + * @param valueToFind the value to find + * @return true if the array contains the object + */ + public static boolean contains(final short[] array, final short valueToFind) { + return (indexOf(array, valueToFind) != -1); + } + + /** + *

Get a hashCode for an array handling multi-dimensional arrays correctly.

+ * + *

Multi-dimensional primitive arrays are also handled correctly by this method.

+ * + * @param array the array to get a hashCode for, may be null + * @return a hashCode for the array, zero if null array input + */ + public static int hashCode(final Object array) { + return new HashCodeBuilder().append(array).toHashCode(); + } + + // boolean IndexOf //----------------------------------------------------------------------- /** *

Find the index of the given value in the array.

@@ -1522,7 +545,7 @@ public static boolean contains(final int[] array, final int valueToFind) { * @return the index of the value within the array, * -1 if not found or null array input */ - public static int indexOf(final short[] array, final short valueToFind) { + public static int indexOf(final boolean[] array, final boolean valueToFind) { return indexOf(array, valueToFind, 0); } @@ -1540,8 +563,8 @@ public static int indexOf(final short[] array, final short valueToFind) { * @return the index of the value within the array, * -1 if not found or null array input */ - public static int indexOf(final short[] array, final short valueToFind, int startIndex) { - if (array == null) { + public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) { + if (ArrayUtils.isEmpty(array)) { return -1; } if (startIndex < 0) { @@ -1555,64 +578,6 @@ public static int indexOf(final short[] array, final short valueToFind, int star return -1; } - /** - *

Find the last index of the given value within the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to travers backwords looking for the object, may be null - * @param valueToFind the object to find - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final short[] array, final short valueToFind) { - return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); - } - - /** - *

Find the last index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex will return -1. A startIndex larger than the array - * length will search from the end of the array.

- * - * @param array the array to traverse for looking for the object, may be null - * @param valueToFind the value to find - * @param startIndex the start index to travers backwards from - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - return -1; - } else if (startIndex >= array.length) { - startIndex = array.length - 1; - } - for (int i = startIndex; i >= 0; i--) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; - } - - /** - *

Checks if the value is in the given array.

- * - *

The method returns false if a null array is passed in.

- * - * @param array the array to search through - * @param valueToFind the value to find - * @return true if the array contains the object - */ - public static boolean contains(final short[] array, final short valueToFind) { - return (indexOf(array, valueToFind) != -1); - } - // byte IndexOf //----------------------------------------------------------------------- /** @@ -1658,64 +623,6 @@ public static int indexOf(final byte[] array, final byte valueToFind, int startI return -1; } - /** - *

Find the last index of the given value within the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to travers backwords looking for the object, may be null - * @param valueToFind the object to find - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final byte[] array, final byte valueToFind) { - return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); - } - - /** - *

Find the last index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex will return -1. A startIndex larger than the array - * length will search from the end of the array.

- * - * @param array the array to traverse for looking for the object, may be null - * @param valueToFind the value to find - * @param startIndex the start index to travers backwards from - * @return the last index of the value within the array, - * -1 if not found or null array input - */ - public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) { - if (array == null) { - return -1; - } - if (startIndex < 0) { - return -1; - } else if (startIndex >= array.length) { - startIndex = array.length - 1; - } - for (int i = startIndex; i >= 0; i--) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; - } - - /** - *

Checks if the value is in the given array.

- * - *

The method returns false if a null array is passed in.

- * - * @param array the array to search through - * @param valueToFind the value to find - * @return true if the array contains the object - */ - public static boolean contains(final byte[] array, final byte valueToFind) { - return (indexOf(array, valueToFind) != -1); - } - // double IndexOf //----------------------------------------------------------------------- /** @@ -1812,6 +719,689 @@ public static int indexOf(final double[] array, final double valueToFind, int st return -1; } + // float IndexOf + //----------------------------------------------------------------------- + /** + *

Find the index of the given value in the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final float[] array, final float valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *

Find the index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return -1.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final float[] array, final float valueToFind, int startIndex) { + if (ArrayUtils.isEmpty(array)) { + return -1; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + // int IndexOf + //----------------------------------------------------------------------- + /** + *

Find the index of the given value in the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final int[] array, final int valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *

Find the index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return -1.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final int[] array, final int valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + // long IndexOf + //----------------------------------------------------------------------- + /** + *

Find the index of the given value in the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final long[] array, final long valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *

Find the index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return -1.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final long[] array, final long valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + // IndexOf search + // ---------------------------------------------------------------------- + + // Object IndexOf + //----------------------------------------------------------------------- + /** + *

Find the index of the given object in the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to search through for the object, may be null + * @param objectToFind the object to find, may be null + * @return the index of the object within the array, + * -1 if not found or null array input + */ + public static int indexOf(final Object[] array, final Object objectToFind) { + return indexOf(array, objectToFind, 0); + } + + /** + *

Find the index of the given object in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return -1.

+ * + * @param array the array to search through for the object, may be null + * @param objectToFind the object to find, may be null + * @param startIndex the index to start searching at + * @return the index of the object within the array starting at the index, + * -1 if not found or null array input + */ + public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + startIndex = 0; + } + if (objectToFind == null) { + for (int i = startIndex; i < array.length; i++) { + if (array[i] == null) { + return i; + } + } + } else { + for (int i = startIndex; i < array.length; i++) { + if (objectToFind.equals(array[i])) { + return i; + } + } + } + return -1; + } + + // short IndexOf + //----------------------------------------------------------------------- + /** + *

Find the index of the given value in the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final short[] array, final short valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *

Find the index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return -1.

+ * + * @param array the array to search through for the object, may be null + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * -1 if not found or null array input + */ + public static int indexOf(final short[] array, final short valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + /** + *

Checks if an array of primitive booleans is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final boolean[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive bytes is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final byte[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive chars is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final char[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive doubles is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final double[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive floats is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final float[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive ints is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final int[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive longs is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final long[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + // ---------------------------------------------------------------------- + /** + *

Checks if an array of Objects is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final Object[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Checks if an array of primitive shorts is empty or null.

+ * + * @param array the array to test + * @return true if the array is empty or null + * @since 2.1 + */ + public static boolean isEmpty(final short[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + /** + *

Compares two arrays, using equals(), handling multi-dimensional arrays + * correctly.

+ * + *

Multi-dimensional primitive arrays are also handled correctly by this method.

+ * + * @param array1 the left hand array to compare, may be null + * @param array2 the right hand array to compare, may be null + * @return true if the arrays are equal + */ + public static boolean isEquals(final Object array1, final Object array2) { + return new EqualsBuilder().append(array1, array2).isEquals(); + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final boolean[] array1, final boolean[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final byte[] array1, final byte[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final char[] array1, final char[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final double[] array1, final double[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final float[] array1, final float[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final int[] array1, final int[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final long[] array1, final long[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + // Is same length + //----------------------------------------------------------------------- + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0. + * + *

Any multi-dimensional aspects of the arrays are ignored.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final Object[] array1, final Object[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same length, treating + * null arrays as length 0.

+ * + * @param array1 the first array, may be null + * @param array2 the second array, may be null + * @return true if length of arrays matches, treating + * null as an empty array + */ + public static boolean isSameLength(final short[] array1, final short[] array2) { + if ((array1 == null && array2 != null && array2.length > 0) || + (array2 == null && array1 != null && array1.length > 0) || + (array1 != null && array2 != null && array1.length != array2.length)) { + return false; + } + return true; + } + + /** + *

Checks whether two arrays are the same type taking into account + * multi-dimensional arrays.

+ * + * @param array1 the first array, must not be null + * @param array2 the second array, must not be null + * @return true if type of arrays matches + * @throws IllegalArgumentException if either array is null + */ + public static boolean isSameType(final Object array1, final Object array2) { + if (array1 == null || array2 == null) { + throw new IllegalArgumentException("The Array must not be null"); + } + return array1.getClass().getName().equals(array2.getClass().getName()); + } + + /** + *

Joins the elements of the provided arrays into a single new array.

+ *

The new array contains all of the element of the first array followed + * by all of the elements from the second array.

+ * + *
+     * ArrayUtils.join(null, null)     = null
+     * ArrayUtils.join(array1, null)   = array1
+     * ArrayUtils.join(null, array2)   = array2
+     * ArrayUtils.join([], [])         = []
+     * ArrayUtils.join([null], [null]) = [null, null]
+     * ArrayUtils.join(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
+     * 
+ * + * @param array1 the first array of values to join together, may be null + * @param array2 the second array of values to join together, may be null + * @return The new joined array, null if null array inputs. + * The type of the joined array is the type of the first array. + * @since 2.1 + */ + public static Object[] join(Object[] array1, Object[] array2) { + if (array1 == null) { + return array2; + } else if (array2 == null) { + return array1; + } else { + Object[] joinedArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(), array1.length + + array2.length); + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + } + + /** + *

Find the last index of the given value within the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to travers backwords looking for the object, may be null + * @param valueToFind the object to find + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final boolean[] array, final boolean valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *

Find the last index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex will return -1. A startIndex larger than the array + * length will search from the end of the array.

+ * + * @param array the array to traverse for looking for the object, may be null + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) { + if (ArrayUtils.isEmpty(array)) { + return -1; + } + if (startIndex < 0) { + return -1; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + /** + *

Find the last index of the given value within the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to travers backwords looking for the object, may be null + * @param valueToFind the object to find + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final byte[] array, final byte valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *

Find the last index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex will return -1. A startIndex larger than the array + * length will search from the end of the array.

+ * + * @param array the array to traverse for looking for the object, may be null + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + return -1; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + /** *

Find the last index of the given value within the array.

* @@ -1910,81 +1500,6 @@ public static int lastIndexOf(final double[] array, final double valueToFind, in return -1; } - /** - *

Checks if the value is in the given array.

- * - *

The method returns false if a null array is passed in.

- * - * @param array the array to search through - * @param valueToFind the value to find - * @return true if the array contains the object - */ - public static boolean contains(final double[] array, final double valueToFind) { - return (indexOf(array, valueToFind) != -1); - } - - /** - *

Checks if a value falling within the given tolerance is in the - * given array. If the array contains a value within the inclusive range - * defined by (value - tolerance) to (value + tolerance).

- * - *

The method returns false if a null array - * is passed in.

- * - * @param array the array to search - * @param valueToFind the value to find - * @param tolerance the array contains the tolerance of the search - * @return true if value falling within tolerance is in array - */ - public static boolean contains(final double[] array, final double valueToFind, final double tolerance) { - return (indexOf(array, valueToFind, 0, tolerance) != -1); - } - - // float IndexOf - //----------------------------------------------------------------------- - /** - *

Find the index of the given value in the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final float[] array, final float valueToFind) { - return indexOf(array, valueToFind, 0); - } - - /** - *

Find the index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return -1.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @param startIndex the index to start searching at - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final float[] array, final float valueToFind, int startIndex) { - if (ArrayUtils.isEmpty(array)) { - return -1; - } - if (startIndex < 0) { - startIndex = 0; - } - for (int i = startIndex; i < array.length; i++) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; - } - /** *

Find the last index of the given value within the array.

* @@ -2030,64 +1545,6 @@ public static int lastIndexOf(final float[] array, final float valueToFind, int return -1; } - /** - *

Checks if the value is in the given array.

- * - *

The method returns false if a null array is passed in.

- * - * @param array the array to search through - * @param valueToFind the value to find - * @return true if the array contains the object - */ - public static boolean contains(final float[] array, final float valueToFind) { - return (indexOf(array, valueToFind) != -1); - } - - // boolean IndexOf - //----------------------------------------------------------------------- - /** - *

Find the index of the given value in the array.

- * - *

This method returns -1 if null array input.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final boolean[] array, final boolean valueToFind) { - return indexOf(array, valueToFind, 0); - } - - /** - *

Find the index of the given value in the array starting at the given index.

- * - *

This method returns -1 if null array input.

- * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return -1.

- * - * @param array the array to search through for the object, may be null - * @param valueToFind the value to find - * @param startIndex the index to start searching at - * @return the index of the value within the array, - * -1 if not found or null array input - */ - public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) { - if (ArrayUtils.isEmpty(array)) { - return -1; - } - if (startIndex < 0) { - startIndex = 0; - } - for (int i = startIndex; i < array.length; i++) { - if (valueToFind == array[i]) { - return i; - } - } - return -1; - } - /** *

Find the last index of the given value within the array.

* @@ -2098,7 +1555,7 @@ public static int indexOf(final boolean[] array, final boolean valueToFind, int * @return the last index of the value within the array, * -1 if not found or null array input */ - public static int lastIndexOf(final boolean[] array, final boolean valueToFind) { + public static int lastIndexOf(final int[] array, final int valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } @@ -2116,8 +1573,8 @@ public static int lastIndexOf(final boolean[] array, final boolean valueToFind) * @return the last index of the value within the array, * -1 if not found or null array input */ - public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) { - if (ArrayUtils.isEmpty(array)) { + public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) { + if (array == null) { return -1; } if (startIndex < 0) { @@ -2134,16 +1591,1145 @@ public static int lastIndexOf(final boolean[] array, final boolean valueToFind, } /** - *

Checks if the value is in the given array.

+ *

Find the last index of the given value within the array.

* - *

The method returns false if a null array is passed in.

+ *

This method returns -1 if null array input.

* - * @param array the array to search through - * @param valueToFind the value to find - * @return true if the array contains the object + * @param array the array to travers backwords looking for the object, may be null + * @param valueToFind the object to find + * @return the last index of the value within the array, + * -1 if not found or null array input */ - public static boolean contains(final boolean[] array, final boolean valueToFind) { - return (indexOf(array, valueToFind) != -1); + public static int lastIndexOf(final long[] array, final long valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *

Find the last index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex will return -1. A startIndex larger than the array + * length will search from the end of the array.

+ * + * @param array the array to traverse for looking for the object, may be null + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + return -1; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + /** + *

Find the last index of the given object within the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to travers backwords looking for the object, may be null + * @param objectToFind the object to find, may be null + * @return the last index of the object within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final Object[] array, final Object objectToFind) { + return lastIndexOf(array, objectToFind, Integer.MAX_VALUE); + } + + /** + *

Find the last index of the given object in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex will return -1. A startIndex larger than + * the array length will search from the end of the array.

+ * + * @param array the array to traverse for looking for the object, may be null + * @param objectToFind the object to find, may be null + * @param startIndex the start index to travers backwards from + * @return the last index of the object within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + return -1; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + if (objectToFind == null) { + for (int i = startIndex; i >= 0; i--) { + if (array[i] == null) { + return i; + } + } + } else { + for (int i = startIndex; i >= 0; i--) { + if (objectToFind.equals(array[i])) { + return i; + } + } + } + return -1; + } + + /** + *

Find the last index of the given value within the array.

+ * + *

This method returns -1 if null array input.

+ * + * @param array the array to travers backwords looking for the object, may be null + * @param valueToFind the object to find + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final short[] array, final short valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *

Find the last index of the given value in the array starting at the given index.

+ * + *

This method returns -1 if null array input.

+ * + *

A negative startIndex will return -1. A startIndex larger than the array + * length will search from the end of the array.

+ * + * @param array the array to traverse for looking for the object, may be null + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * -1 if not found or null array input + */ + public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) { + if (array == null) { + return -1; + } + if (startIndex < 0) { + return -1; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return -1; + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final boolean[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + boolean tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final byte[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + byte tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final char[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + char tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final double[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + double tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final float[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + float tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final int[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + int tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final long[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + long tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + // Reverse + //----------------------------------------------------------------------- + /** + *

Reverses the order of the given array.

+ * + *

There is no special handling for multi-dimensional arrays.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final Object[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + Object tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Reverses the order of the given array.

+ * + *

This method does nothing if null array input.

+ * + * @param array the array to reverse, may be null + */ + public static void reverse(final short[] array) { + if (array == null) { + return; + } + int i = 0; + int j = array.length - 1; + short tmp; + while (j > i) { + tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + j--; + i++; + } + } + + /** + *

Produces a new boolean array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_BOOLEAN_ARRAY; + } + + boolean[] subarray = new boolean[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new byte array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_BYTE_ARRAY; + } + + byte[] subarray = new byte[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new char array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_CHAR_ARRAY; + } + + char[] subarray = new char[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new double array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_DOUBLE_ARRAY; + } + + double[] subarray = new double[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new float array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_FLOAT_ARRAY; + } + + float[] subarray = new float[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new int array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_INT_ARRAY; + } + + int[] subarray = new int[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new long array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_LONG_ARRAY; + } + + long[] subarray = new long[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + // Subarrays + //----------------------------------------------------------------------- + /** + *

Produces a new array containing the elements between + * the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + *

The component type of the subarray is always the same as + * that of the input array. Thus, if the input is an array of type + * Date, the following usage is envisaged:

+ * + *
+     * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
+     * 
+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + Class type = array.getClass().getComponentType(); + if (newSize <= 0) { + return (Object[]) Array.newInstance(type, 0); + } + Object[] subarray = (Object[]) Array.newInstance(type, newSize); + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new short array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + * @return a new array containing the elements between + * the start and end indices. + */ + public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_SHORT_ARRAY; + } + + short[] subarray = new short[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + // To map + //----------------------------------------------------------------------- + /** + *

Converts the given array into a {@link java.util.Map}. Each element of the array + * must be either a {@link java.util.Map.Entry} or an Array, containing at least two + * elements, where the first element is used as key and the second as + * value.

+ * + *

This method can be used to initialize:

+ *
+     * // Create a Map mapping colors.
+     * Map colorMap = MapUtils.toMap(new String[][] {{
+     *     {"RED", "#FF0000"},
+     *     {"GREEN", "#00FF00"},
+     *     {"BLUE", "#0000FF"}});
+     * 
+ * + *

This method returns null if null array input.

+ * + * @param array an array whose elements are either a {@link java.util.Map.Entry} or + * an Array containing at least two elements, may be null + * @return a Map that was created from the array + * @throws IllegalArgumentException if one element of this Array is + * itself an Array containing less then two elements + * @throws IllegalArgumentException if the array contains elements other + * than {@link java.util.Map.Entry} and an Array + */ + public static Map toMap(final Object[] array) { + if (array == null) { + return null; + } + final Map map = new HashMap((int) (array.length * 1.5)); + for (int i = 0; i < array.length; i++) { + Object object = array[i]; + if (object instanceof Map.Entry) { + Map.Entry entry = (Map.Entry) object; + map.put(entry.getKey(), entry.getValue()); + } else if (object instanceof Object[]) { + Object[] entry = (Object[]) object; + if (entry.length < 2) { + throw new IllegalArgumentException("Array element " + i + ", '" + + object + + "', has a length less than 2"); + } + map.put(entry[0], entry[1]); + } else { + throw new IllegalArgumentException("Array element " + i + ", '" + + object + + "', is neither of type Map.Entry nor an Array"); + } + } + return map; + } + + /** + *

Converts an array of primitive booleans to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a boolean array + * @return a Boolean array, null if null array input + */ + public static Boolean[] toObject(final boolean[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BOOLEAN_OBJECT_ARRAY; + } + final Boolean[] result = new Boolean[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE); + } + return result; + } + + /** + *

Converts an array of primitive bytes to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a byte array + * @return a Byte array, null if null array input + */ + public static Byte[] toObject(final byte[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BYTE_OBJECT_ARRAY; + } + final Byte[] result = new Byte[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Byte(array[i]); + } + return result; + } + + /** + *

Converts an array of primitive doubles to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a double array + * @return a Double array, null if null array input + */ + public static Double[] toObject(final double[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_DOUBLE_OBJECT_ARRAY; + } + final Double[] result = new Double[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Double(array[i]); + } + return result; + } + + /** + *

Converts an array of primitive floats to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a float array + * @return a Float array, null if null array input + */ + public static Float[] toObject(final float[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_FLOAT_OBJECT_ARRAY; + } + final Float[] result = new Float[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Float(array[i]); + } + return result; + } + + /** + *

Converts an array of primitive ints to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array an int array + * @return an Integer array, null if null array input + */ + public static Integer[] toObject(final int[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_INTEGER_OBJECT_ARRAY; + } + final Integer[] result = new Integer[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Integer(array[i]); + } + return result; + } + + /** + *

Converts an array of primitive longs to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a long array + * @return a Long array, null if null array input + */ + public static Long[] toObject(final long[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_LONG_OBJECT_ARRAY; + } + final Long[] result = new Long[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Long(array[i]); + } + return result; + } + + /** + *

Converts an array of primitive shorts to objects.

+ * + *

This method returns null if null array input.

+ * + * @param array a short array + * @return a Short array, null if null array input + */ + public static Short[] toObject(final short[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_SHORT_OBJECT_ARRAY; + } + final Short[] result = new Short[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = new Short(array[i]); + } + return result; + } + + // Boolean array converters + // ---------------------------------------------------------------------- + /** + *

Converts an array of object Booleans to primitives.

+ * + *

This method returns null if null array input.

+ * + * @param array a Boolean array, may be null + * @return a boolean array, null if null array input + * @throws NullPointerException if array content is null + */ + public static boolean[] toPrimitive(final Boolean[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BOOLEAN_ARRAY; + } + final boolean[] result = new boolean[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i].booleanValue(); + } + return result; + } + + /** + *

Converts an array of object Booleans to primitives handling null.

+ * + *

This method returns null if null array input.

+ * + * @param array a Boolean array, may be null + * @param valueForNull the value to insert if null found + * @return a boolean array, null if null array input + */ + public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BOOLEAN_ARRAY; + } + final boolean[] result = new boolean[array.length]; + for (int i = 0; i < array.length; i++) { + Boolean b = array[i]; + result[i] = (b == null ? valueForNull : b.booleanValue()); + } + return result; + } + + // Byte array converters + // ---------------------------------------------------------------------- + /** + *

Converts an array of object Bytes to primitives.

+ * + *

This method returns null if null array input.

+ * + * @param array a Byte array, may be null + * @return a byte array, null if null array input + * @throws NullPointerException if array content is null + */ + public static byte[] toPrimitive(final Byte[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BYTE_ARRAY; + } + final byte[] result = new byte[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i].byteValue(); + } + return result; + } + + /** + *

Converts an array of object Bytes to primitives handling null.

+ * + *

This method returns null if null array input.

+ * + * @param array a Byte array, may be null + * @param valueForNull the value to insert if null found + * @return a byte array, null if null array input + */ + public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_BYTE_ARRAY; + } + final byte[] result = new byte[array.length]; + for (int i = 0; i < array.length; i++) { + Byte b = array[i]; + result[i] = (b == null ? valueForNull : b.byteValue()); + } + return result; + } + + // Double array converters + // ---------------------------------------------------------------------- + /** + *

Converts an array of object Doubles to primitives.

+ * + *

This method returns null if null array input.

+ * + * @param array a Double array, may be null + * @return a double array, null if null array input + * @throws NullPointerException if array content is null + */ + public static double[] toPrimitive(final Double[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_DOUBLE_ARRAY; + } + final double[] result = new double[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i].doubleValue(); + } + return result; + } + + /** + *

Converts an array of object Doubles to primitives handling null.

+ * + *

This method returns null if null array input.

+ * + * @param array a Double array, may be null + * @param valueForNull the value to insert if null found + * @return a double array, null if null array input + */ + public static double[] toPrimitive(final Double[] array, final double valueForNull) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_DOUBLE_ARRAY; + } + final double[] result = new double[array.length]; + for (int i = 0; i < array.length; i++) { + Double b = array[i]; + result[i] = (b == null ? valueForNull : b.doubleValue()); + } + return result; + } + + // Float array converters + // ---------------------------------------------------------------------- + /** + *

Converts an array of object Floats to primitives.

+ * + *

This method returns null if null array input.

+ * + * @param array a Float array, may be null + * @return a float array, null if null array input + * @throws NullPointerException if array content is null + */ + public static float[] toPrimitive(final Float[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_FLOAT_ARRAY; + } + final float[] result = new float[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i].floatValue(); + } + return result; + } + + /** + *

Converts an array of object Floats to primitives handling null.

+ * + *

This method returns null if null array input.

+ * + * @param array a Float array, may be null + * @param valueForNull the value to insert if null found + * @return a float array, null if null array input + */ + public static float[] toPrimitive(final Float[] array, final float valueForNull) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_FLOAT_ARRAY; + } + final float[] result = new float[array.length]; + for (int i = 0; i < array.length; i++) { + Float b = array[i]; + result[i] = (b == null ? valueForNull : b.floatValue()); + } + return result; + } + + // Int array converters + // ---------------------------------------------------------------------- + /** + *

Converts an array of object Integers to primitives.

+ * + *

This method returns null if null array input.

+ * + * @param array a Integer array, may be null + * @return an int array, null if null array input + * @throws NullPointerException if array content is null + */ + public static int[] toPrimitive(final Integer[] array) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_INT_ARRAY; + } + final int[] result = new int[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i].intValue(); + } + return result; + } + + /** + *

Converts an array of object Integer to primitives handling null.

+ * + *

This method returns null if null array input.

+ * + * @param array a Integer array, may be null + * @param valueForNull the value to insert if null found + * @return an int array, null if null array input + */ + public static int[] toPrimitive(final Integer[] array, final int valueForNull) { + if (array == null) { + return null; + } else if (array.length == 0) { + return EMPTY_INT_ARRAY; + } + final int[] result = new int[array.length]; + for (int i = 0; i < array.length; i++) { + Integer b = array[i]; + result[i] = (b == null ? valueForNull : b.intValue()); + } + return result; } // Primitive/Object array converters @@ -2196,95 +2782,6 @@ public static long[] toPrimitive(final Long[] array, final long valueForNull) { return result; } - /** - *

Converts an array of primitive longs to objects.

- * - *

This method returns null if null array input.

- * - * @param array a long array - * @return a Long array, null if null array input - */ - public static Long[] toObject(final long[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_LONG_OBJECT_ARRAY; - } - final Long[] result = new Long[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Long(array[i]); - } - return result; - } - - // Int array converters - // ---------------------------------------------------------------------- - /** - *

Converts an array of object Integers to primitives.

- * - *

This method returns null if null array input.

- * - * @param array a Integer array, may be null - * @return an int array, null if null array input - * @throws NullPointerException if array content is null - */ - public static int[] toPrimitive(final Integer[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_INT_ARRAY; - } - final int[] result = new int[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].intValue(); - } - return result; - } - - /** - *

Converts an array of object Integer to primitives handling null.

- * - *

This method returns null if null array input.

- * - * @param array a Integer array, may be null - * @param valueForNull the value to insert if null found - * @return an int array, null if null array input - */ - public static int[] toPrimitive(final Integer[] array, final int valueForNull) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_INT_ARRAY; - } - final int[] result = new int[array.length]; - for (int i = 0; i < array.length; i++) { - Integer b = array[i]; - result[i] = (b == null ? valueForNull : b.intValue()); - } - return result; - } - - /** - *

Converts an array of primitive ints to objects.

- * - *

This method returns null if null array input.

- * - * @param array an int array - * @return an Integer array, null if null array input - */ - public static Integer[] toObject(final int[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_INTEGER_OBJECT_ARRAY; - } - final Integer[] result = new Integer[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Integer(array[i]); - } - return result; - } - // Short array converters // ---------------------------------------------------------------------- /** @@ -2331,547 +2828,50 @@ public static short[] toPrimitive(final Short[] array, final short valueForNull) } return result; } - - /** - *

Converts an array of primitive shorts to objects.

- * - *

This method returns null if null array input.

- * - * @param array a short array - * @return a Short array, null if null array input - */ - public static Short[] toObject(final short[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_SHORT_OBJECT_ARRAY; - } - final Short[] result = new Short[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Short(array[i]); - } - return result; - } - - // Byte array converters - // ---------------------------------------------------------------------- - /** - *

Converts an array of object Bytes to primitives.

- * - *

This method returns null if null array input.

- * - * @param array a Byte array, may be null - * @return a byte array, null if null array input - * @throws NullPointerException if array content is null - */ - public static byte[] toPrimitive(final Byte[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BYTE_ARRAY; - } - final byte[] result = new byte[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].byteValue(); - } - return result; - } - - /** - *

Converts an array of object Bytes to primitives handling null.

- * - *

This method returns null if null array input.

- * - * @param array a Byte array, may be null - * @param valueForNull the value to insert if null found - * @return a byte array, null if null array input - */ - public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BYTE_ARRAY; - } - final byte[] result = new byte[array.length]; - for (int i = 0; i < array.length; i++) { - Byte b = array[i]; - result[i] = (b == null ? valueForNull : b.byteValue()); - } - return result; - } - - /** - *

Converts an array of primitive bytes to objects.

- * - *

This method returns null if null array input.

- * - * @param array a byte array - * @return a Byte array, null if null array input - */ - public static Byte[] toObject(final byte[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BYTE_OBJECT_ARRAY; - } - final Byte[] result = new Byte[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Byte(array[i]); - } - return result; - } - // Double array converters - // ---------------------------------------------------------------------- + // Basic methods handling multi-dimensional arrays + //----------------------------------------------------------------------- /** - *

Converts an array of object Doubles to primitives.

+ *

Outputs an array as a String, treating null as an empty array.

* - *

This method returns null if null array input.

+ *

Multi-dimensional arrays are handled correctly, including + * multi-dimensional primitive arrays.

+ * + *

The format is that of Java source code, for example {a,b}.

* - * @param array a Double array, may be null - * @return a double array, null if null array input - * @throws NullPointerException if array content is null + * @param array the array to get a toString for, may be null + * @return a String representation of the array, '{}' if null array input */ - public static double[] toPrimitive(final Double[] array) { + public static String toString(final Object array) { + return toString(array, "{}"); + } + + /** + *

Outputs an array as a String handling nulls.

+ * + *

Multi-dimensional arrays are handled correctly, including + * multi-dimensional primitive arrays.

+ * + *

The format is that of Java source code, for example {a,b}.

+ * + * @param array the array to get a toString for, may be null + * @param stringIfNull the String to return if the array is null + * @return a String representation of the array + */ + public static String toString(final Object array, final String stringIfNull) { if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_DOUBLE_ARRAY; + return stringIfNull; } - final double[] result = new double[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].doubleValue(); - } - return result; + return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString(); } /** - *

Converts an array of object Doubles to primitives handling null.

- * - *

This method returns null if null array input.

- * - * @param array a Double array, may be null - * @param valueForNull the value to insert if null found - * @return a double array, null if null array input - */ - public static double[] toPrimitive(final Double[] array, final double valueForNull) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_DOUBLE_ARRAY; - } - final double[] result = new double[array.length]; - for (int i = 0; i < array.length; i++) { - Double b = array[i]; - result[i] = (b == null ? valueForNull : b.doubleValue()); - } - return result; - } - - /** - *

Converts an array of primitive doubles to objects.

+ *

ArrayUtils instances should NOT be constructed in standard programming. + * Instead, the class should be used as ArrayUtils.clone(new int[] {2}).

* - *

This method returns null if null array input.

- * - * @param array a double array - * @return a Double array, null if null array input + *

This constructor is public to permit tools that require a JavaBean instance + * to operate.

*/ - public static Double[] toObject(final double[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_DOUBLE_OBJECT_ARRAY; - } - final Double[] result = new Double[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Double(array[i]); - } - return result; - } - - // Float array converters - // ---------------------------------------------------------------------- - /** - *

Converts an array of object Floats to primitives.

- * - *

This method returns null if null array input.

- * - * @param array a Float array, may be null - * @return a float array, null if null array input - * @throws NullPointerException if array content is null - */ - public static float[] toPrimitive(final Float[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_FLOAT_ARRAY; - } - final float[] result = new float[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].floatValue(); - } - return result; - } - - /** - *

Converts an array of object Floats to primitives handling null.

- * - *

This method returns null if null array input.

- * - * @param array a Float array, may be null - * @param valueForNull the value to insert if null found - * @return a float array, null if null array input - */ - public static float[] toPrimitive(final Float[] array, final float valueForNull) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_FLOAT_ARRAY; - } - final float[] result = new float[array.length]; - for (int i = 0; i < array.length; i++) { - Float b = array[i]; - result[i] = (b == null ? valueForNull : b.floatValue()); - } - return result; - } - - /** - *

Converts an array of primitive floats to objects.

- * - *

This method returns null if null array input.

- * - * @param array a float array - * @return a Float array, null if null array input - */ - public static Float[] toObject(final float[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_FLOAT_OBJECT_ARRAY; - } - final Float[] result = new Float[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = new Float(array[i]); - } - return result; - } - - // Boolean array converters - // ---------------------------------------------------------------------- - /** - *

Converts an array of object Booleans to primitives.

- * - *

This method returns null if null array input.

- * - * @param array a Boolean array, may be null - * @return a boolean array, null if null array input - * @throws NullPointerException if array content is null - */ - public static boolean[] toPrimitive(final Boolean[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BOOLEAN_ARRAY; - } - final boolean[] result = new boolean[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i].booleanValue(); - } - return result; - } - - /** - *

Converts an array of object Booleans to primitives handling null.

- * - *

This method returns null if null array input.

- * - * @param array a Boolean array, may be null - * @param valueForNull the value to insert if null found - * @return a boolean array, null if null array input - */ - public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BOOLEAN_ARRAY; - } - final boolean[] result = new boolean[array.length]; - for (int i = 0; i < array.length; i++) { - Boolean b = array[i]; - result[i] = (b == null ? valueForNull : b.booleanValue()); - } - return result; - } - - /** - *

Converts an array of primitive booleans to objects.

- * - *

This method returns null if null array input.

- * - * @param array a boolean array - * @return a Boolean array, null if null array input - */ - public static Boolean[] toObject(final boolean[] array) { - if (array == null) { - return null; - } else if (array.length == 0) { - return EMPTY_BOOLEAN_OBJECT_ARRAY; - } - final Boolean[] result = new Boolean[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE); - } - return result; - } - - // ---------------------------------------------------------------------- - /** - *

Checks if an array of Objects is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final Object[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive longs is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final long[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive ints is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final int[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive shorts is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final short[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive chars is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final char[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive bytes is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final byte[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive doubles is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final double[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive floats is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final float[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Checks if an array of primitive booleans is empty or null.

- * - * @param array the array to test - * @return true if the array is empty or null - * @since 2.1 - */ - public static boolean isEmpty(final boolean[] array) { - if (array == null || array.length == 0) { - return true; - } - return false; - } - - /** - *

Joins the elements of the provided arrays into a single new array.

- *

The new array contains all of the element of the first array followed - * by all of the elements from the second array.

- * - *
-     * ArrayUtils.join(null, null)     = null
-     * ArrayUtils.join(array1, null)   = array1
-     * ArrayUtils.join(null, array2)   = array2
-     * ArrayUtils.join([], [])         = []
-     * ArrayUtils.join([null], [null]) = [null, null]
-     * ArrayUtils.join(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
-     * 
- * - * @param array1 the first array of values to join together, may be null - * @param array2 the second array of values to join together, may be null - * @return The new joined array, null if null array inputs. - * The type of the joined array is the type of the first array. - * @since 2.1 - */ - public static Object[] join(Object[] array1, Object[] array2) { - if (array1 == null) { - return array2; - } else if (array2 == null) { - return array1; - } else { - Object[] joinedArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(), array1.length - + array2.length); - System.arraycopy(array1, 0, joinedArray, 0, array1.length); - System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); - return joinedArray; - } - } - - /** - *

Adds the element to the end of the array.

- * - *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of - * the new array is the same as that of the input array.

- * - *

If the input array is null, a new one element array is returned - * whose component type is the same as the element.

- * - *
-     * ArrayUtils.add(null, null)      = [null]
-     * ArrayUtils.add(null, "a")       = ["a"]
-     * ArrayUtils.add(["a"], null)     = ["a", null]
-     * ArrayUtils.add(["a"], "b")      = ["a", "b"]
-     * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
-     * 
- * - * @param array the array to "add" the element to, may be null - * @param element the object to add - * @return A new array containing the existing elements and the new element - * @since 2.1 - */ - public static Object[] add(Object[] array, Object element) { - Object joinedArray; - int elementPos; - if (array != null) { - joinedArray = Array.newInstance(array.getClass().getComponentType(), array.length + 1); - System.arraycopy(array, 0, joinedArray, 0, array.length); - elementPos = array.length; - } else { - // null input array, use the element type - joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1); - elementPos = 0; - } - Array.set(joinedArray, elementPos, element); - return (Object[]) joinedArray; - } - - /** - *

Inserts the specified element at the specified position in the array. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices).

- * - *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input - * array.

- * - *

If the input array is null, a new one element array is returned - * whose component type is the same as the element.

- * - *
-     * ArrayUtils.add(null, 0, null)      = [null]
-     * ArrayUtils.add(null, 0, "a")       = ["a"]
-     * ArrayUtils.add(["a"], 1, null)     = ["a", null]
-     * ArrayUtils.add(["a"], 1, "b")      = ["a", "b"]
-     * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
-     * 
- * - * @param array the array to add the element to, may be null - * @param index the position of the new object - * @param element the object to add - * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range - * (index < 0 || index > array.length). - */ - public static Object[] add(final Object[] array, final int index, final Object element) { - if (array == null) { - if (index != 0) { - throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0"); - } - Object joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1); - Array.set(joinedArray, 0, element); - return (Object[]) joinedArray; - } - int length = array.length; - if (index > length || index < 0) { - throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); - } - Object result = Array.newInstance(array.getClass().getComponentType(), length + 1); - System.arraycopy(array, 0, result, 0, index); - Array.set(result, index, element); - if (index < length) { - System.arraycopy(array, index, result, index + 1, length - index); - } - return (Object[]) result; + public ArrayUtils() { } }