Added: incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ArrayUtils.java URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ArrayUtils.java?view=auto&rev=151131 ============================================================================== --- incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ArrayUtils.java (added) +++ incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/util/ArrayUtils.java Wed Feb 2 23:18:42 2005 @@ -0,0 +1,4696 @@ +/* + * Copyright 2002-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.asn1.util; + + +import java.lang.reflect.Array; +import java.util.HashMap; +import java.util.Map; + + +/** + * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and + * primitive wrapper arrays (like <code>Integer[]</code>).</p> + * <p/> + * <p>This class tries to handle <code>null</code> input gracefully. An + * exception will not be thrown for a <code>null</code> array input. However, an + * Object array that contains a <code>null</code> element may throw an + * exception. Each method documents its behaviour.</p> + * + * @author Alex Karasulu + * @author Stephen Colebourne + * @author Moritz Petersen + * @author <a href="mailto:[EMAIL PROTECTED]">Fredrik Westermarck</a> + * @author Nikolay Metchev + * @author Matthew Hawthorne + * @author Tim O'Brien + * @author Pete Gieser + * @author Gary Gregory + * @author <a href="mailto:[EMAIL PROTECTED]">Ashwin S</a> + * @author Maarten Coene + * @since 2.0 + */ +public class ArrayUtils +{ + + /** + * An empty immutable <code>Object</code> array. + */ + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + /** + * An empty immutable <code>Class</code> array. + */ + public static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + /** + * An empty immutable <code>String</code> array. + */ + public static final String[] EMPTY_STRING_ARRAY = new String[0]; + /** + * An empty immutable <code>long</code> array. + */ + public static final long[] EMPTY_LONG_ARRAY = new long[0]; + /** + * An empty immutable <code>Long</code> array. + */ + public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; + /** + * An empty immutable <code>int</code> array. + */ + public static final int[] EMPTY_INT_ARRAY = new int[0]; + /** + * An empty immutable <code>Integer</code> array. + */ + public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0]; + /** + * An empty immutable <code>short</code> array. + */ + public static final short[] EMPTY_SHORT_ARRAY = new short[0]; + /** + * An empty immutable <code>Short</code> array. + */ + public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; + /** + * An empty immutable <code>byte</code> array. + */ + public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + /** + * An empty immutable <code>Byte</code> array. + */ + public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0]; + /** + * An empty immutable <code>double</code> array. + */ + public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]; + /** + * An empty immutable <code>Double</code> array. + */ + public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0]; + /** + * An empty immutable <code>float</code> array. + */ + public static final float[] EMPTY_FLOAT_ARRAY = new float[0]; + /** + * An empty immutable <code>Float</code> array. + */ + public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0]; + /** + * An empty immutable <code>boolean</code> array. + */ + public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; + /** + * An empty immutable <code>Boolean</code> array. + */ + public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0]; + /** + * An empty immutable <code>char</code> array. + */ + public static final char[] EMPTY_CHAR_ARRAY = new char[0]; + /** + * An empty immutable <code>Character</code> array. + */ + public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0]; + + + /** + * <p>ArrayUtils instances should NOT be constructed in standard + * programming. Instead, the class should be used as + * <code>ArrayUtils.clone(new int[] {2})</code>.</p> + * <p/> + * <p>This constructor is public to permit tools that require a JavaBean + * instance to operate.</p> + */ + public ArrayUtils() + { + } + + // Basic methods handling multi-dimensional arrays + //----------------------------------------------------------------------- + /** + * <p>Outputs an array as a String, treating <code>null</code> as an empty + * array.</p> + * <p/> + * <p>Multi-dimensional arrays are handled correctly, including + * multi-dimensional primitive arrays.</p> + * <p/> + * <p>The format is that of Java source code, for example + * <code>{a,b}</code>.</p> + * + * @param array the array to get a toString for, may be <code>null</code> + * @return a String representation of the array, '{}' if null array input + */ + public static String toString( Object array ) + { + return toString( array, "{}" ); + } + + + /** + * <p>Outputs an array as a String handling <code>null</code>s.</p> + * <p/> + * <p>Multi-dimensional arrays are handled correctly, including + * multi-dimensional primitive arrays.</p> + * <p/> + * <p>The format is that of Java source code, for example + * <code>{a,b}</code>.</p> + * + * @param array the array to get a toString for, may be + * <code>null</code> + * @param stringIfNull the String to return if the array is + * <code>null</code> + * @return a String representation of the array + */ + public static String toString( Object array, String stringIfNull ) + { + if ( array == null ) + { + return stringIfNull; + } + return new ToStringBuilder( array, ToStringStyle.SIMPLE_STYLE ).append( array ).toString(); + } + + + /** + * <p>Get a hashCode for an array handling multi-dimensional arrays + * correctly.</p> + * <p/> + * <p>Multi-dimensional primitive arrays are also handled correctly by this + * method.</p> + * + * @param array the array to get a hashCode for, may be <code>null</code> + * @return a hashCode for the array, zero if null array input + */ + public static int hashCode( Object array ) + { + return new HashCodeBuilder().append( array ).toHashCode(); + } + + + /** + * <p>Compares two arrays, using equals(), handling multi-dimensional arrays + * correctly.</p> + * <p/> + * <p>Multi-dimensional primitive arrays are also handled correctly by this + * method.</p> + * + * @param array1 the left hand array to compare, may be <code>null</code> + * @param array2 the right hand array to compare, may be <code>null</code> + * @return <code>true</code> if the arrays are equal + */ + public static boolean isEquals( Object array1, Object array2 ) + { + return new EqualsBuilder().append( array1, array2 ).isEquals(); + } + + // To map + //----------------------------------------------------------------------- + /** + * <p>Converts the given array into a [EMAIL PROTECTED] java.util.Map}. Each element of + * the array must be either a [EMAIL PROTECTED] 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.</p> + * <p/> + * <p>This method can be used to initialize:</p> + * <pre> + * // Create a Map mapping colors. + * Map colorMap = MapUtils.toMap(new String[][] {{ + * {"RED", "#FF0000"}, + * {"GREEN", "#00FF00"}, + * {"BLUE", "#0000FF"}}); + * </pre> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array an array whose elements are either a [EMAIL PROTECTED] + * java.util.Map.Entry} or an Array containing at least two + * elements, may be <code>null</code> + * @return a <code>Map</code> 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 [EMAIL PROTECTED] java.util.Map.Entry} and an + * Array + */ + public static Map toMap( 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; + } + + // Clone + //----------------------------------------------------------------------- + /** + * <p>Shallow clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>The objects in the array are not cloned, thus there is no special + * handling for multi-dimensional arrays.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to shallow clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static Object[] clone( Object[] array ) + { + if ( array == null ) + { + return null; + } + return ( Object[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static long[] clone( long[] array ) + { + if ( array == null ) + { + return null; + } + return ( long[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static int[] clone( int[] array ) + { + if ( array == null ) + { + return null; + } + return ( int[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static short[] clone( short[] array ) + { + if ( array == null ) + { + return null; + } + return ( short[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static char[] clone( char[] array ) + { + if ( array == null ) + { + return null; + } + return ( char[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static byte[] clone( byte[] array ) + { + if ( array == null ) + { + return null; + } + return ( byte[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static double[] clone( double[] array ) + { + if ( array == null ) + { + return null; + } + return ( double[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static float[] clone( float[] array ) + { + if ( array == null ) + { + return null; + } + return ( float[] ) array.clone(); + } + + + /** + * <p>Clones an array returning a typecast result and handling + * <code>null</code>.</p> + * <p/> + * <p>This method returns <code>null</code> if <code>null</code> array + * input.</p> + * + * @param array the array to clone, may be <code>null</code> + * @return the cloned array, <code>null</code> if <code>null</code> input + */ + public static boolean[] clone( boolean[] array ) + { + if ( array == null ) + { + return null; + } + return ( boolean[] ) array.clone(); + } + + // Subarrays + //----------------------------------------------------------------------- + /** + * <p>Produces a new array containing the elements between the start and end + * indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * <p/> + * <p>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 <code>Date</code>, + * the following usage is envisaged:</p> + * <p/> + * <pre> + * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5); + * </pre> + * + * @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; + } + + + /** + * <p>Produces a new <code>long</code> array containing the elements between + * the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>int</code> array containing the elements between + * the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>short</code> array containing the elements + * between the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>char</code> array containing the elements between + * the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>byte</code> array containing the elements between + * the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>double</code> array containing the elements + * between the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>float</code> array containing the elements + * between the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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; + } + + + /** + * <p>Produces a new <code>boolean</code> array containing the elements + * between the start and end indices.</p> + * <p/> + * <p>The start index is inclusive, the end index exclusive. Null array + * input produces null output.</p> + * + * @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 + //----------------------------------------------------------------------- + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>. + * <p/> + * <p>Any multi-dimensional aspects of the arrays are ignored.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( Object[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( long[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( int[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( short[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( char[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( byte[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( double[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( float[] array1, 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; + } + + + /** + * <p>Checks whether two arrays are the same length, treating + * <code>null</code> arrays as length <code>0</code>.</p> + * + * @param array1 the first array, may be <code>null</code> + * @param array2 the second array, may be <code>null</code> + * @return <code>true</code> if length of arrays matches, treating + * <code>null</code> as an empty array + */ + public static boolean isSameLength( boolean[] array1, 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; + } + + //----------------------------------------------------------------------- + /** + * <p>Returns the length of the specified array. This method can deal with + * <code>Object</code> arrays and with primitive arrays.</p> + * <p/> + * <p>If the input array is <code>null</code>, <code>0</code> is + * returned.</p> + * <p/> + * <pre> + * ArrayUtils.getLength(null) = 0 + * ArrayUtils.getLength([]) = 0 + * ArrayUtils.getLength([null]) = 1 + * ArrayUtils.getLength([true, false]) = 2 + * ArrayUtils.getLength([1, 2, 3]) = 3 + * ArrayUtils.getLength(["a", "b", "c"]) = 3 + * </pre> + * + * @param array the array to retrieve the length from, may be null + * @return The length of the array, or <code>0</code> if the array is + * <code>null</code> + * @throws IllegalArgumentException if the object arguement is not an + * array. + */ + public static int getLength( Object array ) + { + if ( array == null ) + { + return 0; + } + else + { + return Array.getLength( array ); + } + } + + + /** + * Returns the last index of the given array or -1 if empty or null. This + * method can deal with <code>Object</code> arrays and with primitive + * arrays. This value is one less than the size since arrays indices are + * 0-based.</p> + * <p/> + * <pre> + * ArrayUtils.lastIndex(null) = -1 + * ArrayUtils.lastIndex([]) = -1 + * ArrayUtils.lastIndex([null]) = 0 + * ArrayUtils.lastIndex([true, false]) = 1 + * ArrayUtils.lastIndex([1, 2, 3]) = 2 + * ArrayUtils.lastIndex(["a", "b", "c"]) = 2 + * </pre> + * + * @param array the array to return the last index for, may be null + * @return the last index, -1 if empty or null + * @throws IllegalArgumentException if the object arguement is not an + * array. + */ + public static int lastIndex( Object array ) + { + return ArrayUtils.getLength( array ) - 1; + } + + + /** + * <p>Checks whether two arrays are the same type taking into account + * multi-dimensional arrays.</p> + * + * @param array1 the first array, must not be <code>null</code> + * @param array2 the second array, must not be <code>null</code> + * @return <code>true</code> if type of arrays matches + * @throws IllegalArgumentException if either array is <code>null</code> + */ + public static boolean isSameType( Object array1, 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 + //----------------------------------------------------------------------- + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>There is no special handling for multi-dimensional arrays.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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++; + } + } + + + /** + * <p>Reverses the order of the given array.</p> + * <p/> + * <p>This method does nothing if <code>null</code> array input.</p> + * + * @param array the array to reverse, may be <code>null</code> + */ + public static void reverse( 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 + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given object in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param objectToFind the object to find, may be <code>null</code> + * @return the index of the object within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( Object[] array, Object objectToFind ) + { + return indexOf( array, objectToFind, 0 ); + } + + + /** + * <p>Find the index of the given object in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return <code>-1</code>.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param objectToFind the object to find, may be <code>null</code> + * @param startIndex the index to start searching at + * @return the index of the object within the array starting at the index, + * <code>-1</code> if not found or <code>null</code> array input + */ + public static int indexOf( Object[] array, 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; + } + + + /** + * <p>Find the last index of the given object within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the + * object, may be <code>null</code> + * @param objectToFind the object to find, may be <code>null</code> + * @return the last index of the object within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( Object[] array, Object objectToFind ) + { + return lastIndexOf( array, objectToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given object in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return <code>-1</code>. A startIndex larger + * than the array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @param objectToFind the object to find, may be <code>null</code> + * @param startIndex the start index to travers backwards from + * @return the last index of the object within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( Object[] array, 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; + } + + + /** + * <p>Checks if the object is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param objectToFind the object to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( Object[] array, Object objectToFind ) + { + return ( indexOf( array, objectToFind ) != -1 ); + } + + // long IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( long[] array, long valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( long[] array, 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( long[] array, long valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( long[] array, 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; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( long[] array, long valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + // int IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( int[] array, int valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( int[] array, 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( int[] array, int valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( int[] array, 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; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( int[] array, int valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + // short IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( short[] array, short valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( short[] array, 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( short[] array, short valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( short[] array, 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; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( short[] array, short valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + // char IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( char[] array, char valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( char[] array, char 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( char[] array, char valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( char[] array, char 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; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( char[] array, char valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + // byte IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( byte[] array, byte valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( byte[] array, byte 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( byte[] array, byte valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( byte[] array, 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; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( byte[] array, byte valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + // double IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( double[] array, double valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value within a given tolerance in the + * array. This method will return the index of the first value which falls + * between the region defined by valueToFind - tolerance and valueToFind + + * tolerance.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param tolerance tolerance of the search + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( double[] array, double valueToFind, double tolerance ) + { + return indexOf( array, valueToFind, 0, tolerance ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( double[] array, double 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; + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index. This method will return the index of the first value which falls + * between the region defined by valueToFind - tolerance and valueToFind + + * tolerance.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @param tolerance tolerance of the search + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( double[] array, double valueToFind, int startIndex, double tolerance ) + { + if ( ArrayUtils.isEmpty( array ) ) + { + return -1; + } + if ( startIndex < 0 ) + { + startIndex = 0; + } + double min = valueToFind - tolerance; + double max = valueToFind + tolerance; + for ( int i = startIndex; i < array.length; i++ ) + { + if ( array[i] >= min && array[i] <= max ) + { + return i; + } + } + return -1; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( double[] array, double valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value within a given tolerance in the + * array. This method will return the index of the last value which falls + * between the region defined by valueToFind - tolerance and valueToFind + + * tolerance.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param tolerance tolerance of the search + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int lastIndexOf( double[] array, double valueToFind, double tolerance ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE, tolerance ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( double[] array, double 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; + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index. This method will return the index of the last value which + * falls between the region defined by valueToFind - tolerance and + * valueToFind + tolerance.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @param tolerance search for value within plus/minus this amount + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( double[] array, double valueToFind, int startIndex, double tolerance ) + { + if ( ArrayUtils.isEmpty( array ) ) + { + return -1; + } + if ( startIndex < 0 ) + { + return -1; + } + else if ( startIndex >= array.length ) + { + startIndex = array.length - 1; + } + double min = valueToFind - tolerance; + double max = valueToFind + tolerance; + for ( int i = startIndex; i >= 0; i-- ) + { + if ( array[i] >= min && array[i] <= max ) + { + return i; + } + } + return -1; + } + + + /** + * <p>Checks if the value is in the given array.</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @param array the array to search through + * @param valueToFind the value to find + * @return <code>true</code> if the array contains the object + */ + public static boolean contains( double[] array, double valueToFind ) + { + return ( indexOf( array, valueToFind ) != -1 ); + } + + + /** + * <p>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).</p> + * <p/> + * <p>The method returns <code>false</code> if a <code>null</code> array is + * passed in.</p> + * + * @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( double[] array, double valueToFind, double tolerance ) + { + return ( indexOf( array, valueToFind, 0, tolerance ) != -1 ); + } + + // float IndexOf + //----------------------------------------------------------------------- + /** + * <p>Find the index of the given value in the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( float[] array, float valueToFind ) + { + return indexOf( array, valueToFind, 0 ); + } + + + /** + * <p>Find the index of the given value in the array starting at the given + * index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex is treated as zero. A startIndex larger than the + * array length will return -1.</p> + * + * @param array the array to search through for the object, may be + * <code>null</code> + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, <code>-1</code> if not + * found or <code>null</code> array input + */ + public static int indexOf( float[] array, 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; + } + + + /** + * <p>Find the last index of the given value within the array.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * + * @param array the array to travers backwords looking for the object, + * may be <code>null</code> + * @param valueToFind the object to find + * @return the last index of the value within the array, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( float[] array, float valueToFind ) + { + return lastIndexOf( array, valueToFind, Integer.MAX_VALUE ); + } + + + /** + * <p>Find the last index of the given value in the array starting at the + * given index.</p> + * <p/> + * <p>This method returns <code>-1</code> if <code>null</code> array + * input.</p> + * <p/> + * <p>A negative startIndex will return -1. A startIndex larger than the + * array length will search from the end of the array.</p> + * + * @param array the array to traverse for looking for the object, may + * be <code>null</code> + * @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, <code>-1</code> if + * not found or <code>null</code> array input + */ + public static int lastIndexOf( float[] array, float valueToFind, int startIndex ) + { + if ( ArrayUtils.isEmpty( array ) ) + { + return -1; + }
[... 2086 lines stripped ...]
