/org.apache.commons.lang/source-bundle/org/apache/commons/lang/ArrayUtils.java
Java | 4416 lines | 1583 code | 193 blank | 2640 comment | 655 complexity | 15e37024df5cb4109f45a0ea93273c82 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.commons.lang;
- import java.lang.reflect.Array;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- import org.apache.commons.lang.builder.ToStringBuilder;
- import org.apache.commons.lang.builder.ToStringStyle;
- /**
- * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
- * primitive wrapper arrays (like <code>Integer[]</code>).</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 Stephen Colebourne
- * @author Moritz Petersen
- * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
- * @author Nikolay Metchev
- * @author Matthew Hawthorne
- * @author Tim O'Brien
- * @author Pete Gieser
- * @author Gary Gregory
- * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
- * @author Maarten Coene
- * @since 2.0
- * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
- */
- 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];
- /**
- * The index value when an element is not found in a list or array: <code>-1</code>.
- * This value is returned by methods in this class and can also be used in comparisons with values returned by
- * various method from {@link java.util.List}.
- */
- public static final int INDEX_NOT_FOUND = -1;
- /**
- * <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>This constructor is public to permit tools that require a JavaBean instance
- * to operate.</p>
- */
- public ArrayUtils() {
- super();
- }
-
- // Basic methods handling multi-dimensional arrays
- //-----------------------------------------------------------------------
- /**
- * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
- *
- * <p>Multi-dimensional arrays are handled correctly, including
- * multi-dimensional primitive arrays.</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>Multi-dimensional arrays are handled correctly, including
- * multi-dimensional primitive arrays.</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>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>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 {@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.</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>This method returns <code>null</code> for a <code>null</code> input array.</p>
- *
- * @param array an array whose elements are either a {@link 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 {@link 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>The objects in the array are not cloned, thus there is no special
- * handling for multi-dimensional arrays.</p>
- *
- * <p>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>This method returns <code>null</code> for a <code>null</code> input array.</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>The start index is inclusive, the end index exclusive.
- * Null array input produces null output.</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>
- *
- * <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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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.
- * @since 2.1
- */
- 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>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>If the input array is <code>null</code>, <code>0</code> is returned.</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.
- * @since 2.1
- */
- public static int getLength(Object array) {
- if (array == null) {
- return 0;
- }
- return Array.getLength(array);
- }
- /**
- * <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>There is no special handling for multi-dimensional arrays.</p>
- *
- * <p>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>This method does nothing for a <code>null</code> input array.</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>Finds the index of the given object in the array.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</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,
- * {@link #INDEX_NOT_FOUND} (<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>Finds the index of the given object in the array starting at the given index.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} (<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,
- * {@link #INDEX_NOT_FOUND} (<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 INDEX_NOT_FOUND;
- }
- 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 INDEX_NOT_FOUND;
- }
- /**
- * <p>Finds the last index of the given object within the array.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</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,
- * {@link #INDEX_NOT_FOUND} (<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>Finds the last index of the given object in the array starting at the given index.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
- *
- * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<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,
- * {@link #INDEX_NOT_FOUND} (<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 INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- return INDEX_NOT_FOUND;
- } 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 INDEX_NOT_FOUND;
- }
- /**
- * <p>Checks if the object is in the given array.</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) != INDEX_NOT_FOUND;
- }
- // long IndexOf
- //-----------------------------------------------------------------------
- /**
- * <p>Finds the index of the given value in the array.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</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,
- * {@link #INDEX_NOT_FOUND} (<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>Finds the index of the given value in the array starting at the given index.</p>
- *
- * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
- *
- * <p>A negative startIndex is treated as zero. A startIndex larger than the array
- * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</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,
- * {@link #INDEX_NOT_FOUND} (<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 INDEX_NOT_FOUND;
- }
- if (startIndex < 0) {
- startIndex = 0;
- }
- for (int i = startIndex; i < array.length; i++) {
- if (valueToFind == array[i]) {
- return i;
- }
- }
- return INDEX_NOT_FOUND;
- }
- …
Large files files are truncated, but you can click here to view the full file