0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.commons.lang;
0018:
0019: import java.lang.reflect.Array;
0020: import java.util.HashMap;
0021: import java.util.Map;
0022:
0023: import org.apache.commons.lang.builder.EqualsBuilder;
0024: import org.apache.commons.lang.builder.HashCodeBuilder;
0025: import org.apache.commons.lang.builder.ToStringBuilder;
0026: import org.apache.commons.lang.builder.ToStringStyle;
0027:
0028: /**
0029: * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
0030: * primitive wrapper arrays (like <code>Integer[]</code>).</p>
0031: *
0032: * <p>This class tries to handle <code>null</code> input gracefully.
0033: * An exception will not be thrown for a <code>null</code>
0034: * array input. However, an Object array that contains a <code>null</code>
0035: * element may throw an exception. Each method documents its behaviour.</p>
0036: *
0037: * @author Stephen Colebourne
0038: * @author Moritz Petersen
0039: * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
0040: * @author Nikolay Metchev
0041: * @author Matthew Hawthorne
0042: * @author Tim O'Brien
0043: * @author Pete Gieser
0044: * @author Gary Gregory
0045: * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
0046: * @author Maarten Coene
0047: * @since 2.0
0048: * @version $Id: ArrayUtils.java 437554 2006-08-28 06:21:41Z bayard $
0049: */
0050: public class ArrayUtils {
0051:
0052: /**
0053: * An empty immutable <code>Object</code> array.
0054: */
0055: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
0056: /**
0057: * An empty immutable <code>Class</code> array.
0058: */
0059: public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
0060: /**
0061: * An empty immutable <code>String</code> array.
0062: */
0063: public static final String[] EMPTY_STRING_ARRAY = new String[0];
0064: /**
0065: * An empty immutable <code>long</code> array.
0066: */
0067: public static final long[] EMPTY_LONG_ARRAY = new long[0];
0068: /**
0069: * An empty immutable <code>Long</code> array.
0070: */
0071: public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
0072: /**
0073: * An empty immutable <code>int</code> array.
0074: */
0075: public static final int[] EMPTY_INT_ARRAY = new int[0];
0076: /**
0077: * An empty immutable <code>Integer</code> array.
0078: */
0079: public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
0080: /**
0081: * An empty immutable <code>short</code> array.
0082: */
0083: public static final short[] EMPTY_SHORT_ARRAY = new short[0];
0084: /**
0085: * An empty immutable <code>Short</code> array.
0086: */
0087: public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
0088: /**
0089: * An empty immutable <code>byte</code> array.
0090: */
0091: public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
0092: /**
0093: * An empty immutable <code>Byte</code> array.
0094: */
0095: public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
0096: /**
0097: * An empty immutable <code>double</code> array.
0098: */
0099: public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
0100: /**
0101: * An empty immutable <code>Double</code> array.
0102: */
0103: public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
0104: /**
0105: * An empty immutable <code>float</code> array.
0106: */
0107: public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
0108: /**
0109: * An empty immutable <code>Float</code> array.
0110: */
0111: public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
0112: /**
0113: * An empty immutable <code>boolean</code> array.
0114: */
0115: public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
0116: /**
0117: * An empty immutable <code>Boolean</code> array.
0118: */
0119: public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
0120: /**
0121: * An empty immutable <code>char</code> array.
0122: */
0123: public static final char[] EMPTY_CHAR_ARRAY = new char[0];
0124: /**
0125: * An empty immutable <code>Character</code> array.
0126: */
0127: public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
0128:
0129: /**
0130: * The index value when an element is not found in a list or array: <code>-1</code>.
0131: * This value is returned by methods in this class and can also be used in comparisons with values returned by
0132: * various method from {@link java.util.List}.
0133: */
0134: public static final int INDEX_NOT_FOUND = -1;
0135:
0136: /**
0137: * <p>ArrayUtils instances should NOT be constructed in standard programming.
0138: * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.</p>
0139: *
0140: * <p>This constructor is public to permit tools that require a JavaBean instance
0141: * to operate.</p>
0142: */
0143: public ArrayUtils() {
0144: super ();
0145: }
0146:
0147: // Basic methods handling multi-dimensional arrays
0148: //-----------------------------------------------------------------------
0149: /**
0150: * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
0151: *
0152: * <p>Multi-dimensional arrays are handled correctly, including
0153: * multi-dimensional primitive arrays.</p>
0154: *
0155: * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
0156: *
0157: * @param array the array to get a toString for, may be <code>null</code>
0158: * @return a String representation of the array, '{}' if null array input
0159: */
0160: public static String toString(Object array) {
0161: return toString(array, "{}");
0162: }
0163:
0164: /**
0165: * <p>Outputs an array as a String handling <code>null</code>s.</p>
0166: *
0167: * <p>Multi-dimensional arrays are handled correctly, including
0168: * multi-dimensional primitive arrays.</p>
0169: *
0170: * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
0171: *
0172: * @param array the array to get a toString for, may be <code>null</code>
0173: * @param stringIfNull the String to return if the array is <code>null</code>
0174: * @return a String representation of the array
0175: */
0176: public static String toString(Object array, String stringIfNull) {
0177: if (array == null) {
0178: return stringIfNull;
0179: }
0180: return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE)
0181: .append(array).toString();
0182: }
0183:
0184: /**
0185: * <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
0186: *
0187: * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
0188: *
0189: * @param array the array to get a hashCode for, may be <code>null</code>
0190: * @return a hashCode for the array, zero if null array input
0191: */
0192: public static int hashCode(Object array) {
0193: return new HashCodeBuilder().append(array).toHashCode();
0194: }
0195:
0196: /**
0197: * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
0198: * correctly.</p>
0199: *
0200: * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
0201: *
0202: * @param array1 the left hand array to compare, may be <code>null</code>
0203: * @param array2 the right hand array to compare, may be <code>null</code>
0204: * @return <code>true</code> if the arrays are equal
0205: */
0206: public static boolean isEquals(Object array1, Object array2) {
0207: return new EqualsBuilder().append(array1, array2).isEquals();
0208: }
0209:
0210: // To map
0211: //-----------------------------------------------------------------------
0212: /**
0213: * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
0214: * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
0215: * elements, where the first element is used as key and the second as
0216: * value.</p>
0217: *
0218: * <p>This method can be used to initialize:</p>
0219: * <pre>
0220: * // Create a Map mapping colors.
0221: * Map colorMap = MapUtils.toMap(new String[][] {{
0222: * {"RED", "#FF0000"},
0223: * {"GREEN", "#00FF00"},
0224: * {"BLUE", "#0000FF"}});
0225: * </pre>
0226: *
0227: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0228: *
0229: * @param array an array whose elements are either a {@link java.util.Map.Entry} or
0230: * an Array containing at least two elements, may be <code>null</code>
0231: * @return a <code>Map</code> that was created from the array
0232: * @throws IllegalArgumentException if one element of this Array is
0233: * itself an Array containing less then two elements
0234: * @throws IllegalArgumentException if the array contains elements other
0235: * than {@link java.util.Map.Entry} and an Array
0236: */
0237: public static Map toMap(Object[] array) {
0238: if (array == null) {
0239: return null;
0240: }
0241: final Map map = new HashMap((int) (array.length * 1.5));
0242: for (int i = 0; i < array.length; i++) {
0243: Object object = array[i];
0244: if (object instanceof Map.Entry) {
0245: Map.Entry entry = (Map.Entry) object;
0246: map.put(entry.getKey(), entry.getValue());
0247: } else if (object instanceof Object[]) {
0248: Object[] entry = (Object[]) object;
0249: if (entry.length < 2) {
0250: throw new IllegalArgumentException("Array element "
0251: + i + ", '" + object
0252: + "', has a length less than 2");
0253: }
0254: map.put(entry[0], entry[1]);
0255: } else {
0256: throw new IllegalArgumentException(
0257: "Array element "
0258: + i
0259: + ", '"
0260: + object
0261: + "', is neither of type Map.Entry nor an Array");
0262: }
0263: }
0264: return map;
0265: }
0266:
0267: // Clone
0268: //-----------------------------------------------------------------------
0269: /**
0270: * <p>Shallow clones an array returning a typecast result and handling
0271: * <code>null</code>.</p>
0272: *
0273: * <p>The objects in the array are not cloned, thus there is no special
0274: * handling for multi-dimensional arrays.</p>
0275: *
0276: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0277: *
0278: * @param array the array to shallow clone, may be <code>null</code>
0279: * @return the cloned array, <code>null</code> if <code>null</code> input
0280: */
0281: public static Object[] clone(Object[] array) {
0282: if (array == null) {
0283: return null;
0284: }
0285: return (Object[]) array.clone();
0286: }
0287:
0288: /**
0289: * <p>Clones an array returning a typecast result and handling
0290: * <code>null</code>.</p>
0291: *
0292: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0293: *
0294: * @param array the array to clone, may be <code>null</code>
0295: * @return the cloned array, <code>null</code> if <code>null</code> input
0296: */
0297: public static long[] clone(long[] array) {
0298: if (array == null) {
0299: return null;
0300: }
0301: return (long[]) array.clone();
0302: }
0303:
0304: /**
0305: * <p>Clones an array returning a typecast result and handling
0306: * <code>null</code>.</p>
0307: *
0308: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0309: *
0310: * @param array the array to clone, may be <code>null</code>
0311: * @return the cloned array, <code>null</code> if <code>null</code> input
0312: */
0313: public static int[] clone(int[] array) {
0314: if (array == null) {
0315: return null;
0316: }
0317: return (int[]) array.clone();
0318: }
0319:
0320: /**
0321: * <p>Clones an array returning a typecast result and handling
0322: * <code>null</code>.</p>
0323: *
0324: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0325: *
0326: * @param array the array to clone, may be <code>null</code>
0327: * @return the cloned array, <code>null</code> if <code>null</code> input
0328: */
0329: public static short[] clone(short[] array) {
0330: if (array == null) {
0331: return null;
0332: }
0333: return (short[]) array.clone();
0334: }
0335:
0336: /**
0337: * <p>Clones an array returning a typecast result and handling
0338: * <code>null</code>.</p>
0339: *
0340: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0341: *
0342: * @param array the array to clone, may be <code>null</code>
0343: * @return the cloned array, <code>null</code> if <code>null</code> input
0344: */
0345: public static char[] clone(char[] array) {
0346: if (array == null) {
0347: return null;
0348: }
0349: return (char[]) array.clone();
0350: }
0351:
0352: /**
0353: * <p>Clones an array returning a typecast result and handling
0354: * <code>null</code>.</p>
0355: *
0356: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0357: *
0358: * @param array the array to clone, may be <code>null</code>
0359: * @return the cloned array, <code>null</code> if <code>null</code> input
0360: */
0361: public static byte[] clone(byte[] array) {
0362: if (array == null) {
0363: return null;
0364: }
0365: return (byte[]) array.clone();
0366: }
0367:
0368: /**
0369: * <p>Clones an array returning a typecast result and handling
0370: * <code>null</code>.</p>
0371: *
0372: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0373: *
0374: * @param array the array to clone, may be <code>null</code>
0375: * @return the cloned array, <code>null</code> if <code>null</code> input
0376: */
0377: public static double[] clone(double[] array) {
0378: if (array == null) {
0379: return null;
0380: }
0381: return (double[]) array.clone();
0382: }
0383:
0384: /**
0385: * <p>Clones an array returning a typecast result and handling
0386: * <code>null</code>.</p>
0387: *
0388: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0389: *
0390: * @param array the array to clone, may be <code>null</code>
0391: * @return the cloned array, <code>null</code> if <code>null</code> input
0392: */
0393: public static float[] clone(float[] array) {
0394: if (array == null) {
0395: return null;
0396: }
0397: return (float[]) array.clone();
0398: }
0399:
0400: /**
0401: * <p>Clones an array returning a typecast result and handling
0402: * <code>null</code>.</p>
0403: *
0404: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
0405: *
0406: * @param array the array to clone, may be <code>null</code>
0407: * @return the cloned array, <code>null</code> if <code>null</code> input
0408: */
0409: public static boolean[] clone(boolean[] array) {
0410: if (array == null) {
0411: return null;
0412: }
0413: return (boolean[]) array.clone();
0414: }
0415:
0416: // Subarrays
0417: //-----------------------------------------------------------------------
0418: /**
0419: * <p>Produces a new array containing the elements between
0420: * the start and end indices.</p>
0421: *
0422: * <p>The start index is inclusive, the end index exclusive.
0423: * Null array input produces null output.</p>
0424: *
0425: * <p>The component type of the subarray is always the same as
0426: * that of the input array. Thus, if the input is an array of type
0427: * <code>Date</code>, the following usage is envisaged:</p>
0428: *
0429: * <pre>
0430: * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
0431: * </pre>
0432: *
0433: * @param array the array
0434: * @param startIndexInclusive the starting index. Undervalue (<0)
0435: * is promoted to 0, overvalue (>array.length) results
0436: * in an empty array.
0437: * @param endIndexExclusive elements up to endIndex-1 are present in the
0438: * returned subarray. Undervalue (< startIndex) produces
0439: * empty array, overvalue (>array.length) is demoted to
0440: * array length.
0441: * @return a new array containing the elements between
0442: * the start and end indices.
0443: * @since 2.1
0444: */
0445: public static Object[] subarray(Object[] array,
0446: int startIndexInclusive, int endIndexExclusive) {
0447: if (array == null) {
0448: return null;
0449: }
0450: if (startIndexInclusive < 0) {
0451: startIndexInclusive = 0;
0452: }
0453: if (endIndexExclusive > array.length) {
0454: endIndexExclusive = array.length;
0455: }
0456: int newSize = endIndexExclusive - startIndexInclusive;
0457: Class type = array.getClass().getComponentType();
0458: if (newSize <= 0) {
0459: return (Object[]) Array.newInstance(type, 0);
0460: }
0461: Object[] subarray = (Object[]) Array.newInstance(type, newSize);
0462: System.arraycopy(array, startIndexInclusive, subarray, 0,
0463: newSize);
0464: return subarray;
0465: }
0466:
0467: /**
0468: * <p>Produces a new <code>long</code> array containing the elements
0469: * between the start and end indices.</p>
0470: *
0471: * <p>The start index is inclusive, the end index exclusive.
0472: * Null array input produces null output.</p>
0473: *
0474: * @param array the array
0475: * @param startIndexInclusive the starting index. Undervalue (<0)
0476: * is promoted to 0, overvalue (>array.length) results
0477: * in an empty array.
0478: * @param endIndexExclusive elements up to endIndex-1 are present in the
0479: * returned subarray. Undervalue (< startIndex) produces
0480: * empty array, overvalue (>array.length) is demoted to
0481: * array length.
0482: * @return a new array containing the elements between
0483: * the start and end indices.
0484: * @since 2.1
0485: */
0486: public static long[] subarray(long[] array,
0487: int startIndexInclusive, int endIndexExclusive) {
0488: if (array == null) {
0489: return null;
0490: }
0491: if (startIndexInclusive < 0) {
0492: startIndexInclusive = 0;
0493: }
0494: if (endIndexExclusive > array.length) {
0495: endIndexExclusive = array.length;
0496: }
0497: int newSize = endIndexExclusive - startIndexInclusive;
0498: if (newSize <= 0) {
0499: return EMPTY_LONG_ARRAY;
0500: }
0501:
0502: long[] subarray = new long[newSize];
0503: System.arraycopy(array, startIndexInclusive, subarray, 0,
0504: newSize);
0505: return subarray;
0506: }
0507:
0508: /**
0509: * <p>Produces a new <code>int</code> array containing the elements
0510: * between the start and end indices.</p>
0511: *
0512: * <p>The start index is inclusive, the end index exclusive.
0513: * Null array input produces null output.</p>
0514: *
0515: * @param array the array
0516: * @param startIndexInclusive the starting index. Undervalue (<0)
0517: * is promoted to 0, overvalue (>array.length) results
0518: * in an empty array.
0519: * @param endIndexExclusive elements up to endIndex-1 are present in the
0520: * returned subarray. Undervalue (< startIndex) produces
0521: * empty array, overvalue (>array.length) is demoted to
0522: * array length.
0523: * @return a new array containing the elements between
0524: * the start and end indices.
0525: * @since 2.1
0526: */
0527: public static int[] subarray(int[] array, int startIndexInclusive,
0528: int endIndexExclusive) {
0529: if (array == null) {
0530: return null;
0531: }
0532: if (startIndexInclusive < 0) {
0533: startIndexInclusive = 0;
0534: }
0535: if (endIndexExclusive > array.length) {
0536: endIndexExclusive = array.length;
0537: }
0538: int newSize = endIndexExclusive - startIndexInclusive;
0539: if (newSize <= 0) {
0540: return EMPTY_INT_ARRAY;
0541: }
0542:
0543: int[] subarray = new int[newSize];
0544: System.arraycopy(array, startIndexInclusive, subarray, 0,
0545: newSize);
0546: return subarray;
0547: }
0548:
0549: /**
0550: * <p>Produces a new <code>short</code> array containing the elements
0551: * between the start and end indices.</p>
0552: *
0553: * <p>The start index is inclusive, the end index exclusive.
0554: * Null array input produces null output.</p>
0555: *
0556: * @param array the array
0557: * @param startIndexInclusive the starting index. Undervalue (<0)
0558: * is promoted to 0, overvalue (>array.length) results
0559: * in an empty array.
0560: * @param endIndexExclusive elements up to endIndex-1 are present in the
0561: * returned subarray. Undervalue (< startIndex) produces
0562: * empty array, overvalue (>array.length) is demoted to
0563: * array length.
0564: * @return a new array containing the elements between
0565: * the start and end indices.
0566: * @since 2.1
0567: */
0568: public static short[] subarray(short[] array,
0569: int startIndexInclusive, int endIndexExclusive) {
0570: if (array == null) {
0571: return null;
0572: }
0573: if (startIndexInclusive < 0) {
0574: startIndexInclusive = 0;
0575: }
0576: if (endIndexExclusive > array.length) {
0577: endIndexExclusive = array.length;
0578: }
0579: int newSize = endIndexExclusive - startIndexInclusive;
0580: if (newSize <= 0) {
0581: return EMPTY_SHORT_ARRAY;
0582: }
0583:
0584: short[] subarray = new short[newSize];
0585: System.arraycopy(array, startIndexInclusive, subarray, 0,
0586: newSize);
0587: return subarray;
0588: }
0589:
0590: /**
0591: * <p>Produces a new <code>char</code> array containing the elements
0592: * between the start and end indices.</p>
0593: *
0594: * <p>The start index is inclusive, the end index exclusive.
0595: * Null array input produces null output.</p>
0596: *
0597: * @param array the array
0598: * @param startIndexInclusive the starting index. Undervalue (<0)
0599: * is promoted to 0, overvalue (>array.length) results
0600: * in an empty array.
0601: * @param endIndexExclusive elements up to endIndex-1 are present in the
0602: * returned subarray. Undervalue (< startIndex) produces
0603: * empty array, overvalue (>array.length) is demoted to
0604: * array length.
0605: * @return a new array containing the elements between
0606: * the start and end indices.
0607: * @since 2.1
0608: */
0609: public static char[] subarray(char[] array,
0610: int startIndexInclusive, int endIndexExclusive) {
0611: if (array == null) {
0612: return null;
0613: }
0614: if (startIndexInclusive < 0) {
0615: startIndexInclusive = 0;
0616: }
0617: if (endIndexExclusive > array.length) {
0618: endIndexExclusive = array.length;
0619: }
0620: int newSize = endIndexExclusive - startIndexInclusive;
0621: if (newSize <= 0) {
0622: return EMPTY_CHAR_ARRAY;
0623: }
0624:
0625: char[] subarray = new char[newSize];
0626: System.arraycopy(array, startIndexInclusive, subarray, 0,
0627: newSize);
0628: return subarray;
0629: }
0630:
0631: /**
0632: * <p>Produces a new <code>byte</code> array containing the elements
0633: * between the start and end indices.</p>
0634: *
0635: * <p>The start index is inclusive, the end index exclusive.
0636: * Null array input produces null output.</p>
0637: *
0638: * @param array the array
0639: * @param startIndexInclusive the starting index. Undervalue (<0)
0640: * is promoted to 0, overvalue (>array.length) results
0641: * in an empty array.
0642: * @param endIndexExclusive elements up to endIndex-1 are present in the
0643: * returned subarray. Undervalue (< startIndex) produces
0644: * empty array, overvalue (>array.length) is demoted to
0645: * array length.
0646: * @return a new array containing the elements between
0647: * the start and end indices.
0648: * @since 2.1
0649: */
0650: public static byte[] subarray(byte[] array,
0651: int startIndexInclusive, int endIndexExclusive) {
0652: if (array == null) {
0653: return null;
0654: }
0655: if (startIndexInclusive < 0) {
0656: startIndexInclusive = 0;
0657: }
0658: if (endIndexExclusive > array.length) {
0659: endIndexExclusive = array.length;
0660: }
0661: int newSize = endIndexExclusive - startIndexInclusive;
0662: if (newSize <= 0) {
0663: return EMPTY_BYTE_ARRAY;
0664: }
0665:
0666: byte[] subarray = new byte[newSize];
0667: System.arraycopy(array, startIndexInclusive, subarray, 0,
0668: newSize);
0669: return subarray;
0670: }
0671:
0672: /**
0673: * <p>Produces a new <code>double</code> array containing the elements
0674: * between the start and end indices.</p>
0675: *
0676: * <p>The start index is inclusive, the end index exclusive.
0677: * Null array input produces null output.</p>
0678: *
0679: * @param array the array
0680: * @param startIndexInclusive the starting index. Undervalue (<0)
0681: * is promoted to 0, overvalue (>array.length) results
0682: * in an empty array.
0683: * @param endIndexExclusive elements up to endIndex-1 are present in the
0684: * returned subarray. Undervalue (< startIndex) produces
0685: * empty array, overvalue (>array.length) is demoted to
0686: * array length.
0687: * @return a new array containing the elements between
0688: * the start and end indices.
0689: * @since 2.1
0690: */
0691: public static double[] subarray(double[] array,
0692: int startIndexInclusive, int endIndexExclusive) {
0693: if (array == null) {
0694: return null;
0695: }
0696: if (startIndexInclusive < 0) {
0697: startIndexInclusive = 0;
0698: }
0699: if (endIndexExclusive > array.length) {
0700: endIndexExclusive = array.length;
0701: }
0702: int newSize = endIndexExclusive - startIndexInclusive;
0703: if (newSize <= 0) {
0704: return EMPTY_DOUBLE_ARRAY;
0705: }
0706:
0707: double[] subarray = new double[newSize];
0708: System.arraycopy(array, startIndexInclusive, subarray, 0,
0709: newSize);
0710: return subarray;
0711: }
0712:
0713: /**
0714: * <p>Produces a new <code>float</code> array containing the elements
0715: * between the start and end indices.</p>
0716: *
0717: * <p>The start index is inclusive, the end index exclusive.
0718: * Null array input produces null output.</p>
0719: *
0720: * @param array the array
0721: * @param startIndexInclusive the starting index. Undervalue (<0)
0722: * is promoted to 0, overvalue (>array.length) results
0723: * in an empty array.
0724: * @param endIndexExclusive elements up to endIndex-1 are present in the
0725: * returned subarray. Undervalue (< startIndex) produces
0726: * empty array, overvalue (>array.length) is demoted to
0727: * array length.
0728: * @return a new array containing the elements between
0729: * the start and end indices.
0730: * @since 2.1
0731: */
0732: public static float[] subarray(float[] array,
0733: int startIndexInclusive, int endIndexExclusive) {
0734: if (array == null) {
0735: return null;
0736: }
0737: if (startIndexInclusive < 0) {
0738: startIndexInclusive = 0;
0739: }
0740: if (endIndexExclusive > array.length) {
0741: endIndexExclusive = array.length;
0742: }
0743: int newSize = endIndexExclusive - startIndexInclusive;
0744: if (newSize <= 0) {
0745: return EMPTY_FLOAT_ARRAY;
0746: }
0747:
0748: float[] subarray = new float[newSize];
0749: System.arraycopy(array, startIndexInclusive, subarray, 0,
0750: newSize);
0751: return subarray;
0752: }
0753:
0754: /**
0755: * <p>Produces a new <code>boolean</code> array containing the elements
0756: * between the start and end indices.</p>
0757: *
0758: * <p>The start index is inclusive, the end index exclusive.
0759: * Null array input produces null output.</p>
0760: *
0761: * @param array the array
0762: * @param startIndexInclusive the starting index. Undervalue (<0)
0763: * is promoted to 0, overvalue (>array.length) results
0764: * in an empty array.
0765: * @param endIndexExclusive elements up to endIndex-1 are present in the
0766: * returned subarray. Undervalue (< startIndex) produces
0767: * empty array, overvalue (>array.length) is demoted to
0768: * array length.
0769: * @return a new array containing the elements between
0770: * the start and end indices.
0771: * @since 2.1
0772: */
0773: public static boolean[] subarray(boolean[] array,
0774: int startIndexInclusive, int endIndexExclusive) {
0775: if (array == null) {
0776: return null;
0777: }
0778: if (startIndexInclusive < 0) {
0779: startIndexInclusive = 0;
0780: }
0781: if (endIndexExclusive > array.length) {
0782: endIndexExclusive = array.length;
0783: }
0784: int newSize = endIndexExclusive - startIndexInclusive;
0785: if (newSize <= 0) {
0786: return EMPTY_BOOLEAN_ARRAY;
0787: }
0788:
0789: boolean[] subarray = new boolean[newSize];
0790: System.arraycopy(array, startIndexInclusive, subarray, 0,
0791: newSize);
0792: return subarray;
0793: }
0794:
0795: // Is same length
0796: //-----------------------------------------------------------------------
0797: /**
0798: * <p>Checks whether two arrays are the same length, treating
0799: * <code>null</code> arrays as length <code>0</code>.
0800: *
0801: * <p>Any multi-dimensional aspects of the arrays are ignored.</p>
0802: *
0803: * @param array1 the first array, may be <code>null</code>
0804: * @param array2 the second array, may be <code>null</code>
0805: * @return <code>true</code> if length of arrays matches, treating
0806: * <code>null</code> as an empty array
0807: */
0808: public static boolean isSameLength(Object[] array1, Object[] array2) {
0809: if ((array1 == null && array2 != null && array2.length > 0)
0810: || (array2 == null && array1 != null && array1.length > 0)
0811: || (array1 != null && array2 != null && array1.length != array2.length)) {
0812: return false;
0813: }
0814: return true;
0815: }
0816:
0817: /**
0818: * <p>Checks whether two arrays are the same length, treating
0819: * <code>null</code> arrays as length <code>0</code>.</p>
0820: *
0821: * @param array1 the first array, may be <code>null</code>
0822: * @param array2 the second array, may be <code>null</code>
0823: * @return <code>true</code> if length of arrays matches, treating
0824: * <code>null</code> as an empty array
0825: */
0826: public static boolean isSameLength(long[] array1, long[] array2) {
0827: if ((array1 == null && array2 != null && array2.length > 0)
0828: || (array2 == null && array1 != null && array1.length > 0)
0829: || (array1 != null && array2 != null && array1.length != array2.length)) {
0830: return false;
0831: }
0832: return true;
0833: }
0834:
0835: /**
0836: * <p>Checks whether two arrays are the same length, treating
0837: * <code>null</code> arrays as length <code>0</code>.</p>
0838: *
0839: * @param array1 the first array, may be <code>null</code>
0840: * @param array2 the second array, may be <code>null</code>
0841: * @return <code>true</code> if length of arrays matches, treating
0842: * <code>null</code> as an empty array
0843: */
0844: public static boolean isSameLength(int[] array1, int[] array2) {
0845: if ((array1 == null && array2 != null && array2.length > 0)
0846: || (array2 == null && array1 != null && array1.length > 0)
0847: || (array1 != null && array2 != null && array1.length != array2.length)) {
0848: return false;
0849: }
0850: return true;
0851: }
0852:
0853: /**
0854: * <p>Checks whether two arrays are the same length, treating
0855: * <code>null</code> arrays as length <code>0</code>.</p>
0856: *
0857: * @param array1 the first array, may be <code>null</code>
0858: * @param array2 the second array, may be <code>null</code>
0859: * @return <code>true</code> if length of arrays matches, treating
0860: * <code>null</code> as an empty array
0861: */
0862: public static boolean isSameLength(short[] array1, short[] array2) {
0863: if ((array1 == null && array2 != null && array2.length > 0)
0864: || (array2 == null && array1 != null && array1.length > 0)
0865: || (array1 != null && array2 != null && array1.length != array2.length)) {
0866: return false;
0867: }
0868: return true;
0869: }
0870:
0871: /**
0872: * <p>Checks whether two arrays are the same length, treating
0873: * <code>null</code> arrays as length <code>0</code>.</p>
0874: *
0875: * @param array1 the first array, may be <code>null</code>
0876: * @param array2 the second array, may be <code>null</code>
0877: * @return <code>true</code> if length of arrays matches, treating
0878: * <code>null</code> as an empty array
0879: */
0880: public static boolean isSameLength(char[] array1, char[] array2) {
0881: if ((array1 == null && array2 != null && array2.length > 0)
0882: || (array2 == null && array1 != null && array1.length > 0)
0883: || (array1 != null && array2 != null && array1.length != array2.length)) {
0884: return false;
0885: }
0886: return true;
0887: }
0888:
0889: /**
0890: * <p>Checks whether two arrays are the same length, treating
0891: * <code>null</code> arrays as length <code>0</code>.</p>
0892: *
0893: * @param array1 the first array, may be <code>null</code>
0894: * @param array2 the second array, may be <code>null</code>
0895: * @return <code>true</code> if length of arrays matches, treating
0896: * <code>null</code> as an empty array
0897: */
0898: public static boolean isSameLength(byte[] array1, byte[] array2) {
0899: if ((array1 == null && array2 != null && array2.length > 0)
0900: || (array2 == null && array1 != null && array1.length > 0)
0901: || (array1 != null && array2 != null && array1.length != array2.length)) {
0902: return false;
0903: }
0904: return true;
0905: }
0906:
0907: /**
0908: * <p>Checks whether two arrays are the same length, treating
0909: * <code>null</code> arrays as length <code>0</code>.</p>
0910: *
0911: * @param array1 the first array, may be <code>null</code>
0912: * @param array2 the second array, may be <code>null</code>
0913: * @return <code>true</code> if length of arrays matches, treating
0914: * <code>null</code> as an empty array
0915: */
0916: public static boolean isSameLength(double[] array1, double[] array2) {
0917: if ((array1 == null && array2 != null && array2.length > 0)
0918: || (array2 == null && array1 != null && array1.length > 0)
0919: || (array1 != null && array2 != null && array1.length != array2.length)) {
0920: return false;
0921: }
0922: return true;
0923: }
0924:
0925: /**
0926: * <p>Checks whether two arrays are the same length, treating
0927: * <code>null</code> arrays as length <code>0</code>.</p>
0928: *
0929: * @param array1 the first array, may be <code>null</code>
0930: * @param array2 the second array, may be <code>null</code>
0931: * @return <code>true</code> if length of arrays matches, treating
0932: * <code>null</code> as an empty array
0933: */
0934: public static boolean isSameLength(float[] array1, float[] array2) {
0935: if ((array1 == null && array2 != null && array2.length > 0)
0936: || (array2 == null && array1 != null && array1.length > 0)
0937: || (array1 != null && array2 != null && array1.length != array2.length)) {
0938: return false;
0939: }
0940: return true;
0941: }
0942:
0943: /**
0944: * <p>Checks whether two arrays are the same length, treating
0945: * <code>null</code> arrays as length <code>0</code>.</p>
0946: *
0947: * @param array1 the first array, may be <code>null</code>
0948: * @param array2 the second array, may be <code>null</code>
0949: * @return <code>true</code> if length of arrays matches, treating
0950: * <code>null</code> as an empty array
0951: */
0952: public static boolean isSameLength(boolean[] array1,
0953: boolean[] array2) {
0954: if ((array1 == null && array2 != null && array2.length > 0)
0955: || (array2 == null && array1 != null && array1.length > 0)
0956: || (array1 != null && array2 != null && array1.length != array2.length)) {
0957: return false;
0958: }
0959: return true;
0960: }
0961:
0962: //-----------------------------------------------------------------------
0963: /**
0964: * <p>Returns the length of the specified array.
0965: * This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
0966: *
0967: * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p>
0968: *
0969: * <pre>
0970: * ArrayUtils.getLength(null) = 0
0971: * ArrayUtils.getLength([]) = 0
0972: * ArrayUtils.getLength([null]) = 1
0973: * ArrayUtils.getLength([true, false]) = 2
0974: * ArrayUtils.getLength([1, 2, 3]) = 3
0975: * ArrayUtils.getLength(["a", "b", "c"]) = 3
0976: * </pre>
0977: *
0978: * @param array the array to retrieve the length from, may be null
0979: * @return The length of the array, or <code>0</code> if the array is <code>null</code>
0980: * @throws IllegalArgumentException if the object arguement is not an array.
0981: * @since 2.1
0982: */
0983: public static int getLength(Object array) {
0984: if (array == null) {
0985: return 0;
0986: }
0987: return Array.getLength(array);
0988: }
0989:
0990: /**
0991: * <p>Checks whether two arrays are the same type taking into account
0992: * multi-dimensional arrays.</p>
0993: *
0994: * @param array1 the first array, must not be <code>null</code>
0995: * @param array2 the second array, must not be <code>null</code>
0996: * @return <code>true</code> if type of arrays matches
0997: * @throws IllegalArgumentException if either array is <code>null</code>
0998: */
0999: public static boolean isSameType(Object array1, Object array2) {
1000: if (array1 == null || array2 == null) {
1001: throw new IllegalArgumentException(
1002: "The Array must not be null");
1003: }
1004: return array1.getClass().getName().equals(
1005: array2.getClass().getName());
1006: }
1007:
1008: // Reverse
1009: //-----------------------------------------------------------------------
1010: /**
1011: * <p>Reverses the order of the given array.</p>
1012: *
1013: * <p>There is no special handling for multi-dimensional arrays.</p>
1014: *
1015: * <p>This method does nothing for a <code>null</code> input array.</p>
1016: *
1017: * @param array the array to reverse, may be <code>null</code>
1018: */
1019: public static void reverse(Object[] array) {
1020: if (array == null) {
1021: return;
1022: }
1023: int i = 0;
1024: int j = array.length - 1;
1025: Object tmp;
1026: while (j > i) {
1027: tmp = array[j];
1028: array[j] = array[i];
1029: array[i] = tmp;
1030: j--;
1031: i++;
1032: }
1033: }
1034:
1035: /**
1036: * <p>Reverses the order of the given array.</p>
1037: *
1038: * <p>This method does nothing for a <code>null</code> input array.</p>
1039: *
1040: * @param array the array to reverse, may be <code>null</code>
1041: */
1042: public static void reverse(long[] array) {
1043: if (array == null) {
1044: return;
1045: }
1046: int i = 0;
1047: int j = array.length - 1;
1048: long tmp;
1049: while (j > i) {
1050: tmp = array[j];
1051: array[j] = array[i];
1052: array[i] = tmp;
1053: j--;
1054: i++;
1055: }
1056: }
1057:
1058: /**
1059: * <p>Reverses the order of the given array.</p>
1060: *
1061: * <p>This method does nothing for a <code>null</code> input array.</p>
1062: *
1063: * @param array the array to reverse, may be <code>null</code>
1064: */
1065: public static void reverse(int[] array) {
1066: if (array == null) {
1067: return;
1068: }
1069: int i = 0;
1070: int j = array.length - 1;
1071: int tmp;
1072: while (j > i) {
1073: tmp = array[j];
1074: array[j] = array[i];
1075: array[i] = tmp;
1076: j--;
1077: i++;
1078: }
1079: }
1080:
1081: /**
1082: * <p>Reverses the order of the given array.</p>
1083: *
1084: * <p>This method does nothing for a <code>null</code> input array.</p>
1085: *
1086: * @param array the array to reverse, may be <code>null</code>
1087: */
1088: public static void reverse(short[] array) {
1089: if (array == null) {
1090: return;
1091: }
1092: int i = 0;
1093: int j = array.length - 1;
1094: short tmp;
1095: while (j > i) {
1096: tmp = array[j];
1097: array[j] = array[i];
1098: array[i] = tmp;
1099: j--;
1100: i++;
1101: }
1102: }
1103:
1104: /**
1105: * <p>Reverses the order of the given array.</p>
1106: *
1107: * <p>This method does nothing for a <code>null</code> input array.</p>
1108: *
1109: * @param array the array to reverse, may be <code>null</code>
1110: */
1111: public static void reverse(char[] array) {
1112: if (array == null) {
1113: return;
1114: }
1115: int i = 0;
1116: int j = array.length - 1;
1117: char tmp;
1118: while (j > i) {
1119: tmp = array[j];
1120: array[j] = array[i];
1121: array[i] = tmp;
1122: j--;
1123: i++;
1124: }
1125: }
1126:
1127: /**
1128: * <p>Reverses the order of the given array.</p>
1129: *
1130: * <p>This method does nothing for a <code>null</code> input array.</p>
1131: *
1132: * @param array the array to reverse, may be <code>null</code>
1133: */
1134: public static void reverse(byte[] array) {
1135: if (array == null) {
1136: return;
1137: }
1138: int i = 0;
1139: int j = array.length - 1;
1140: byte tmp;
1141: while (j > i) {
1142: tmp = array[j];
1143: array[j] = array[i];
1144: array[i] = tmp;
1145: j--;
1146: i++;
1147: }
1148: }
1149:
1150: /**
1151: * <p>Reverses the order of the given array.</p>
1152: *
1153: * <p>This method does nothing for a <code>null</code> input array.</p>
1154: *
1155: * @param array the array to reverse, may be <code>null</code>
1156: */
1157: public static void reverse(double[] array) {
1158: if (array == null) {
1159: return;
1160: }
1161: int i = 0;
1162: int j = array.length - 1;
1163: double tmp;
1164: while (j > i) {
1165: tmp = array[j];
1166: array[j] = array[i];
1167: array[i] = tmp;
1168: j--;
1169: i++;
1170: }
1171: }
1172:
1173: /**
1174: * <p>Reverses the order of the given array.</p>
1175: *
1176: * <p>This method does nothing for a <code>null</code> input array.</p>
1177: *
1178: * @param array the array to reverse, may be <code>null</code>
1179: */
1180: public static void reverse(float[] array) {
1181: if (array == null) {
1182: return;
1183: }
1184: int i = 0;
1185: int j = array.length - 1;
1186: float tmp;
1187: while (j > i) {
1188: tmp = array[j];
1189: array[j] = array[i];
1190: array[i] = tmp;
1191: j--;
1192: i++;
1193: }
1194: }
1195:
1196: /**
1197: * <p>Reverses the order of the given array.</p>
1198: *
1199: * <p>This method does nothing for a <code>null</code> input array.</p>
1200: *
1201: * @param array the array to reverse, may be <code>null</code>
1202: */
1203: public static void reverse(boolean[] array) {
1204: if (array == null) {
1205: return;
1206: }
1207: int i = 0;
1208: int j = array.length - 1;
1209: boolean tmp;
1210: while (j > i) {
1211: tmp = array[j];
1212: array[j] = array[i];
1213: array[i] = tmp;
1214: j--;
1215: i++;
1216: }
1217: }
1218:
1219: // IndexOf search
1220: // ----------------------------------------------------------------------
1221:
1222: // Object IndexOf
1223: //-----------------------------------------------------------------------
1224: /**
1225: * <p>Finds the index of the given object in the array.</p>
1226: *
1227: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1228: *
1229: * @param array the array to search through for the object, may be <code>null</code>
1230: * @param objectToFind the object to find, may be <code>null</code>
1231: * @return the index of the object within the array,
1232: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1233: */
1234: public static int indexOf(Object[] array, Object objectToFind) {
1235: return indexOf(array, objectToFind, 0);
1236: }
1237:
1238: /**
1239: * <p>Finds the index of the given object in the array starting at the given index.</p>
1240: *
1241: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1242: *
1243: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1244: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1245: *
1246: * @param array the array to search through for the object, may be <code>null</code>
1247: * @param objectToFind the object to find, may be <code>null</code>
1248: * @param startIndex the index to start searching at
1249: * @return the index of the object within the array starting at the index,
1250: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1251: */
1252: public static int indexOf(Object[] array, Object objectToFind,
1253: int startIndex) {
1254: if (array == null) {
1255: return INDEX_NOT_FOUND;
1256: }
1257: if (startIndex < 0) {
1258: startIndex = 0;
1259: }
1260: if (objectToFind == null) {
1261: for (int i = startIndex; i < array.length; i++) {
1262: if (array[i] == null) {
1263: return i;
1264: }
1265: }
1266: } else {
1267: for (int i = startIndex; i < array.length; i++) {
1268: if (objectToFind.equals(array[i])) {
1269: return i;
1270: }
1271: }
1272: }
1273: return INDEX_NOT_FOUND;
1274: }
1275:
1276: /**
1277: * <p>Finds the last index of the given object within the array.</p>
1278: *
1279: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1280: *
1281: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1282: * @param objectToFind the object to find, may be <code>null</code>
1283: * @return the last index of the object within the array,
1284: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1285: */
1286: public static int lastIndexOf(Object[] array, Object objectToFind) {
1287: return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
1288: }
1289:
1290: /**
1291: * <p>Finds the last index of the given object in the array starting at the given index.</p>
1292: *
1293: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1294: *
1295: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than
1296: * the array length will search from the end of the array.</p>
1297: *
1298: * @param array the array to traverse for looking for the object, may be <code>null</code>
1299: * @param objectToFind the object to find, may be <code>null</code>
1300: * @param startIndex the start index to travers backwards from
1301: * @return the last index of the object within the array,
1302: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1303: */
1304: public static int lastIndexOf(Object[] array, Object objectToFind,
1305: int startIndex) {
1306: if (array == null) {
1307: return INDEX_NOT_FOUND;
1308: }
1309: if (startIndex < 0) {
1310: return INDEX_NOT_FOUND;
1311: } else if (startIndex >= array.length) {
1312: startIndex = array.length - 1;
1313: }
1314: if (objectToFind == null) {
1315: for (int i = startIndex; i >= 0; i--) {
1316: if (array[i] == null) {
1317: return i;
1318: }
1319: }
1320: } else {
1321: for (int i = startIndex; i >= 0; i--) {
1322: if (objectToFind.equals(array[i])) {
1323: return i;
1324: }
1325: }
1326: }
1327: return INDEX_NOT_FOUND;
1328: }
1329:
1330: /**
1331: * <p>Checks if the object is in the given array.</p>
1332: *
1333: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1334: *
1335: * @param array the array to search through
1336: * @param objectToFind the object to find
1337: * @return <code>true</code> if the array contains the object
1338: */
1339: public static boolean contains(Object[] array, Object objectToFind) {
1340: return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
1341: }
1342:
1343: // long IndexOf
1344: //-----------------------------------------------------------------------
1345: /**
1346: * <p>Finds the index of the given value in the array.</p>
1347: *
1348: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1349: *
1350: * @param array the array to search through for the object, may be <code>null</code>
1351: * @param valueToFind the value to find
1352: * @return the index of the value within the array,
1353: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1354: */
1355: public static int indexOf(long[] array, long valueToFind) {
1356: return indexOf(array, valueToFind, 0);
1357: }
1358:
1359: /**
1360: * <p>Finds the index of the given value in the array starting at the given index.</p>
1361: *
1362: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1363: *
1364: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1365: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1366: *
1367: * @param array the array to search through for the object, may be <code>null</code>
1368: * @param valueToFind the value to find
1369: * @param startIndex the index to start searching at
1370: * @return the index of the value within the array,
1371: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1372: */
1373: public static int indexOf(long[] array, long valueToFind,
1374: int startIndex) {
1375: if (array == null) {
1376: return INDEX_NOT_FOUND;
1377: }
1378: if (startIndex < 0) {
1379: startIndex = 0;
1380: }
1381: for (int i = startIndex; i < array.length; i++) {
1382: if (valueToFind == array[i]) {
1383: return i;
1384: }
1385: }
1386: return INDEX_NOT_FOUND;
1387: }
1388:
1389: /**
1390: * <p>Finds the last index of the given value within the array.</p>
1391: *
1392: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1393: *
1394: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1395: * @param valueToFind the object to find
1396: * @return the last index of the value within the array,
1397: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1398: */
1399: public static int lastIndexOf(long[] array, long valueToFind) {
1400: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1401: }
1402:
1403: /**
1404: * <p>Finds the last index of the given value in the array starting at the given index.</p>
1405: *
1406: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1407: *
1408: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
1409: * array length will search from the end of the array.</p>
1410: *
1411: * @param array the array to traverse for looking for the object, may be <code>null</code>
1412: * @param valueToFind the value to find
1413: * @param startIndex the start index to travers backwards from
1414: * @return the last index of the value within the array,
1415: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1416: */
1417: public static int lastIndexOf(long[] array, long valueToFind,
1418: int startIndex) {
1419: if (array == null) {
1420: return INDEX_NOT_FOUND;
1421: }
1422: if (startIndex < 0) {
1423: return INDEX_NOT_FOUND;
1424: } else if (startIndex >= array.length) {
1425: startIndex = array.length - 1;
1426: }
1427: for (int i = startIndex; i >= 0; i--) {
1428: if (valueToFind == array[i]) {
1429: return i;
1430: }
1431: }
1432: return INDEX_NOT_FOUND;
1433: }
1434:
1435: /**
1436: * <p>Checks if the value is in the given array.</p>
1437: *
1438: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1439: *
1440: * @param array the array to search through
1441: * @param valueToFind the value to find
1442: * @return <code>true</code> if the array contains the object
1443: */
1444: public static boolean contains(long[] array, long valueToFind) {
1445: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
1446: }
1447:
1448: // int IndexOf
1449: //-----------------------------------------------------------------------
1450: /**
1451: * <p>Finds the index of the given value in the array.</p>
1452: *
1453: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1454: *
1455: * @param array the array to search through for the object, may be <code>null</code>
1456: * @param valueToFind the value to find
1457: * @return the index of the value within the array,
1458: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1459: */
1460: public static int indexOf(int[] array, int valueToFind) {
1461: return indexOf(array, valueToFind, 0);
1462: }
1463:
1464: /**
1465: * <p>Finds the index of the given value in the array starting at the given index.</p>
1466: *
1467: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1468: *
1469: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1470: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1471: *
1472: * @param array the array to search through for the object, may be <code>null</code>
1473: * @param valueToFind the value to find
1474: * @param startIndex the index to start searching at
1475: * @return the index of the value within the array,
1476: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1477: */
1478: public static int indexOf(int[] array, int valueToFind,
1479: int startIndex) {
1480: if (array == null) {
1481: return INDEX_NOT_FOUND;
1482: }
1483: if (startIndex < 0) {
1484: startIndex = 0;
1485: }
1486: for (int i = startIndex; i < array.length; i++) {
1487: if (valueToFind == array[i]) {
1488: return i;
1489: }
1490: }
1491: return INDEX_NOT_FOUND;
1492: }
1493:
1494: /**
1495: * <p>Finds the last index of the given value within the array.</p>
1496: *
1497: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1498: *
1499: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1500: * @param valueToFind the object to find
1501: * @return the last index of the value within the array,
1502: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1503: */
1504: public static int lastIndexOf(int[] array, int valueToFind) {
1505: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1506: }
1507:
1508: /**
1509: * <p>Finds the last index of the given value in the array starting at the given index.</p>
1510: *
1511: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1512: *
1513: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
1514: * array length will search from the end of the array.</p>
1515: *
1516: * @param array the array to traverse for looking for the object, may be <code>null</code>
1517: * @param valueToFind the value to find
1518: * @param startIndex the start index to travers backwards from
1519: * @return the last index of the value within the array,
1520: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1521: */
1522: public static int lastIndexOf(int[] array, int valueToFind,
1523: int startIndex) {
1524: if (array == null) {
1525: return INDEX_NOT_FOUND;
1526: }
1527: if (startIndex < 0) {
1528: return INDEX_NOT_FOUND;
1529: } else if (startIndex >= array.length) {
1530: startIndex = array.length - 1;
1531: }
1532: for (int i = startIndex; i >= 0; i--) {
1533: if (valueToFind == array[i]) {
1534: return i;
1535: }
1536: }
1537: return INDEX_NOT_FOUND;
1538: }
1539:
1540: /**
1541: * <p>Checks if the value is in the given array.</p>
1542: *
1543: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1544: *
1545: * @param array the array to search through
1546: * @param valueToFind the value to find
1547: * @return <code>true</code> if the array contains the object
1548: */
1549: public static boolean contains(int[] array, int valueToFind) {
1550: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
1551: }
1552:
1553: // short IndexOf
1554: //-----------------------------------------------------------------------
1555: /**
1556: * <p>Finds the index of the given value in the array.</p>
1557: *
1558: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1559: *
1560: * @param array the array to search through for the object, may be <code>null</code>
1561: * @param valueToFind the value to find
1562: * @return the index of the value within the array,
1563: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1564: */
1565: public static int indexOf(short[] array, short valueToFind) {
1566: return indexOf(array, valueToFind, 0);
1567: }
1568:
1569: /**
1570: * <p>Finds the index of the given value in the array starting at the given index.</p>
1571: *
1572: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1573: *
1574: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1575: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1576: *
1577: * @param array the array to search through for the object, may be <code>null</code>
1578: * @param valueToFind the value to find
1579: * @param startIndex the index to start searching at
1580: * @return the index of the value within the array,
1581: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1582: */
1583: public static int indexOf(short[] array, short valueToFind,
1584: int startIndex) {
1585: if (array == null) {
1586: return INDEX_NOT_FOUND;
1587: }
1588: if (startIndex < 0) {
1589: startIndex = 0;
1590: }
1591: for (int i = startIndex; i < array.length; i++) {
1592: if (valueToFind == array[i]) {
1593: return i;
1594: }
1595: }
1596: return INDEX_NOT_FOUND;
1597: }
1598:
1599: /**
1600: * <p>Finds the last index of the given value within the array.</p>
1601: *
1602: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1603: *
1604: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1605: * @param valueToFind the object to find
1606: * @return the last index of the value within the array,
1607: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1608: */
1609: public static int lastIndexOf(short[] array, short valueToFind) {
1610: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1611: }
1612:
1613: /**
1614: * <p>Finds the last index of the given value in the array starting at the given index.</p>
1615: *
1616: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1617: *
1618: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
1619: * array length will search from the end of the array.</p>
1620: *
1621: * @param array the array to traverse for looking for the object, may be <code>null</code>
1622: * @param valueToFind the value to find
1623: * @param startIndex the start index to travers backwards from
1624: * @return the last index of the value within the array,
1625: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1626: */
1627: public static int lastIndexOf(short[] array, short valueToFind,
1628: int startIndex) {
1629: if (array == null) {
1630: return INDEX_NOT_FOUND;
1631: }
1632: if (startIndex < 0) {
1633: return INDEX_NOT_FOUND;
1634: } else if (startIndex >= array.length) {
1635: startIndex = array.length - 1;
1636: }
1637: for (int i = startIndex; i >= 0; i--) {
1638: if (valueToFind == array[i]) {
1639: return i;
1640: }
1641: }
1642: return INDEX_NOT_FOUND;
1643: }
1644:
1645: /**
1646: * <p>Checks if the value is in the given array.</p>
1647: *
1648: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1649: *
1650: * @param array the array to search through
1651: * @param valueToFind the value to find
1652: * @return <code>true</code> if the array contains the object
1653: */
1654: public static boolean contains(short[] array, short valueToFind) {
1655: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
1656: }
1657:
1658: // char IndexOf
1659: //-----------------------------------------------------------------------
1660: /**
1661: * <p>Finds the index of the given value in the array.</p>
1662: *
1663: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1664: *
1665: * @param array the array to search through for the object, may be <code>null</code>
1666: * @param valueToFind the value to find
1667: * @return the index of the value within the array,
1668: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1669: * @since 2.1
1670: */
1671: public static int indexOf(char[] array, char valueToFind) {
1672: return indexOf(array, valueToFind, 0);
1673: }
1674:
1675: /**
1676: * <p>Finds the index of the given value in the array starting at the given index.</p>
1677: *
1678: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1679: *
1680: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1681: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1682: *
1683: * @param array the array to search through for the object, may be <code>null</code>
1684: * @param valueToFind the value to find
1685: * @param startIndex the index to start searching at
1686: * @return the index of the value within the array,
1687: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1688: * @since 2.1
1689: */
1690: public static int indexOf(char[] array, char valueToFind,
1691: int startIndex) {
1692: if (array == null) {
1693: return INDEX_NOT_FOUND;
1694: }
1695: if (startIndex < 0) {
1696: startIndex = 0;
1697: }
1698: for (int i = startIndex; i < array.length; i++) {
1699: if (valueToFind == array[i]) {
1700: return i;
1701: }
1702: }
1703: return INDEX_NOT_FOUND;
1704: }
1705:
1706: /**
1707: * <p>Finds the last index of the given value within the array.</p>
1708: *
1709: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1710: *
1711: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1712: * @param valueToFind the object to find
1713: * @return the last index of the value within the array,
1714: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1715: * @since 2.1
1716: */
1717: public static int lastIndexOf(char[] array, char valueToFind) {
1718: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1719: }
1720:
1721: /**
1722: * <p>Finds the last index of the given value in the array starting at the given index.</p>
1723: *
1724: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1725: *
1726: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
1727: * array length will search from the end of the array.</p>
1728: *
1729: * @param array the array to traverse for looking for the object, may be <code>null</code>
1730: * @param valueToFind the value to find
1731: * @param startIndex the start index to travers backwards from
1732: * @return the last index of the value within the array,
1733: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1734: * @since 2.1
1735: */
1736: public static int lastIndexOf(char[] array, char valueToFind,
1737: int startIndex) {
1738: if (array == null) {
1739: return INDEX_NOT_FOUND;
1740: }
1741: if (startIndex < 0) {
1742: return INDEX_NOT_FOUND;
1743: } else if (startIndex >= array.length) {
1744: startIndex = array.length - 1;
1745: }
1746: for (int i = startIndex; i >= 0; i--) {
1747: if (valueToFind == array[i]) {
1748: return i;
1749: }
1750: }
1751: return INDEX_NOT_FOUND;
1752: }
1753:
1754: /**
1755: * <p>Checks if the value is in the given array.</p>
1756: *
1757: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1758: *
1759: * @param array the array to search through
1760: * @param valueToFind the value to find
1761: * @return <code>true</code> if the array contains the object
1762: * @since 2.1
1763: */
1764: public static boolean contains(char[] array, char valueToFind) {
1765: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
1766: }
1767:
1768: // byte IndexOf
1769: //-----------------------------------------------------------------------
1770: /**
1771: * <p>Finds the index of the given value in the array.</p>
1772: *
1773: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1774: *
1775: * @param array the array to search through for the object, may be <code>null</code>
1776: * @param valueToFind the value to find
1777: * @return the index of the value within the array,
1778: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1779: */
1780: public static int indexOf(byte[] array, byte valueToFind) {
1781: return indexOf(array, valueToFind, 0);
1782: }
1783:
1784: /**
1785: * <p>Finds the index of the given value in the array starting at the given index.</p>
1786: *
1787: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1788: *
1789: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1790: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1791: *
1792: * @param array the array to search through for the object, may be <code>null</code>
1793: * @param valueToFind the value to find
1794: * @param startIndex the index to start searching at
1795: * @return the index of the value within the array,
1796: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1797: */
1798: public static int indexOf(byte[] array, byte valueToFind,
1799: int startIndex) {
1800: if (array == null) {
1801: return INDEX_NOT_FOUND;
1802: }
1803: if (startIndex < 0) {
1804: startIndex = 0;
1805: }
1806: for (int i = startIndex; i < array.length; i++) {
1807: if (valueToFind == array[i]) {
1808: return i;
1809: }
1810: }
1811: return INDEX_NOT_FOUND;
1812: }
1813:
1814: /**
1815: * <p>Finds the last index of the given value within the array.</p>
1816: *
1817: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1818: *
1819: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1820: * @param valueToFind the object to find
1821: * @return the last index of the value within the array,
1822: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1823: */
1824: public static int lastIndexOf(byte[] array, byte valueToFind) {
1825: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1826: }
1827:
1828: /**
1829: * <p>Finds the last index of the given value in the array starting at the given index.</p>
1830: *
1831: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1832: *
1833: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
1834: * array length will search from the end of the array.</p>
1835: *
1836: * @param array the array to traverse for looking for the object, may be <code>null</code>
1837: * @param valueToFind the value to find
1838: * @param startIndex the start index to travers backwards from
1839: * @return the last index of the value within the array,
1840: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1841: */
1842: public static int lastIndexOf(byte[] array, byte valueToFind,
1843: int startIndex) {
1844: if (array == null) {
1845: return INDEX_NOT_FOUND;
1846: }
1847: if (startIndex < 0) {
1848: return INDEX_NOT_FOUND;
1849: } else if (startIndex >= array.length) {
1850: startIndex = array.length - 1;
1851: }
1852: for (int i = startIndex; i >= 0; i--) {
1853: if (valueToFind == array[i]) {
1854: return i;
1855: }
1856: }
1857: return INDEX_NOT_FOUND;
1858: }
1859:
1860: /**
1861: * <p>Checks if the value is in the given array.</p>
1862: *
1863: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1864: *
1865: * @param array the array to search through
1866: * @param valueToFind the value to find
1867: * @return <code>true</code> if the array contains the object
1868: */
1869: public static boolean contains(byte[] array, byte valueToFind) {
1870: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
1871: }
1872:
1873: // double IndexOf
1874: //-----------------------------------------------------------------------
1875: /**
1876: * <p>Finds the index of the given value in the array.</p>
1877: *
1878: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1879: *
1880: * @param array the array to search through for the object, may be <code>null</code>
1881: * @param valueToFind the value to find
1882: * @return the index of the value within the array,
1883: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1884: */
1885: public static int indexOf(double[] array, double valueToFind) {
1886: return indexOf(array, valueToFind, 0);
1887: }
1888:
1889: /**
1890: * <p>Finds the index of the given value within a given tolerance in the array.
1891: * This method will return the index of the first value which falls between the region
1892: * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1893: *
1894: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1895: *
1896: * @param array the array to search through for the object, may be <code>null</code>
1897: * @param valueToFind the value to find
1898: * @param tolerance tolerance of the search
1899: * @return the index of the value within the array,
1900: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1901: */
1902: public static int indexOf(double[] array, double valueToFind,
1903: double tolerance) {
1904: return indexOf(array, valueToFind, 0, tolerance);
1905: }
1906:
1907: /**
1908: * <p>Finds the index of the given value in the array starting at the given index.</p>
1909: *
1910: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1911: *
1912: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1913: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1914: *
1915: * @param array the array to search through for the object, may be <code>null</code>
1916: * @param valueToFind the value to find
1917: * @param startIndex the index to start searching at
1918: * @return the index of the value within the array,
1919: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1920: */
1921: public static int indexOf(double[] array, double valueToFind,
1922: int startIndex) {
1923: if (ArrayUtils.isEmpty(array)) {
1924: return INDEX_NOT_FOUND;
1925: }
1926: if (startIndex < 0) {
1927: startIndex = 0;
1928: }
1929: for (int i = startIndex; i < array.length; i++) {
1930: if (valueToFind == array[i]) {
1931: return i;
1932: }
1933: }
1934: return INDEX_NOT_FOUND;
1935: }
1936:
1937: /**
1938: * <p>Finds the index of the given value in the array starting at the given index.
1939: * This method will return the index of the first value which falls between the region
1940: * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1941: *
1942: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1943: *
1944: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1945: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
1946: *
1947: * @param array the array to search through for the object, may be <code>null</code>
1948: * @param valueToFind the value to find
1949: * @param startIndex the index to start searching at
1950: * @param tolerance tolerance of the search
1951: * @return the index of the value within the array,
1952: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1953: */
1954: public static int indexOf(double[] array, double valueToFind,
1955: int startIndex, double tolerance) {
1956: if (ArrayUtils.isEmpty(array)) {
1957: return INDEX_NOT_FOUND;
1958: }
1959: if (startIndex < 0) {
1960: startIndex = 0;
1961: }
1962: double min = valueToFind - tolerance;
1963: double max = valueToFind + tolerance;
1964: for (int i = startIndex; i < array.length; i++) {
1965: if (array[i] >= min && array[i] <= max) {
1966: return i;
1967: }
1968: }
1969: return INDEX_NOT_FOUND;
1970: }
1971:
1972: /**
1973: * <p>Finds the last index of the given value within the array.</p>
1974: *
1975: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1976: *
1977: * @param array the array to travers backwords looking for the object, may be <code>null</code>
1978: * @param valueToFind the object to find
1979: * @return the last index of the value within the array,
1980: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1981: */
1982: public static int lastIndexOf(double[] array, double valueToFind) {
1983: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1984: }
1985:
1986: /**
1987: * <p>Finds the last index of the given value within a given tolerance in the array.
1988: * This method will return the index of the last value which falls between the region
1989: * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1990: *
1991: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
1992: *
1993: * @param array the array to search through for the object, may be <code>null</code>
1994: * @param valueToFind the value to find
1995: * @param tolerance tolerance of the search
1996: * @return the index of the value within the array,
1997: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
1998: */
1999: public static int lastIndexOf(double[] array, double valueToFind,
2000: double tolerance) {
2001: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE,
2002: tolerance);
2003: }
2004:
2005: /**
2006: * <p>Finds the last index of the given value in the array starting at the given index.</p>
2007: *
2008: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2009: *
2010: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
2011: * array length will search from the end of the array.</p>
2012: *
2013: * @param array the array to traverse for looking for the object, may be <code>null</code>
2014: * @param valueToFind the value to find
2015: * @param startIndex the start index to travers backwards from
2016: * @return the last index of the value within the array,
2017: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2018: */
2019: public static int lastIndexOf(double[] array, double valueToFind,
2020: int startIndex) {
2021: if (ArrayUtils.isEmpty(array)) {
2022: return INDEX_NOT_FOUND;
2023: }
2024: if (startIndex < 0) {
2025: return INDEX_NOT_FOUND;
2026: } else if (startIndex >= array.length) {
2027: startIndex = array.length - 1;
2028: }
2029: for (int i = startIndex; i >= 0; i--) {
2030: if (valueToFind == array[i]) {
2031: return i;
2032: }
2033: }
2034: return INDEX_NOT_FOUND;
2035: }
2036:
2037: /**
2038: * <p>Finds the last index of the given value in the array starting at the given index.
2039: * This method will return the index of the last value which falls between the region
2040: * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
2041: *
2042: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2043: *
2044: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
2045: * array length will search from the end of the array.</p>
2046: *
2047: * @param array the array to traverse for looking for the object, may be <code>null</code>
2048: * @param valueToFind the value to find
2049: * @param startIndex the start index to travers backwards from
2050: * @param tolerance search for value within plus/minus this amount
2051: * @return the last index of the value within the array,
2052: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2053: */
2054: public static int lastIndexOf(double[] array, double valueToFind,
2055: int startIndex, double tolerance) {
2056: if (ArrayUtils.isEmpty(array)) {
2057: return INDEX_NOT_FOUND;
2058: }
2059: if (startIndex < 0) {
2060: return INDEX_NOT_FOUND;
2061: } else if (startIndex >= array.length) {
2062: startIndex = array.length - 1;
2063: }
2064: double min = valueToFind - tolerance;
2065: double max = valueToFind + tolerance;
2066: for (int i = startIndex; i >= 0; i--) {
2067: if (array[i] >= min && array[i] <= max) {
2068: return i;
2069: }
2070: }
2071: return INDEX_NOT_FOUND;
2072: }
2073:
2074: /**
2075: * <p>Checks if the value is in the given array.</p>
2076: *
2077: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2078: *
2079: * @param array the array to search through
2080: * @param valueToFind the value to find
2081: * @return <code>true</code> if the array contains the object
2082: */
2083: public static boolean contains(double[] array, double valueToFind) {
2084: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
2085: }
2086:
2087: /**
2088: * <p>Checks if a value falling within the given tolerance is in the
2089: * given array. If the array contains a value within the inclusive range
2090: * defined by (value - tolerance) to (value + tolerance).</p>
2091: *
2092: * <p>The method returns <code>false</code> if a <code>null</code> array
2093: * is passed in.</p>
2094: *
2095: * @param array the array to search
2096: * @param valueToFind the value to find
2097: * @param tolerance the array contains the tolerance of the search
2098: * @return true if value falling within tolerance is in array
2099: */
2100: public static boolean contains(double[] array, double valueToFind,
2101: double tolerance) {
2102: return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
2103: }
2104:
2105: // float IndexOf
2106: //-----------------------------------------------------------------------
2107: /**
2108: * <p>Finds the index of the given value in the array.</p>
2109: *
2110: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2111: *
2112: * @param array the array to search through for the object, may be <code>null</code>
2113: * @param valueToFind the value to find
2114: * @return the index of the value within the array,
2115: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2116: */
2117: public static int indexOf(float[] array, float valueToFind) {
2118: return indexOf(array, valueToFind, 0);
2119: }
2120:
2121: /**
2122: * <p>Finds the index of the given value in the array starting at the given index.</p>
2123: *
2124: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2125: *
2126: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
2127: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
2128: *
2129: * @param array the array to search through for the object, may be <code>null</code>
2130: * @param valueToFind the value to find
2131: * @param startIndex the index to start searching at
2132: * @return the index of the value within the array,
2133: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2134: */
2135: public static int indexOf(float[] array, float valueToFind,
2136: int startIndex) {
2137: if (ArrayUtils.isEmpty(array)) {
2138: return INDEX_NOT_FOUND;
2139: }
2140: if (startIndex < 0) {
2141: startIndex = 0;
2142: }
2143: for (int i = startIndex; i < array.length; i++) {
2144: if (valueToFind == array[i]) {
2145: return i;
2146: }
2147: }
2148: return INDEX_NOT_FOUND;
2149: }
2150:
2151: /**
2152: * <p>Finds the last index of the given value within the array.</p>
2153: *
2154: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2155: *
2156: * @param array the array to travers backwords looking for the object, may be <code>null</code>
2157: * @param valueToFind the object to find
2158: * @return the last index of the value within the array,
2159: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2160: */
2161: public static int lastIndexOf(float[] array, float valueToFind) {
2162: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
2163: }
2164:
2165: /**
2166: * <p>Finds the last index of the given value in the array starting at the given index.</p>
2167: *
2168: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2169: *
2170: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
2171: * array length will search from the end of the array.</p>
2172: *
2173: * @param array the array to traverse for looking for the object, may be <code>null</code>
2174: * @param valueToFind the value to find
2175: * @param startIndex the start index to travers backwards from
2176: * @return the last index of the value within the array,
2177: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2178: */
2179: public static int lastIndexOf(float[] array, float valueToFind,
2180: int startIndex) {
2181: if (ArrayUtils.isEmpty(array)) {
2182: return INDEX_NOT_FOUND;
2183: }
2184: if (startIndex < 0) {
2185: return INDEX_NOT_FOUND;
2186: } else if (startIndex >= array.length) {
2187: startIndex = array.length - 1;
2188: }
2189: for (int i = startIndex; i >= 0; i--) {
2190: if (valueToFind == array[i]) {
2191: return i;
2192: }
2193: }
2194: return INDEX_NOT_FOUND;
2195: }
2196:
2197: /**
2198: * <p>Checks if the value is in the given array.</p>
2199: *
2200: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2201: *
2202: * @param array the array to search through
2203: * @param valueToFind the value to find
2204: * @return <code>true</code> if the array contains the object
2205: */
2206: public static boolean contains(float[] array, float valueToFind) {
2207: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
2208: }
2209:
2210: // boolean IndexOf
2211: //-----------------------------------------------------------------------
2212: /**
2213: * <p>Finds the index of the given value in the array.</p>
2214: *
2215: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2216: *
2217: * @param array the array to search through for the object, may be <code>null</code>
2218: * @param valueToFind the value to find
2219: * @return the index of the value within the array,
2220: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2221: */
2222: public static int indexOf(boolean[] array, boolean valueToFind) {
2223: return indexOf(array, valueToFind, 0);
2224: }
2225:
2226: /**
2227: * <p>Finds the index of the given value in the array starting at the given index.</p>
2228: *
2229: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2230: *
2231: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
2232: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
2233: *
2234: * @param array the array to search through for the object, may be <code>null</code>
2235: * @param valueToFind the value to find
2236: * @param startIndex the index to start searching at
2237: * @return the index of the value within the array,
2238: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code>
2239: * array input
2240: */
2241: public static int indexOf(boolean[] array, boolean valueToFind,
2242: int startIndex) {
2243: if (ArrayUtils.isEmpty(array)) {
2244: return INDEX_NOT_FOUND;
2245: }
2246: if (startIndex < 0) {
2247: startIndex = 0;
2248: }
2249: for (int i = startIndex; i < array.length; i++) {
2250: if (valueToFind == array[i]) {
2251: return i;
2252: }
2253: }
2254: return INDEX_NOT_FOUND;
2255: }
2256:
2257: /**
2258: * <p>Finds the last index of the given value within the array.</p>
2259: *
2260: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) if
2261: * <code>null</code> array input.</p>
2262: *
2263: * @param array the array to travers backwords looking for the object, may be <code>null</code>
2264: * @param valueToFind the object to find
2265: * @return the last index of the value within the array,
2266: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2267: */
2268: public static int lastIndexOf(boolean[] array, boolean valueToFind) {
2269: return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
2270: }
2271:
2272: /**
2273: * <p>Finds the last index of the given value in the array starting at the given index.</p>
2274: *
2275: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
2276: *
2277: * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than
2278: * the array length will search from the end of the array.</p>
2279: *
2280: * @param array the array to traverse for looking for the object, may be <code>null</code>
2281: * @param valueToFind the value to find
2282: * @param startIndex the start index to travers backwards from
2283: * @return the last index of the value within the array,
2284: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
2285: */
2286: public static int lastIndexOf(boolean[] array, boolean valueToFind,
2287: int startIndex) {
2288: if (ArrayUtils.isEmpty(array)) {
2289: return INDEX_NOT_FOUND;
2290: }
2291: if (startIndex < 0) {
2292: return INDEX_NOT_FOUND;
2293: } else if (startIndex >= array.length) {
2294: startIndex = array.length - 1;
2295: }
2296: for (int i = startIndex; i >= 0; i--) {
2297: if (valueToFind == array[i]) {
2298: return i;
2299: }
2300: }
2301: return INDEX_NOT_FOUND;
2302: }
2303:
2304: /**
2305: * <p>Checks if the value is in the given array.</p>
2306: *
2307: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2308: *
2309: * @param array the array to search through
2310: * @param valueToFind the value to find
2311: * @return <code>true</code> if the array contains the object
2312: */
2313: public static boolean contains(boolean[] array, boolean valueToFind) {
2314: return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
2315: }
2316:
2317: // Primitive/Object array converters
2318: // ----------------------------------------------------------------------
2319:
2320: // Character array converters
2321: // ----------------------------------------------------------------------
2322: /**
2323: * <p>Converts an array of object Characters to primitives.</p>
2324: *
2325: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2326: *
2327: * @param array a <code>Character</code> array, may be <code>null</code>
2328: * @return a <code>char</code> array, <code>null</code> if null array input
2329: * @throws NullPointerException if array content is <code>null</code>
2330: */
2331: public static char[] toPrimitive(Character[] array) {
2332: if (array == null) {
2333: return null;
2334: } else if (array.length == 0) {
2335: return EMPTY_CHAR_ARRAY;
2336: }
2337: final char[] result = new char[array.length];
2338: for (int i = 0; i < array.length; i++) {
2339: result[i] = array[i].charValue();
2340: }
2341: return result;
2342: }
2343:
2344: /**
2345: * <p>Converts an array of object Character to primitives handling <code>null</code>.</p>
2346: *
2347: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2348: *
2349: * @param array a <code>Character</code> array, may be <code>null</code>
2350: * @param valueForNull the value to insert if <code>null</code> found
2351: * @return a <code>char</code> array, <code>null</code> if null array input
2352: */
2353: public static char[] toPrimitive(Character[] array,
2354: char valueForNull) {
2355: if (array == null) {
2356: return null;
2357: } else if (array.length == 0) {
2358: return EMPTY_CHAR_ARRAY;
2359: }
2360: final char[] result = new char[array.length];
2361: for (int i = 0; i < array.length; i++) {
2362: Character b = array[i];
2363: result[i] = (b == null ? valueForNull : b.charValue());
2364: }
2365: return result;
2366: }
2367:
2368: /**
2369: * <p>Converts an array of primitive chars to objects.</p>
2370: *
2371: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2372: *
2373: * @param array a <code>char</code> array
2374: * @return a <code>Character</code> array, <code>null</code> if null array input
2375: */
2376: public static Character[] toObject(char[] array) {
2377: if (array == null) {
2378: return null;
2379: } else if (array.length == 0) {
2380: return EMPTY_CHARACTER_OBJECT_ARRAY;
2381: }
2382: final Character[] result = new Character[array.length];
2383: for (int i = 0; i < array.length; i++) {
2384: result[i] = new Character(array[i]);
2385: }
2386: return result;
2387: }
2388:
2389: // Long array converters
2390: // ----------------------------------------------------------------------
2391: /**
2392: * <p>Converts an array of object Longs to primitives.</p>
2393: *
2394: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2395: *
2396: * @param array a <code>Long</code> array, may be <code>null</code>
2397: * @return a <code>long</code> array, <code>null</code> if null array input
2398: * @throws NullPointerException if array content is <code>null</code>
2399: */
2400: public static long[] toPrimitive(Long[] array) {
2401: if (array == null) {
2402: return null;
2403: } else if (array.length == 0) {
2404: return EMPTY_LONG_ARRAY;
2405: }
2406: final long[] result = new long[array.length];
2407: for (int i = 0; i < array.length; i++) {
2408: result[i] = array[i].longValue();
2409: }
2410: return result;
2411: }
2412:
2413: /**
2414: * <p>Converts an array of object Long to primitives handling <code>null</code>.</p>
2415: *
2416: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2417: *
2418: * @param array a <code>Long</code> array, may be <code>null</code>
2419: * @param valueForNull the value to insert if <code>null</code> found
2420: * @return a <code>long</code> array, <code>null</code> if null array input
2421: */
2422: public static long[] toPrimitive(Long[] array, long valueForNull) {
2423: if (array == null) {
2424: return null;
2425: } else if (array.length == 0) {
2426: return EMPTY_LONG_ARRAY;
2427: }
2428: final long[] result = new long[array.length];
2429: for (int i = 0; i < array.length; i++) {
2430: Long b = array[i];
2431: result[i] = (b == null ? valueForNull : b.longValue());
2432: }
2433: return result;
2434: }
2435:
2436: /**
2437: * <p>Converts an array of primitive longs to objects.</p>
2438: *
2439: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2440: *
2441: * @param array a <code>long</code> array
2442: * @return a <code>Long</code> array, <code>null</code> if null array input
2443: */
2444: public static Long[] toObject(long[] array) {
2445: if (array == null) {
2446: return null;
2447: } else if (array.length == 0) {
2448: return EMPTY_LONG_OBJECT_ARRAY;
2449: }
2450: final Long[] result = new Long[array.length];
2451: for (int i = 0; i < array.length; i++) {
2452: result[i] = new Long(array[i]);
2453: }
2454: return result;
2455: }
2456:
2457: // Int array converters
2458: // ----------------------------------------------------------------------
2459: /**
2460: * <p>Converts an array of object Integers to primitives.</p>
2461: *
2462: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2463: *
2464: * @param array a <code>Integer</code> array, may be <code>null</code>
2465: * @return an <code>int</code> array, <code>null</code> if null array input
2466: * @throws NullPointerException if array content is <code>null</code>
2467: */
2468: public static int[] toPrimitive(Integer[] array) {
2469: if (array == null) {
2470: return null;
2471: } else if (array.length == 0) {
2472: return EMPTY_INT_ARRAY;
2473: }
2474: final int[] result = new int[array.length];
2475: for (int i = 0; i < array.length; i++) {
2476: result[i] = array[i].intValue();
2477: }
2478: return result;
2479: }
2480:
2481: /**
2482: * <p>Converts an array of object Integer to primitives handling <code>null</code>.</p>
2483: *
2484: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2485: *
2486: * @param array a <code>Integer</code> array, may be <code>null</code>
2487: * @param valueForNull the value to insert if <code>null</code> found
2488: * @return an <code>int</code> array, <code>null</code> if null array input
2489: */
2490: public static int[] toPrimitive(Integer[] array, int valueForNull) {
2491: if (array == null) {
2492: return null;
2493: } else if (array.length == 0) {
2494: return EMPTY_INT_ARRAY;
2495: }
2496: final int[] result = new int[array.length];
2497: for (int i = 0; i < array.length; i++) {
2498: Integer b = array[i];
2499: result[i] = (b == null ? valueForNull : b.intValue());
2500: }
2501: return result;
2502: }
2503:
2504: /**
2505: * <p>Converts an array of primitive ints to objects.</p>
2506: *
2507: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2508: *
2509: * @param array an <code>int</code> array
2510: * @return an <code>Integer</code> array, <code>null</code> if null array input
2511: */
2512: public static Integer[] toObject(int[] array) {
2513: if (array == null) {
2514: return null;
2515: } else if (array.length == 0) {
2516: return EMPTY_INTEGER_OBJECT_ARRAY;
2517: }
2518: final Integer[] result = new Integer[array.length];
2519: for (int i = 0; i < array.length; i++) {
2520: result[i] = new Integer(array[i]);
2521: }
2522: return result;
2523: }
2524:
2525: // Short array converters
2526: // ----------------------------------------------------------------------
2527: /**
2528: * <p>Converts an array of object Shorts to primitives.</p>
2529: *
2530: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2531: *
2532: * @param array a <code>Short</code> array, may be <code>null</code>
2533: * @return a <code>byte</code> array, <code>null</code> if null array input
2534: * @throws NullPointerException if array content is <code>null</code>
2535: */
2536: public static short[] toPrimitive(Short[] array) {
2537: if (array == null) {
2538: return null;
2539: } else if (array.length == 0) {
2540: return EMPTY_SHORT_ARRAY;
2541: }
2542: final short[] result = new short[array.length];
2543: for (int i = 0; i < array.length; i++) {
2544: result[i] = array[i].shortValue();
2545: }
2546: return result;
2547: }
2548:
2549: /**
2550: * <p>Converts an array of object Short to primitives handling <code>null</code>.</p>
2551: *
2552: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2553: *
2554: * @param array a <code>Short</code> array, may be <code>null</code>
2555: * @param valueForNull the value to insert if <code>null</code> found
2556: * @return a <code>byte</code> array, <code>null</code> if null array input
2557: */
2558: public static short[] toPrimitive(Short[] array, short valueForNull) {
2559: if (array == null) {
2560: return null;
2561: } else if (array.length == 0) {
2562: return EMPTY_SHORT_ARRAY;
2563: }
2564: final short[] result = new short[array.length];
2565: for (int i = 0; i < array.length; i++) {
2566: Short b = array[i];
2567: result[i] = (b == null ? valueForNull : b.shortValue());
2568: }
2569: return result;
2570: }
2571:
2572: /**
2573: * <p>Converts an array of primitive shorts to objects.</p>
2574: *
2575: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2576: *
2577: * @param array a <code>short</code> array
2578: * @return a <code>Short</code> array, <code>null</code> if null array input
2579: */
2580: public static Short[] toObject(short[] array) {
2581: if (array == null) {
2582: return null;
2583: } else if (array.length == 0) {
2584: return EMPTY_SHORT_OBJECT_ARRAY;
2585: }
2586: final Short[] result = new Short[array.length];
2587: for (int i = 0; i < array.length; i++) {
2588: result[i] = new Short(array[i]);
2589: }
2590: return result;
2591: }
2592:
2593: // Byte array converters
2594: // ----------------------------------------------------------------------
2595: /**
2596: * <p>Converts an array of object Bytes to primitives.</p>
2597: *
2598: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2599: *
2600: * @param array a <code>Byte</code> array, may be <code>null</code>
2601: * @return a <code>byte</code> array, <code>null</code> if null array input
2602: * @throws NullPointerException if array content is <code>null</code>
2603: */
2604: public static byte[] toPrimitive(Byte[] array) {
2605: if (array == null) {
2606: return null;
2607: } else if (array.length == 0) {
2608: return EMPTY_BYTE_ARRAY;
2609: }
2610: final byte[] result = new byte[array.length];
2611: for (int i = 0; i < array.length; i++) {
2612: result[i] = array[i].byteValue();
2613: }
2614: return result;
2615: }
2616:
2617: /**
2618: * <p>Converts an array of object Bytes to primitives handling <code>null</code>.</p>
2619: *
2620: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2621: *
2622: * @param array a <code>Byte</code> array, may be <code>null</code>
2623: * @param valueForNull the value to insert if <code>null</code> found
2624: * @return a <code>byte</code> array, <code>null</code> if null array input
2625: */
2626: public static byte[] toPrimitive(Byte[] array, byte valueForNull) {
2627: if (array == null) {
2628: return null;
2629: } else if (array.length == 0) {
2630: return EMPTY_BYTE_ARRAY;
2631: }
2632: final byte[] result = new byte[array.length];
2633: for (int i = 0; i < array.length; i++) {
2634: Byte b = array[i];
2635: result[i] = (b == null ? valueForNull : b.byteValue());
2636: }
2637: return result;
2638: }
2639:
2640: /**
2641: * <p>Converts an array of primitive bytes to objects.</p>
2642: *
2643: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2644: *
2645: * @param array a <code>byte</code> array
2646: * @return a <code>Byte</code> array, <code>null</code> if null array input
2647: */
2648: public static Byte[] toObject(byte[] array) {
2649: if (array == null) {
2650: return null;
2651: } else if (array.length == 0) {
2652: return EMPTY_BYTE_OBJECT_ARRAY;
2653: }
2654: final Byte[] result = new Byte[array.length];
2655: for (int i = 0; i < array.length; i++) {
2656: result[i] = new Byte(array[i]);
2657: }
2658: return result;
2659: }
2660:
2661: // Double array converters
2662: // ----------------------------------------------------------------------
2663: /**
2664: * <p>Converts an array of object Doubles to primitives.</p>
2665: *
2666: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2667: *
2668: * @param array a <code>Double</code> array, may be <code>null</code>
2669: * @return a <code>double</code> array, <code>null</code> if null array input
2670: * @throws NullPointerException if array content is <code>null</code>
2671: */
2672: public static double[] toPrimitive(Double[] array) {
2673: if (array == null) {
2674: return null;
2675: } else if (array.length == 0) {
2676: return EMPTY_DOUBLE_ARRAY;
2677: }
2678: final double[] result = new double[array.length];
2679: for (int i = 0; i < array.length; i++) {
2680: result[i] = array[i].doubleValue();
2681: }
2682: return result;
2683: }
2684:
2685: /**
2686: * <p>Converts an array of object Doubles to primitives handling <code>null</code>.</p>
2687: *
2688: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2689: *
2690: * @param array a <code>Double</code> array, may be <code>null</code>
2691: * @param valueForNull the value to insert if <code>null</code> found
2692: * @return a <code>double</code> array, <code>null</code> if null array input
2693: */
2694: public static double[] toPrimitive(Double[] array,
2695: double valueForNull) {
2696: if (array == null) {
2697: return null;
2698: } else if (array.length == 0) {
2699: return EMPTY_DOUBLE_ARRAY;
2700: }
2701: final double[] result = new double[array.length];
2702: for (int i = 0; i < array.length; i++) {
2703: Double b = array[i];
2704: result[i] = (b == null ? valueForNull : b.doubleValue());
2705: }
2706: return result;
2707: }
2708:
2709: /**
2710: * <p>Converts an array of primitive doubles to objects.</p>
2711: *
2712: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2713: *
2714: * @param array a <code>double</code> array
2715: * @return a <code>Double</code> array, <code>null</code> if null array input
2716: */
2717: public static Double[] toObject(double[] array) {
2718: if (array == null) {
2719: return null;
2720: } else if (array.length == 0) {
2721: return EMPTY_DOUBLE_OBJECT_ARRAY;
2722: }
2723: final Double[] result = new Double[array.length];
2724: for (int i = 0; i < array.length; i++) {
2725: result[i] = new Double(array[i]);
2726: }
2727: return result;
2728: }
2729:
2730: // Float array converters
2731: // ----------------------------------------------------------------------
2732: /**
2733: * <p>Converts an array of object Floats to primitives.</p>
2734: *
2735: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2736: *
2737: * @param array a <code>Float</code> array, may be <code>null</code>
2738: * @return a <code>float</code> array, <code>null</code> if null array input
2739: * @throws NullPointerException if array content is <code>null</code>
2740: */
2741: public static float[] toPrimitive(Float[] array) {
2742: if (array == null) {
2743: return null;
2744: } else if (array.length == 0) {
2745: return EMPTY_FLOAT_ARRAY;
2746: }
2747: final float[] result = new float[array.length];
2748: for (int i = 0; i < array.length; i++) {
2749: result[i] = array[i].floatValue();
2750: }
2751: return result;
2752: }
2753:
2754: /**
2755: * <p>Converts an array of object Floats to primitives handling <code>null</code>.</p>
2756: *
2757: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2758: *
2759: * @param array a <code>Float</code> array, may be <code>null</code>
2760: * @param valueForNull the value to insert if <code>null</code> found
2761: * @return a <code>float</code> array, <code>null</code> if null array input
2762: */
2763: public static float[] toPrimitive(Float[] array, float valueForNull) {
2764: if (array == null) {
2765: return null;
2766: } else if (array.length == 0) {
2767: return EMPTY_FLOAT_ARRAY;
2768: }
2769: final float[] result = new float[array.length];
2770: for (int i = 0; i < array.length; i++) {
2771: Float b = array[i];
2772: result[i] = (b == null ? valueForNull : b.floatValue());
2773: }
2774: return result;
2775: }
2776:
2777: /**
2778: * <p>Converts an array of primitive floats to objects.</p>
2779: *
2780: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2781: *
2782: * @param array a <code>float</code> array
2783: * @return a <code>Float</code> array, <code>null</code> if null array input
2784: */
2785: public static Float[] toObject(float[] array) {
2786: if (array == null) {
2787: return null;
2788: } else if (array.length == 0) {
2789: return EMPTY_FLOAT_OBJECT_ARRAY;
2790: }
2791: final Float[] result = new Float[array.length];
2792: for (int i = 0; i < array.length; i++) {
2793: result[i] = new Float(array[i]);
2794: }
2795: return result;
2796: }
2797:
2798: // Boolean array converters
2799: // ----------------------------------------------------------------------
2800: /**
2801: * <p>Converts an array of object Booleans to primitives.</p>
2802: *
2803: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2804: *
2805: * @param array a <code>Boolean</code> array, may be <code>null</code>
2806: * @return a <code>boolean</code> array, <code>null</code> if null array input
2807: * @throws NullPointerException if array content is <code>null</code>
2808: */
2809: public static boolean[] toPrimitive(Boolean[] array) {
2810: if (array == null) {
2811: return null;
2812: } else if (array.length == 0) {
2813: return EMPTY_BOOLEAN_ARRAY;
2814: }
2815: final boolean[] result = new boolean[array.length];
2816: for (int i = 0; i < array.length; i++) {
2817: result[i] = array[i].booleanValue();
2818: }
2819: return result;
2820: }
2821:
2822: /**
2823: * <p>Converts an array of object Booleans to primitives handling <code>null</code>.</p>
2824: *
2825: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2826: *
2827: * @param array a <code>Boolean</code> array, may be <code>null</code>
2828: * @param valueForNull the value to insert if <code>null</code> found
2829: * @return a <code>boolean</code> array, <code>null</code> if null array input
2830: */
2831: public static boolean[] toPrimitive(Boolean[] array,
2832: boolean valueForNull) {
2833: if (array == null) {
2834: return null;
2835: } else if (array.length == 0) {
2836: return EMPTY_BOOLEAN_ARRAY;
2837: }
2838: final boolean[] result = new boolean[array.length];
2839: for (int i = 0; i < array.length; i++) {
2840: Boolean b = array[i];
2841: result[i] = (b == null ? valueForNull : b.booleanValue());
2842: }
2843: return result;
2844: }
2845:
2846: /**
2847: * <p>Converts an array of primitive booleans to objects.</p>
2848: *
2849: * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
2850: *
2851: * @param array a <code>boolean</code> array
2852: * @return a <code>Boolean</code> array, <code>null</code> if null array input
2853: */
2854: public static Boolean[] toObject(boolean[] array) {
2855: if (array == null) {
2856: return null;
2857: } else if (array.length == 0) {
2858: return EMPTY_BOOLEAN_OBJECT_ARRAY;
2859: }
2860: final Boolean[] result = new Boolean[array.length];
2861: for (int i = 0; i < array.length; i++) {
2862: result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
2863: }
2864: return result;
2865: }
2866:
2867: // ----------------------------------------------------------------------
2868: /**
2869: * <p>Checks if an array of Objects is empty or <code>null</code>.</p>
2870: *
2871: * @param array the array to test
2872: * @return <code>true</code> if the array is empty or <code>null</code>
2873: * @since 2.1
2874: */
2875: public static boolean isEmpty(Object[] array) {
2876: if (array == null || array.length == 0) {
2877: return true;
2878: }
2879: return false;
2880: }
2881:
2882: /**
2883: * <p>Checks if an array of primitive longs is empty or <code>null</code>.</p>
2884: *
2885: * @param array the array to test
2886: * @return <code>true</code> if the array is empty or <code>null</code>
2887: * @since 2.1
2888: */
2889: public static boolean isEmpty(long[] array) {
2890: if (array == null || array.length == 0) {
2891: return true;
2892: }
2893: return false;
2894: }
2895:
2896: /**
2897: * <p>Checks if an array of primitive ints is empty or <code>null</code>.</p>
2898: *
2899: * @param array the array to test
2900: * @return <code>true</code> if the array is empty or <code>null</code>
2901: * @since 2.1
2902: */
2903: public static boolean isEmpty(int[] array) {
2904: if (array == null || array.length == 0) {
2905: return true;
2906: }
2907: return false;
2908: }
2909:
2910: /**
2911: * <p>Checks if an array of primitive shorts is empty or <code>null</code>.</p>
2912: *
2913: * @param array the array to test
2914: * @return <code>true</code> if the array is empty or <code>null</code>
2915: * @since 2.1
2916: */
2917: public static boolean isEmpty(short[] array) {
2918: if (array == null || array.length == 0) {
2919: return true;
2920: }
2921: return false;
2922: }
2923:
2924: /**
2925: * <p>Checks if an array of primitive chars is empty or <code>null</code>.</p>
2926: *
2927: * @param array the array to test
2928: * @return <code>true</code> if the array is empty or <code>null</code>
2929: * @since 2.1
2930: */
2931: public static boolean isEmpty(char[] array) {
2932: if (array == null || array.length == 0) {
2933: return true;
2934: }
2935: return false;
2936: }
2937:
2938: /**
2939: * <p>Checks if an array of primitive bytes is empty or <code>null</code>.</p>
2940: *
2941: * @param array the array to test
2942: * @return <code>true</code> if the array is empty or <code>null</code>
2943: * @since 2.1
2944: */
2945: public static boolean isEmpty(byte[] array) {
2946: if (array == null || array.length == 0) {
2947: return true;
2948: }
2949: return false;
2950: }
2951:
2952: /**
2953: * <p>Checks if an array of primitive doubles is empty or <code>null</code>.</p>
2954: *
2955: * @param array the array to test
2956: * @return <code>true</code> if the array is empty or <code>null</code>
2957: * @since 2.1
2958: */
2959: public static boolean isEmpty(double[] array) {
2960: if (array == null || array.length == 0) {
2961: return true;
2962: }
2963: return false;
2964: }
2965:
2966: /**
2967: * <p>Checks if an array of primitive floats is empty or <code>null</code>.</p>
2968: *
2969: * @param array the array to test
2970: * @return <code>true</code> if the array is empty or <code>null</code>
2971: * @since 2.1
2972: */
2973: public static boolean isEmpty(float[] array) {
2974: if (array == null || array.length == 0) {
2975: return true;
2976: }
2977: return false;
2978: }
2979:
2980: /**
2981: * <p>Checks if an array of primitive booleans is empty or <code>null</code>.</p>
2982: *
2983: * @param array the array to test
2984: * @return <code>true</code> if the array is empty or <code>null</code>
2985: * @since 2.1
2986: */
2987: public static boolean isEmpty(boolean[] array) {
2988: if (array == null || array.length == 0) {
2989: return true;
2990: }
2991: return false;
2992: }
2993:
2994: /**
2995: * <p>Adds all the elements of the given arrays into a new array.</p>
2996: * <p>The new array contains all of the element of <code>array1</code> followed
2997: * by all of the elements <code>array2</code>. When an array is returned, it is always
2998: * a new array.</p>
2999: *
3000: * <pre>
3001: * ArrayUtils.addAll(null, null) = null
3002: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3003: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3004: * ArrayUtils.addAll([], []) = []
3005: * ArrayUtils.addAll([null], [null]) = [null, null]
3006: * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
3007: * </pre>
3008: *
3009: * @param array1 the first array whose elements are added to the new array, may be <code>null</code>
3010: * @param array2 the second array whose elements are added to the new array, may be <code>null</code>
3011: * @return The new array, <code>null</code> if <code>null</code> array inputs.
3012: * The type of the new array is the type of the first array.
3013: * @since 2.1
3014: */
3015: public static Object[] addAll(Object[] array1, Object[] array2) {
3016: if (array1 == null) {
3017: return clone(array2);
3018: } else if (array2 == null) {
3019: return clone(array1);
3020: }
3021: Object[] joinedArray = (Object[]) Array.newInstance(array1
3022: .getClass().getComponentType(), array1.length
3023: + array2.length);
3024: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3025: System.arraycopy(array2, 0, joinedArray, array1.length,
3026: array2.length);
3027: return joinedArray;
3028: }
3029:
3030: /**
3031: * <p>Adds all the elements of the given arrays into a new array.</p>
3032: * <p>The new array contains all of the element of <code>array1</code> followed
3033: * by all of the elements <code>array2</code>. When an array is returned, it is always
3034: * a new array.</p>
3035: *
3036: * <pre>
3037: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3038: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3039: * ArrayUtils.addAll([], []) = []
3040: * </pre>
3041: *
3042: * @param array1 the first array whose elements are added to the new array.
3043: * @param array2 the second array whose elements are added to the new array.
3044: * @return The new boolean[] array.
3045: * @since 2.1
3046: */
3047: public static boolean[] addAll(boolean[] array1, boolean[] array2) {
3048: if (array1 == null) {
3049: return clone(array2);
3050: } else if (array2 == null) {
3051: return clone(array1);
3052: }
3053: boolean[] joinedArray = new boolean[array1.length
3054: + array2.length];
3055: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3056: System.arraycopy(array2, 0, joinedArray, array1.length,
3057: array2.length);
3058: return joinedArray;
3059: }
3060:
3061: /**
3062: * <p>Adds all the elements of the given arrays into a new array.</p>
3063: * <p>The new array contains all of the element of <code>array1</code> followed
3064: * by all of the elements <code>array2</code>. When an array is returned, it is always
3065: * a new array.</p>
3066: *
3067: * <pre>
3068: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3069: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3070: * ArrayUtils.addAll([], []) = []
3071: * </pre>
3072: *
3073: * @param array1 the first array whose elements are added to the new array.
3074: * @param array2 the second array whose elements are added to the new array.
3075: * @return The new char[] array.
3076: * @since 2.1
3077: */
3078: public static char[] addAll(char[] array1, char[] array2) {
3079: if (array1 == null) {
3080: return clone(array2);
3081: } else if (array2 == null) {
3082: return clone(array1);
3083: }
3084: char[] joinedArray = new char[array1.length + array2.length];
3085: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3086: System.arraycopy(array2, 0, joinedArray, array1.length,
3087: array2.length);
3088: return joinedArray;
3089: }
3090:
3091: /**
3092: * <p>Adds all the elements of the given arrays into a new array.</p>
3093: * <p>The new array contains all of the element of <code>array1</code> followed
3094: * by all of the elements <code>array2</code>. When an array is returned, it is always
3095: * a new array.</p>
3096: *
3097: * <pre>
3098: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3099: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3100: * ArrayUtils.addAll([], []) = []
3101: * </pre>
3102: *
3103: * @param array1 the first array whose elements are added to the new array.
3104: * @param array2 the second array whose elements are added to the new array.
3105: * @return The new byte[] array.
3106: * @since 2.1
3107: */
3108: public static byte[] addAll(byte[] array1, byte[] array2) {
3109: if (array1 == null) {
3110: return clone(array2);
3111: } else if (array2 == null) {
3112: return clone(array1);
3113: }
3114: byte[] joinedArray = new byte[array1.length + array2.length];
3115: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3116: System.arraycopy(array2, 0, joinedArray, array1.length,
3117: array2.length);
3118: return joinedArray;
3119: }
3120:
3121: /**
3122: * <p>Adds all the elements of the given arrays into a new array.</p>
3123: * <p>The new array contains all of the element of <code>array1</code> followed
3124: * by all of the elements <code>array2</code>. When an array is returned, it is always
3125: * a new array.</p>
3126: *
3127: * <pre>
3128: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3129: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3130: * ArrayUtils.addAll([], []) = []
3131: * </pre>
3132: *
3133: * @param array1 the first array whose elements are added to the new array.
3134: * @param array2 the second array whose elements are added to the new array.
3135: * @return The new short[] array.
3136: * @since 2.1
3137: */
3138: public static short[] addAll(short[] array1, short[] array2) {
3139: if (array1 == null) {
3140: return clone(array2);
3141: } else if (array2 == null) {
3142: return clone(array1);
3143: }
3144: short[] joinedArray = new short[array1.length + array2.length];
3145: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3146: System.arraycopy(array2, 0, joinedArray, array1.length,
3147: array2.length);
3148: return joinedArray;
3149: }
3150:
3151: /**
3152: * <p>Adds all the elements of the given arrays into a new array.</p>
3153: * <p>The new array contains all of the element of <code>array1</code> followed
3154: * by all of the elements <code>array2</code>. When an array is returned, it is always
3155: * a new array.</p>
3156: *
3157: * <pre>
3158: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3159: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3160: * ArrayUtils.addAll([], []) = []
3161: * </pre>
3162: *
3163: * @param array1 the first array whose elements are added to the new array.
3164: * @param array2 the second array whose elements are added to the new array.
3165: * @return The new int[] array.
3166: * @since 2.1
3167: */
3168: public static int[] addAll(int[] array1, int[] array2) {
3169: if (array1 == null) {
3170: return clone(array2);
3171: } else if (array2 == null) {
3172: return clone(array1);
3173: }
3174: int[] joinedArray = new int[array1.length + array2.length];
3175: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3176: System.arraycopy(array2, 0, joinedArray, array1.length,
3177: array2.length);
3178: return joinedArray;
3179: }
3180:
3181: /**
3182: * <p>Adds all the elements of the given arrays into a new array.</p>
3183: * <p>The new array contains all of the element of <code>array1</code> followed
3184: * by all of the elements <code>array2</code>. When an array is returned, it is always
3185: * a new array.</p>
3186: *
3187: * <pre>
3188: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3189: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3190: * ArrayUtils.addAll([], []) = []
3191: * </pre>
3192: *
3193: * @param array1 the first array whose elements are added to the new array.
3194: * @param array2 the second array whose elements are added to the new array.
3195: * @return The new long[] array.
3196: * @since 2.1
3197: */
3198: public static long[] addAll(long[] array1, long[] array2) {
3199: if (array1 == null) {
3200: return clone(array2);
3201: } else if (array2 == null) {
3202: return clone(array1);
3203: }
3204: long[] joinedArray = new long[array1.length + array2.length];
3205: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3206: System.arraycopy(array2, 0, joinedArray, array1.length,
3207: array2.length);
3208: return joinedArray;
3209: }
3210:
3211: /**
3212: * <p>Adds all the elements of the given arrays into a new array.</p>
3213: * <p>The new array contains all of the element of <code>array1</code> followed
3214: * by all of the elements <code>array2</code>. When an array is returned, it is always
3215: * a new array.</p>
3216: *
3217: * <pre>
3218: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3219: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3220: * ArrayUtils.addAll([], []) = []
3221: * </pre>
3222: *
3223: * @param array1 the first array whose elements are added to the new array.
3224: * @param array2 the second array whose elements are added to the new array.
3225: * @return The new float[] array.
3226: * @since 2.1
3227: */
3228: public static float[] addAll(float[] array1, float[] array2) {
3229: if (array1 == null) {
3230: return clone(array2);
3231: } else if (array2 == null) {
3232: return clone(array1);
3233: }
3234: float[] joinedArray = new float[array1.length + array2.length];
3235: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3236: System.arraycopy(array2, 0, joinedArray, array1.length,
3237: array2.length);
3238: return joinedArray;
3239: }
3240:
3241: /**
3242: * <p>Adds all the elements of the given arrays into a new array.</p>
3243: * <p>The new array contains all of the element of <code>array1</code> followed
3244: * by all of the elements <code>array2</code>. When an array is returned, it is always
3245: * a new array.</p>
3246: *
3247: * <pre>
3248: * ArrayUtils.addAll(array1, null) = cloned copy of array1
3249: * ArrayUtils.addAll(null, array2) = cloned copy of array2
3250: * ArrayUtils.addAll([], []) = []
3251: * </pre>
3252: *
3253: * @param array1 the first array whose elements are added to the new array.
3254: * @param array2 the second array whose elements are added to the new array.
3255: * @return The new double[] array.
3256: * @since 2.1
3257: */
3258: public static double[] addAll(double[] array1, double[] array2) {
3259: if (array1 == null) {
3260: return clone(array2);
3261: } else if (array2 == null) {
3262: return clone(array1);
3263: }
3264: double[] joinedArray = new double[array1.length + array2.length];
3265: System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3266: System.arraycopy(array2, 0, joinedArray, array1.length,
3267: array2.length);
3268: return joinedArray;
3269: }
3270:
3271: /**
3272: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3273: *
3274: * <p>The new array contains the same elements of the input
3275: * array plus the given element in the last position. The component type of
3276: * the new array is the same as that of the input array.</p>
3277: *
3278: * <p>If the input array is <code>null</code>, a new one element array is returned
3279: * whose component type is the same as the element.</p>
3280: *
3281: * <pre>
3282: * ArrayUtils.add(null, null) = [null]
3283: * ArrayUtils.add(null, "a") = ["a"]
3284: * ArrayUtils.add(["a"], null) = ["a", null]
3285: * ArrayUtils.add(["a"], "b") = ["a", "b"]
3286: * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
3287: * </pre>
3288: *
3289: * @param array the array to "add" the element to, may be <code>null</code>
3290: * @param element the object to add
3291: * @return A new array containing the existing elements plus the new element
3292: * @since 2.1
3293: */
3294: public static Object[] add(Object[] array, Object element) {
3295: Class type = (array != null ? array.getClass()
3296: : (element != null ? element.getClass() : Object.class));
3297: Object[] newArray = (Object[]) copyArrayGrow1(array, type);
3298: newArray[newArray.length - 1] = element;
3299: return newArray;
3300: }
3301:
3302: /**
3303: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3304: *
3305: * <p>The new array contains the same elements of the input
3306: * array plus the given element in the last position. The component type of
3307: * the new array is the same as that of the input array.</p>
3308: *
3309: * <p>If the input array is <code>null</code>, a new one element array is returned
3310: * whose component type is the same as the element.</p>
3311: *
3312: * <pre>
3313: * ArrayUtils.add(null, true) = [true]
3314: * ArrayUtils.add([true], false) = [true, false]
3315: * ArrayUtils.add([true, false], true) = [true, false, true]
3316: * </pre>
3317: *
3318: * @param array the array to copy and add the element to, may be <code>null</code>
3319: * @param element the object to add at the last index of the new array
3320: * @return A new array containing the existing elements plus the new element
3321: * @since 2.1
3322: */
3323: public static boolean[] add(boolean[] array, boolean element) {
3324: boolean[] newArray = (boolean[]) copyArrayGrow1(array,
3325: Boolean.TYPE);
3326: newArray[newArray.length - 1] = element;
3327: return newArray;
3328: }
3329:
3330: /**
3331: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3332: *
3333: * <p>The new array contains the same elements of the input
3334: * array plus the given element in the last position. The component type of
3335: * the new array is the same as that of the input array.</p>
3336: *
3337: * <p>If the input array is <code>null</code>, a new one element array is returned
3338: * whose component type is the same as the element.</p>
3339: *
3340: * <pre>
3341: * ArrayUtils.add(null, 0) = [0]
3342: * ArrayUtils.add([1], 0) = [1, 0]
3343: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3344: * </pre>
3345: *
3346: * @param array the array to copy and add the element to, may be <code>null</code>
3347: * @param element the object to add at the last index of the new array
3348: * @return A new array containing the existing elements plus the new element
3349: * @since 2.1
3350: */
3351: public static byte[] add(byte[] array, byte element) {
3352: byte[] newArray = (byte[]) copyArrayGrow1(array, Byte.TYPE);
3353: newArray[newArray.length - 1] = element;
3354: return newArray;
3355: }
3356:
3357: /**
3358: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3359: *
3360: * <p>The new array contains the same elements of the input
3361: * array plus the given element in the last position. The component type of
3362: * the new array is the same as that of the input array.</p>
3363: *
3364: * <p>If the input array is <code>null</code>, a new one element array is returned
3365: * whose component type is the same as the element.</p>
3366: *
3367: * <pre>
3368: * ArrayUtils.add(null, '0') = ['0']
3369: * ArrayUtils.add(['1'], '0') = ['1', '0']
3370: * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
3371: * </pre>
3372: *
3373: * @param array the array to copy and add the element to, may be <code>null</code>
3374: * @param element the object to add at the last index of the new array
3375: * @return A new array containing the existing elements plus the new element
3376: * @since 2.1
3377: */
3378: public static char[] add(char[] array, char element) {
3379: char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE);
3380: newArray[newArray.length - 1] = element;
3381: return newArray;
3382: }
3383:
3384: /**
3385: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3386: *
3387: * <p>The new array contains the same elements of the input
3388: * array plus the given element in the last position. The component type of
3389: * the new array is the same as that of the input array.</p>
3390: *
3391: * <p>If the input array is <code>null</code>, a new one element array is returned
3392: * whose component type is the same as the element.</p>
3393: *
3394: * <pre>
3395: * ArrayUtils.add(null, 0) = [0]
3396: * ArrayUtils.add([1], 0) = [1, 0]
3397: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3398: * </pre>
3399: *
3400: * @param array the array to copy and add the element to, may be <code>null</code>
3401: * @param element the object to add at the last index of the new array
3402: * @return A new array containing the existing elements plus the new element
3403: * @since 2.1
3404: */
3405: public static double[] add(double[] array, double element) {
3406: double[] newArray = (double[]) copyArrayGrow1(array,
3407: Double.TYPE);
3408: newArray[newArray.length - 1] = element;
3409: return newArray;
3410: }
3411:
3412: /**
3413: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3414: *
3415: * <p>The new array contains the same elements of the input
3416: * array plus the given element in the last position. The component type of
3417: * the new array is the same as that of the input array.</p>
3418: *
3419: * <p>If the input array is <code>null</code>, a new one element array is returned
3420: * whose component type is the same as the element.</p>
3421: *
3422: * <pre>
3423: * ArrayUtils.add(null, 0) = [0]
3424: * ArrayUtils.add([1], 0) = [1, 0]
3425: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3426: * </pre>
3427: *
3428: * @param array the array to copy and add the element to, may be <code>null</code>
3429: * @param element the object to add at the last index of the new array
3430: * @return A new array containing the existing elements plus the new element
3431: * @since 2.1
3432: */
3433: public static float[] add(float[] array, float element) {
3434: float[] newArray = (float[]) copyArrayGrow1(array, Float.TYPE);
3435: newArray[newArray.length - 1] = element;
3436: return newArray;
3437: }
3438:
3439: /**
3440: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3441: *
3442: * <p>The new array contains the same elements of the input
3443: * array plus the given element in the last position. The component type of
3444: * the new array is the same as that of the input array.</p>
3445: *
3446: * <p>If the input array is <code>null</code>, a new one element array is returned
3447: * whose component type is the same as the element.</p>
3448: *
3449: * <pre>
3450: * ArrayUtils.add(null, 0) = [0]
3451: * ArrayUtils.add([1], 0) = [1, 0]
3452: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3453: * </pre>
3454: *
3455: * @param array the array to copy and add the element to, may be <code>null</code>
3456: * @param element the object to add at the last index of the new array
3457: * @return A new array containing the existing elements plus the new element
3458: * @since 2.1
3459: */
3460: public static int[] add(int[] array, int element) {
3461: int[] newArray = (int[]) copyArrayGrow1(array, Integer.TYPE);
3462: newArray[newArray.length - 1] = element;
3463: return newArray;
3464: }
3465:
3466: /**
3467: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3468: *
3469: * <p>The new array contains the same elements of the input
3470: * array plus the given element in the last position. The component type of
3471: * the new array is the same as that of the input array.</p>
3472: *
3473: * <p>If the input array is <code>null</code>, a new one element array is returned
3474: * whose component type is the same as the element.</p>
3475: *
3476: * <pre>
3477: * ArrayUtils.add(null, 0) = [0]
3478: * ArrayUtils.add([1], 0) = [1, 0]
3479: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3480: * </pre>
3481: *
3482: * @param array the array to copy and add the element to, may be <code>null</code>
3483: * @param element the object to add at the last index of the new array
3484: * @return A new array containing the existing elements plus the new element
3485: * @since 2.1
3486: */
3487: public static long[] add(long[] array, long element) {
3488: long[] newArray = (long[]) copyArrayGrow1(array, Long.TYPE);
3489: newArray[newArray.length - 1] = element;
3490: return newArray;
3491: }
3492:
3493: /**
3494: * <p>Copies the given array and adds the given element at the end of the new array.</p>
3495: *
3496: * <p>The new array contains the same elements of the input
3497: * array plus the given element in the last position. The component type of
3498: * the new array is the same as that of the input array.</p>
3499: *
3500: * <p>If the input array is <code>null</code>, a new one element array is returned
3501: * whose component type is the same as the element.</p>
3502: *
3503: * <pre>
3504: * ArrayUtils.add(null, 0) = [0]
3505: * ArrayUtils.add([1], 0) = [1, 0]
3506: * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3507: * </pre>
3508: *
3509: * @param array the array to copy and add the element to, may be <code>null</code>
3510: * @param element the object to add at the last index of the new array
3511: * @return A new array containing the existing elements plus the new element
3512: * @since 2.1
3513: */
3514: public static short[] add(short[] array, short element) {
3515: short[] newArray = (short[]) copyArrayGrow1(array, Short.TYPE);
3516: newArray[newArray.length - 1] = element;
3517: return newArray;
3518: }
3519:
3520: /**
3521: * Returns a copy of the given array of size 1 greater than the argument.
3522: * The last value of the array is left to the default value.
3523: *
3524: * @param array The array to copy, must not be <code>null</code>.
3525: * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a
3526: * size 1 array of this type.
3527: * @return A new copy of the array of size 1 greater than the input.
3528: */
3529: private static Object copyArrayGrow1(Object array,
3530: Class newArrayComponentType) {
3531: if (array != null) {
3532: int arrayLength = Array.getLength(array);
3533: Object newArray = Array.newInstance(array.getClass()
3534: .getComponentType(), arrayLength + 1);
3535: System.arraycopy(array, 0, newArray, 0, arrayLength);
3536: return newArray;
3537: }
3538: return Array.newInstance(newArrayComponentType, 1);
3539: }
3540:
3541: /**
3542: * <p>Inserts the specified element at the specified position in the array.
3543: * Shifts the element currently at that position (if any) and any subsequent
3544: * elements to the right (adds one to their indices).</p>
3545: *
3546: * <p>This method returns a new array with the same elements of the input
3547: * array plus the given element on the specified position. The component
3548: * type of the returned array is always the same as that of the input
3549: * array.</p>
3550: *
3551: * <p>If the input array is <code>null</code>, a new one element array is returned
3552: * whose component type is the same as the element.</p>
3553: *
3554: * <pre>
3555: * ArrayUtils.add(null, 0, null) = [null]
3556: * ArrayUtils.add(null, 0, "a") = ["a"]
3557: * ArrayUtils.add(["a"], 1, null) = ["a", null]
3558: * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
3559: * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
3560: * </pre>
3561: *
3562: * @param array the array to add the element to, may be <code>null</code>
3563: * @param index the position of the new object
3564: * @param element the object to add
3565: * @return A new array containing the existing elements and the new element
3566: * @throws IndexOutOfBoundsException if the index is out of range
3567: * (index < 0 || index > array.length).
3568: */
3569: public static Object[] add(Object[] array, int index, Object element) {
3570: Class clss = null;
3571: if (array != null) {
3572: clss = array.getClass().getComponentType();
3573: } else if (element != null) {
3574: clss = element.getClass();
3575: } else {
3576: return new Object[] { null };
3577: }
3578: return (Object[]) add(array, index, element, clss);
3579: }
3580:
3581: /**
3582: * <p>Inserts the specified element at the specified position in the array.
3583: * Shifts the element currently at that position (if any) and any subsequent
3584: * elements to the right (adds one to their indices).</p>
3585: *
3586: * <p>This method returns a new array with the same elements of the input
3587: * array plus the given element on the specified position. The component
3588: * type of the returned array is always the same as that of the input
3589: * array.</p>
3590: *
3591: * <p>If the input array is <code>null</code>, a new one element array is returned
3592: * whose component type is the same as the element.</p>
3593: *
3594: * <pre>
3595: * ArrayUtils.add(null, 0, true) = [true]
3596: * ArrayUtils.add([true], 0, false) = [false, true]
3597: * ArrayUtils.add([false], 1, true) = [false, true]
3598: * ArrayUtils.add([true, false], 1, true) = [true, true, false]
3599: * </pre>
3600: *
3601: * @param array the array to add the element to, may be <code>null</code>
3602: * @param index the position of the new object
3603: * @param element the object to add
3604: * @return A new array containing the existing elements and the new element
3605: * @throws IndexOutOfBoundsException if the index is out of range
3606: * (index < 0 || index > array.length).
3607: */
3608: public static boolean[] add(boolean[] array, int index,
3609: boolean element) {
3610: return (boolean[]) add(array, index, BooleanUtils
3611: .toBooleanObject(element), Boolean.TYPE);
3612: }
3613:
3614: /**
3615: * <p>Inserts the specified element at the specified position in the array.
3616: * Shifts the element currently at that position (if any) and any subsequent
3617: * elements to the right (adds one to their indices).</p>
3618: *
3619: * <p>This method returns a new array with the same elements of the input
3620: * array plus the given element on the specified position. The component
3621: * type of the returned array is always the same as that of the input
3622: * array.</p>
3623: *
3624: * <p>If the input array is <code>null</code>, a new one element array is returned
3625: * whose component type is the same as the element.</p>
3626: *
3627: * <pre>
3628: * ArrayUtils.add(null, 0, 'a') = ['a']
3629: * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
3630: * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
3631: * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
3632: * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
3633: * </pre>
3634: *
3635: * @param array the array to add the element to, may be <code>null</code>
3636: * @param index the position of the new object
3637: * @param element the object to add
3638: * @return A new array containing the existing elements and the new element
3639: * @throws IndexOutOfBoundsException if the index is out of range
3640: * (index < 0 || index > array.length).
3641: */
3642: public static char[] add(char[] array, int index, char element) {
3643: return (char[]) add(array, index, new Character(element),
3644: Character.TYPE);
3645: }
3646:
3647: /**
3648: * <p>Inserts the specified element at the specified position in the array.
3649: * Shifts the element currently at that position (if any) and any subsequent
3650: * elements to the right (adds one to their indices).</p>
3651: *
3652: * <p>This method returns a new array with the same elements of the input
3653: * array plus the given element on the specified position. The component
3654: * type of the returned array is always the same as that of the input
3655: * array.</p>
3656: *
3657: * <p>If the input array is <code>null</code>, a new one element array is returned
3658: * whose component type is the same as the element.</p>
3659: *
3660: * <pre>
3661: * ArrayUtils.add([1], 0, 2) = [2, 1]
3662: * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
3663: * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
3664: * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3665: * </pre>
3666: *
3667: * @param array the array to add the element to, may be <code>null</code>
3668: * @param index the position of the new object
3669: * @param element the object to add
3670: * @return A new array containing the existing elements and the new element
3671: * @throws IndexOutOfBoundsException if the index is out of range
3672: * (index < 0 || index > array.length).
3673: */
3674: public static byte[] add(byte[] array, int index, byte element) {
3675: return (byte[]) add(array, index, new Byte(element), Byte.TYPE);
3676: }
3677:
3678: /**
3679: * <p>Inserts the specified element at the specified position in the array.
3680: * Shifts the element currently at that position (if any) and any subsequent
3681: * elements to the right (adds one to their indices).</p>
3682: *
3683: * <p>This method returns a new array with the same elements of the input
3684: * array plus the given element on the specified position. The component
3685: * type of the returned array is always the same as that of the input
3686: * array.</p>
3687: *
3688: * <p>If the input array is <code>null</code>, a new one element array is returned
3689: * whose component type is the same as the element.</p>
3690: *
3691: * <pre>
3692: * ArrayUtils.add([1], 0, 2) = [2, 1]
3693: * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
3694: * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
3695: * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3696: * </pre>
3697: *
3698: * @param array the array to add the element to, may be <code>null</code>
3699: * @param index the position of the new object
3700: * @param element the object to add
3701: * @return A new array containing the existing elements and the new element
3702: * @throws IndexOutOfBoundsException if the index is out of range
3703: * (index < 0 || index > array.length).
3704: */
3705: public static short[] add(short[] array, int index, short element) {
3706: return (short[]) add(array, index, new Short(element),
3707: Short.TYPE);
3708: }
3709:
3710: /**
3711: * <p>Inserts the specified element at the specified position in the array.
3712: * Shifts the element currently at that position (if any) and any subsequent
3713: * elements to the right (adds one to their indices).</p>
3714: *
3715: * <p>This method returns a new array with the same elements of the input
3716: * array plus the given element on the specified position. The component
3717: * type of the returned array is always the same as that of the input
3718: * array.</p>
3719: *
3720: * <p>If the input array is <code>null</code>, a new one element array is returned
3721: * whose component type is the same as the element.</p>
3722: *
3723: * <pre>
3724: * ArrayUtils.add([1], 0, 2) = [2, 1]
3725: * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
3726: * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
3727: * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3728: * </pre>
3729: *
3730: * @param array the array to add the element to, may be <code>null</code>
3731: * @param index the position of the new object
3732: * @param element the object to add
3733: * @return A new array containing the existing elements and the new element
3734: * @throws IndexOutOfBoundsException if the index is out of range
3735: * (index < 0 || index > array.length).
3736: */
3737: public static int[] add(int[] array, int index, int element) {
3738: return (int[]) add(array, index, new Integer(element),
3739: Integer.TYPE);
3740: }
3741:
3742: /**
3743: * <p>Inserts the specified element at the specified position in the array.
3744: * Shifts the element currently at that position (if any) and any subsequent
3745: * elements to the right (adds one to their indices).</p>
3746: *
3747: * <p>This method returns a new array with the same elements of the input
3748: * array plus the given element on the specified position. The component
3749: * type of the returned array is always the same as that of the input
3750: * array.</p>
3751: *
3752: * <p>If the input array is <code>null</code>, a new one element array is returned
3753: * whose component type is the same as the element.</p>
3754: *
3755: * <pre>
3756: * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
3757: * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
3758: * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
3759: * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
3760: * </pre>
3761: *
3762: * @param array the array to add the element to, may be <code>null</code>
3763: * @param index the position of the new object
3764: * @param element the object to add
3765: * @return A new array containing the existing elements and the new element
3766: * @throws IndexOutOfBoundsException if the index is out of range
3767: * (index < 0 || index > array.length).
3768: */
3769: public static long[] add(long[] array, int index, long element) {
3770: return (long[]) add(array, index, new Long(element), Long.TYPE);
3771: }
3772:
3773: /**
3774: * <p>Inserts the specified element at the specified position in the array.
3775: * Shifts the element currently at that position (if any) and any subsequent
3776: * elements to the right (adds one to their indices).</p>
3777: *
3778: * <p>This method returns a new array with the same elements of the input
3779: * array plus the given element on the specified position. The component
3780: * type of the returned array is always the same as that of the input
3781: * array.</p>
3782: *
3783: * <p>If the input array is <code>null</code>, a new one element array is returned
3784: * whose component type is the same as the element.</p>
3785: *
3786: * <pre>
3787: * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
3788: * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
3789: * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
3790: * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f]
3791: * </pre>
3792: *
3793: * @param array the array to add the element to, may be <code>null</code>
3794: * @param index the position of the new object
3795: * @param element the object to add
3796: * @return A new array containing the existing elements and the new element
3797: * @throws IndexOutOfBoundsException if the index is out of range
3798: * (index < 0 || index > array.length).
3799: */
3800: public static float[] add(float[] array, int index, float element) {
3801: return (float[]) add(array, index, new Float(element),
3802: Float.TYPE);
3803: }
3804:
3805: /**
3806: * <p>Inserts the specified element at the specified position in the array.
3807: * Shifts the element currently at that position (if any) and any subsequent
3808: * elements to the right (adds one to their indices).</p>
3809: *
3810: * <p>This method returns a new array with the same elements of the input
3811: * array plus the given element on the specified position. The component
3812: * type of the returned array is always the same as that of the input
3813: * array.</p>
3814: *
3815: * <p>If the input array is <code>null</code>, a new one element array is returned
3816: * whose component type is the same as the element.</p>
3817: *
3818: * <pre>
3819: * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
3820: * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
3821: * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
3822: * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
3823: * </pre>
3824: *
3825: * @param array the array to add the element to, may be <code>null</code>
3826: * @param index the position of the new object
3827: * @param element the object to add
3828: * @return A new array containing the existing elements and the new element
3829: * @throws IndexOutOfBoundsException if the index is out of range
3830: * (index < 0 || index > array.length).
3831: */
3832: public static double[] add(double[] array, int index, double element) {
3833: return (double[]) add(array, index, new Double(element),
3834: Double.TYPE);
3835: }
3836:
3837: /**
3838: * Underlying implementation of add(array, index, element) methods.
3839: * The last parameter is the class, which may not equal element.getClass
3840: * for primitives.
3841: *
3842: * @param array the array to add the element to, may be <code>null</code>
3843: * @param index the position of the new object
3844: * @param element the object to add
3845: * @param clss the type of the element being added
3846: * @return A new array containing the existing elements and the new element
3847: */
3848: private static Object add(Object array, int index, Object element,
3849: Class clss) {
3850: if (array == null) {
3851: if (index != 0) {
3852: throw new IndexOutOfBoundsException("Index: " + index
3853: + ", Length: 0");
3854: }
3855: Object joinedArray = Array.newInstance(clss, 1);
3856: Array.set(joinedArray, 0, element);
3857: return joinedArray;
3858: }
3859: int length = Array.getLength(array);
3860: if (index > length || index < 0) {
3861: throw new IndexOutOfBoundsException("Index: " + index
3862: + ", Length: " + length);
3863: }
3864: Object result = Array.newInstance(clss, length + 1);
3865: System.arraycopy(array, 0, result, 0, index);
3866: Array.set(result, index, element);
3867: if (index < length) {
3868: System.arraycopy(array, index, result, index + 1, length
3869: - index);
3870: }
3871: return result;
3872: }
3873:
3874: /**
3875: * <p>Removes the element at the specified position from the specified array.
3876: * All subsequent elements are shifted to the left (substracts one from
3877: * their indices).</p>
3878: *
3879: * <p>This method returns a new array with the same elements of the input
3880: * array except the element on the specified position. The component
3881: * type of the returned array is always the same as that of the input
3882: * array.</p>
3883: *
3884: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3885: * will be thrown, because in that case no valid index can be specified.</p>
3886: *
3887: * <pre>
3888: * ArrayUtils.remove(["a"], 0) = []
3889: * ArrayUtils.remove(["a", "b"], 0) = ["b"]
3890: * ArrayUtils.remove(["a", "b"], 1) = ["a"]
3891: * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
3892: * </pre>
3893: *
3894: * @param array the array to remove the element from, may not be <code>null</code>
3895: * @param index the position of the element to be removed
3896: * @return A new array containing the existing elements except the element
3897: * at the specified position.
3898: * @throws IndexOutOfBoundsException if the index is out of range
3899: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3900: * @since 2.1
3901: */
3902: public static Object[] remove(Object[] array, int index) {
3903: return (Object[]) remove((Object) array, index);
3904: }
3905:
3906: /**
3907: * <p>Removes the first occurrence of the specified element from the
3908: * specified array. All subsequent elements are shifted to the left
3909: * (substracts one from their indices). If the array doesn't contains
3910: * such an element, no elements are removed from the array.</p>
3911: *
3912: * <p>This method returns a new array with the same elements of the input
3913: * array except the first occurrence of the specified element. The component
3914: * type of the returned array is always the same as that of the input
3915: * array.</p>
3916: *
3917: * <pre>
3918: * ArrayUtils.removeElement(null, "a") = null
3919: * ArrayUtils.removeElement([], "a") = []
3920: * ArrayUtils.removeElement(["a"], "b") = ["a"]
3921: * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
3922: * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
3923: * </pre>
3924: *
3925: * @param array the array to remove the element from, may be <code>null</code>
3926: * @param element the element to be removed
3927: * @return A new array containing the existing elements except the first
3928: * occurrence of the specified element.
3929: * @since 2.1
3930: */
3931: public static Object[] removeElement(Object[] array, Object element) {
3932: int index = indexOf(array, element);
3933: if (index == INDEX_NOT_FOUND) {
3934: return clone(array);
3935: }
3936: return remove(array, index);
3937: }
3938:
3939: /**
3940: * <p>Removes the element at the specified position from the specified array.
3941: * All subsequent elements are shifted to the left (substracts one from
3942: * their indices).</p>
3943: *
3944: * <p>This method returns a new array with the same elements of the input
3945: * array except the element on the specified position. The component
3946: * type of the returned array is always the same as that of the input
3947: * array.</p>
3948: *
3949: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3950: * will be thrown, because in that case no valid index can be specified.</p>
3951: *
3952: * <pre>
3953: * ArrayUtils.remove([true], 0) = []
3954: * ArrayUtils.remove([true, false], 0) = [false]
3955: * ArrayUtils.remove([true, false], 1) = [true]
3956: * ArrayUtils.remove([true, true, false], 1) = [true, false]
3957: * </pre>
3958: *
3959: * @param array the array to remove the element from, may not be <code>null</code>
3960: * @param index the position of the element to be removed
3961: * @return A new array containing the existing elements except the element
3962: * at the specified position.
3963: * @throws IndexOutOfBoundsException if the index is out of range
3964: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3965: * @since 2.1
3966: */
3967: public static boolean[] remove(boolean[] array, int index) {
3968: return (boolean[]) remove((Object) array, index);
3969: }
3970:
3971: /**
3972: * <p>Removes the first occurrence of the specified element from the
3973: * specified array. All subsequent elements are shifted to the left
3974: * (substracts one from their indices). If the array doesn't contains
3975: * such an element, no elements are removed from the array.</p>
3976: *
3977: * <p>This method returns a new array with the same elements of the input
3978: * array except the first occurrence of the specified element. The component
3979: * type of the returned array is always the same as that of the input
3980: * array.</p>
3981: *
3982: * <pre>
3983: * ArrayUtils.removeElement(null, true) = null
3984: * ArrayUtils.removeElement([], true) = []
3985: * ArrayUtils.removeElement([true], false) = [true]
3986: * ArrayUtils.removeElement([true, false], false) = [true]
3987: * ArrayUtils.removeElement([true, false, true], true) = [false, true]
3988: * </pre>
3989: *
3990: * @param array the array to remove the element from, may be <code>null</code>
3991: * @param element the element to be removed
3992: * @return A new array containing the existing elements except the first
3993: * occurrence of the specified element.
3994: * @since 2.1
3995: */
3996: public static boolean[] removeElement(boolean[] array,
3997: boolean element) {
3998: int index = indexOf(array, element);
3999: if (index == INDEX_NOT_FOUND) {
4000: return clone(array);
4001: }
4002: return remove(array, index);
4003: }
4004:
4005: /**
4006: * <p>Removes the element at the specified position from the specified array.
4007: * All subsequent elements are shifted to the left (substracts one from
4008: * their indices).</p>
4009: *
4010: * <p>This method returns a new array with the same elements of the input
4011: * array except the element on the specified position. The component
4012: * type of the returned array is always the same as that of the input
4013: * array.</p>
4014: *
4015: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4016: * will be thrown, because in that case no valid index can be specified.</p>
4017: *
4018: * <pre>
4019: * ArrayUtils.remove([1], 0) = []
4020: * ArrayUtils.remove([1, 0], 0) = [0]
4021: * ArrayUtils.remove([1, 0], 1) = [1]
4022: * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
4023: * </pre>
4024: *
4025: * @param array the array to remove the element from, may not be <code>null</code>
4026: * @param index the position of the element to be removed
4027: * @return A new array containing the existing elements except the element
4028: * at the specified position.
4029: * @throws IndexOutOfBoundsException if the index is out of range
4030: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4031: * @since 2.1
4032: */
4033: public static byte[] remove(byte[] array, int index) {
4034: return (byte[]) remove((Object) array, index);
4035: }
4036:
4037: /**
4038: * <p>Removes the first occurrence of the specified element from the
4039: * specified array. All subsequent elements are shifted to the left
4040: * (substracts one from their indices). If the array doesn't contains
4041: * such an element, no elements are removed from the array.</p>
4042: *
4043: * <p>This method returns a new array with the same elements of the input
4044: * array except the first occurrence of the specified element. The component
4045: * type of the returned array is always the same as that of the input
4046: * array.</p>
4047: *
4048: * <pre>
4049: * ArrayUtils.removeElement(null, 1) = null
4050: * ArrayUtils.removeElement([], 1) = []
4051: * ArrayUtils.removeElement([1], 0) = [1]
4052: * ArrayUtils.removeElement([1, 0], 0) = [1]
4053: * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
4054: * </pre>
4055: *
4056: * @param array the array to remove the element from, may be <code>null</code>
4057: * @param element the element to be removed
4058: * @return A new array containing the existing elements except the first
4059: * occurrence of the specified element.
4060: * @since 2.1
4061: */
4062: public static byte[] removeElement(byte[] array, byte element) {
4063: int index = indexOf(array, element);
4064: if (index == INDEX_NOT_FOUND) {
4065: return clone(array);
4066: }
4067: return remove(array, index);
4068: }
4069:
4070: /**
4071: * <p>Removes the element at the specified position from the specified array.
4072: * All subsequent elements are shifted to the left (substracts one from
4073: * their indices).</p>
4074: *
4075: * <p>This method returns a new array with the same elements of the input
4076: * array except the element on the specified position. The component
4077: * type of the returned array is always the same as that of the input
4078: * array.</p>
4079: *
4080: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4081: * will be thrown, because in that case no valid index can be specified.</p>
4082: *
4083: * <pre>
4084: * ArrayUtils.remove(['a'], 0) = []
4085: * ArrayUtils.remove(['a', 'b'], 0) = ['b']
4086: * ArrayUtils.remove(['a', 'b'], 1) = ['a']
4087: * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
4088: * </pre>
4089: *
4090: * @param array the array to remove the element from, may not be <code>null</code>
4091: * @param index the position of the element to be removed
4092: * @return A new array containing the existing elements except the element
4093: * at the specified position.
4094: * @throws IndexOutOfBoundsException if the index is out of range
4095: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4096: * @since 2.1
4097: */
4098: public static char[] remove(char[] array, int index) {
4099: return (char[]) remove((Object) array, index);
4100: }
4101:
4102: /**
4103: * <p>Removes the first occurrence of the specified element from the
4104: * specified array. All subsequent elements are shifted to the left
4105: * (substracts one from their indices). If the array doesn't contains
4106: * such an element, no elements are removed from the array.</p>
4107: *
4108: * <p>This method returns a new array with the same elements of the input
4109: * array except the first occurrence of the specified element. The component
4110: * type of the returned array is always the same as that of the input
4111: * array.</p>
4112: *
4113: * <pre>
4114: * ArrayUtils.removeElement(null, 'a') = null
4115: * ArrayUtils.removeElement([], 'a') = []
4116: * ArrayUtils.removeElement(['a'], 'b') = ['a']
4117: * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
4118: * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
4119: * </pre>
4120: *
4121: * @param array the array to remove the element from, may be <code>null</code>
4122: * @param element the element to be removed
4123: * @return A new array containing the existing elements except the first
4124: * occurrence of the specified element.
4125: * @since 2.1
4126: */
4127: public static char[] removeElement(char[] array, char element) {
4128: int index = indexOf(array, element);
4129: if (index == INDEX_NOT_FOUND) {
4130: return clone(array);
4131: }
4132: return remove(array, index);
4133: }
4134:
4135: /**
4136: * <p>Removes the element at the specified position from the specified array.
4137: * All subsequent elements are shifted to the left (substracts one from
4138: * their indices).</p>
4139: *
4140: * <p>This method returns a new array with the same elements of the input
4141: * array except the element on the specified position. The component
4142: * type of the returned array is always the same as that of the input
4143: * array.</p>
4144: *
4145: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4146: * will be thrown, because in that case no valid index can be specified.</p>
4147: *
4148: * <pre>
4149: * ArrayUtils.remove([1.1], 0) = []
4150: * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
4151: * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
4152: * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
4153: * </pre>
4154: *
4155: * @param array the array to remove the element from, may not be <code>null</code>
4156: * @param index the position of the element to be removed
4157: * @return A new array containing the existing elements except the element
4158: * at the specified position.
4159: * @throws IndexOutOfBoundsException if the index is out of range
4160: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4161: * @since 2.1
4162: */
4163: public static double[] remove(double[] array, int index) {
4164: return (double[]) remove((Object) array, index);
4165: }
4166:
4167: /**
4168: * <p>Removes the first occurrence of the specified element from the
4169: * specified array. All subsequent elements are shifted to the left
4170: * (substracts one from their indices). If the array doesn't contains
4171: * such an element, no elements are removed from the array.</p>
4172: *
4173: * <p>This method returns a new array with the same elements of the input
4174: * array except the first occurrence of the specified element. The component
4175: * type of the returned array is always the same as that of the input
4176: * array.</p>
4177: *
4178: * <pre>
4179: * ArrayUtils.removeElement(null, 1.1) = null
4180: * ArrayUtils.removeElement([], 1.1) = []
4181: * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
4182: * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
4183: * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
4184: * </pre>
4185: *
4186: * @param array the array to remove the element from, may be <code>null</code>
4187: * @param element the element to be removed
4188: * @return A new array containing the existing elements except the first
4189: * occurrence of the specified element.
4190: * @since 2.1
4191: */
4192: public static double[] removeElement(double[] array, double element) {
4193: int index = indexOf(array, element);
4194: if (index == INDEX_NOT_FOUND) {
4195: return clone(array);
4196: }
4197: return remove(array, index);
4198: }
4199:
4200: /**
4201: * <p>Removes the element at the specified position from the specified array.
4202: * All subsequent elements are shifted to the left (substracts one from
4203: * their indices).</p>
4204: *
4205: * <p>This method returns a new array with the same elements of the input
4206: * array except the element on the specified position. The component
4207: * type of the returned array is always the same as that of the input
4208: * array.</p>
4209: *
4210: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4211: * will be thrown, because in that case no valid index can be specified.</p>
4212: *
4213: * <pre>
4214: * ArrayUtils.remove([1.1], 0) = []
4215: * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
4216: * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
4217: * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
4218: * </pre>
4219: *
4220: * @param array the array to remove the element from, may not be <code>null</code>
4221: * @param index the position of the element to be removed
4222: * @return A new array containing the existing elements except the element
4223: * at the specified position.
4224: * @throws IndexOutOfBoundsException if the index is out of range
4225: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4226: * @since 2.1
4227: */
4228: public static float[] remove(float[] array, int index) {
4229: return (float[]) remove((Object) array, index);
4230: }
4231:
4232: /**
4233: * <p>Removes the first occurrence of the specified element from the
4234: * specified array. All subsequent elements are shifted to the left
4235: * (substracts one from their indices). If the array doesn't contains
4236: * such an element, no elements are removed from the array.</p>
4237: *
4238: * <p>This method returns a new array with the same elements of the input
4239: * array except the first occurrence of the specified element. The component
4240: * type of the returned array is always the same as that of the input
4241: * array.</p>
4242: *
4243: * <pre>
4244: * ArrayUtils.removeElement(null, 1.1) = null
4245: * ArrayUtils.removeElement([], 1.1) = []
4246: * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
4247: * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
4248: * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
4249: * </pre>
4250: *
4251: * @param array the array to remove the element from, may be <code>null</code>
4252: * @param element the element to be removed
4253: * @return A new array containing the existing elements except the first
4254: * occurrence of the specified element.
4255: * @since 2.1
4256: */
4257: public static float[] removeElement(float[] array, float element) {
4258: int index = indexOf(array, element);
4259: if (index == INDEX_NOT_FOUND) {
4260: return clone(array);
4261: }
4262: return remove(array, index);
4263: }
4264:
4265: /**
4266: * <p>Removes the element at the specified position from the specified array.
4267: * All subsequent elements are shifted to the left (substracts one from
4268: * their indices).</p>
4269: *
4270: * <p>This method returns a new array with the same elements of the input
4271: * array except the element on the specified position. The component
4272: * type of the returned array is always the same as that of the input
4273: * array.</p>
4274: *
4275: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4276: * will be thrown, because in that case no valid index can be specified.</p>
4277: *
4278: * <pre>
4279: * ArrayUtils.remove([1], 0) = []
4280: * ArrayUtils.remove([2, 6], 0) = [6]
4281: * ArrayUtils.remove([2, 6], 1) = [2]
4282: * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4283: * </pre>
4284: *
4285: * @param array the array to remove the element from, may not be <code>null</code>
4286: * @param index the position of the element to be removed
4287: * @return A new array containing the existing elements except the element
4288: * at the specified position.
4289: * @throws IndexOutOfBoundsException if the index is out of range
4290: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4291: * @since 2.1
4292: */
4293: public static int[] remove(int[] array, int index) {
4294: return (int[]) remove((Object) array, index);
4295: }
4296:
4297: /**
4298: * <p>Removes the first occurrence of the specified element from the
4299: * specified array. All subsequent elements are shifted to the left
4300: * (substracts one from their indices). If the array doesn't contains
4301: * such an element, no elements are removed from the array.</p>
4302: *
4303: * <p>This method returns a new array with the same elements of the input
4304: * array except the first occurrence of the specified element. The component
4305: * type of the returned array is always the same as that of the input
4306: * array.</p>
4307: *
4308: * <pre>
4309: * ArrayUtils.removeElement(null, 1) = null
4310: * ArrayUtils.removeElement([], 1) = []
4311: * ArrayUtils.removeElement([1], 2) = [1]
4312: * ArrayUtils.removeElement([1, 3], 1) = [3]
4313: * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4314: * </pre>
4315: *
4316: * @param array the array to remove the element from, may be <code>null</code>
4317: * @param element the element to be removed
4318: * @return A new array containing the existing elements except the first
4319: * occurrence of the specified element.
4320: * @since 2.1
4321: */
4322: public static int[] removeElement(int[] array, int element) {
4323: int index = indexOf(array, element);
4324: if (index == INDEX_NOT_FOUND) {
4325: return clone(array);
4326: }
4327: return remove(array, index);
4328: }
4329:
4330: /**
4331: * <p>Removes the element at the specified position from the specified array.
4332: * All subsequent elements are shifted to the left (substracts one from
4333: * their indices).</p>
4334: *
4335: * <p>This method returns a new array with the same elements of the input
4336: * array except the element on the specified position. The component
4337: * type of the returned array is always the same as that of the input
4338: * array.</p>
4339: *
4340: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4341: * will be thrown, because in that case no valid index can be specified.</p>
4342: *
4343: * <pre>
4344: * ArrayUtils.remove([1], 0) = []
4345: * ArrayUtils.remove([2, 6], 0) = [6]
4346: * ArrayUtils.remove([2, 6], 1) = [2]
4347: * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4348: * </pre>
4349: *
4350: * @param array the array to remove the element from, may not be <code>null</code>
4351: * @param index the position of the element to be removed
4352: * @return A new array containing the existing elements except the element
4353: * at the specified position.
4354: * @throws IndexOutOfBoundsException if the index is out of range
4355: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4356: * @since 2.1
4357: */
4358: public static long[] remove(long[] array, int index) {
4359: return (long[]) remove((Object) array, index);
4360: }
4361:
4362: /**
4363: * <p>Removes the first occurrence of the specified element from the
4364: * specified array. All subsequent elements are shifted to the left
4365: * (substracts one from their indices). If the array doesn't contains
4366: * such an element, no elements are removed from the array.</p>
4367: *
4368: * <p>This method returns a new array with the same elements of the input
4369: * array except the first occurrence of the specified element. The component
4370: * type of the returned array is always the same as that of the input
4371: * array.</p>
4372: *
4373: * <pre>
4374: * ArrayUtils.removeElement(null, 1) = null
4375: * ArrayUtils.removeElement([], 1) = []
4376: * ArrayUtils.removeElement([1], 2) = [1]
4377: * ArrayUtils.removeElement([1, 3], 1) = [3]
4378: * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4379: * </pre>
4380: *
4381: * @param array the array to remove the element from, may be <code>null</code>
4382: * @param element the element to be removed
4383: * @return A new array containing the existing elements except the first
4384: * occurrence of the specified element.
4385: * @since 2.1
4386: */
4387: public static long[] removeElement(long[] array, long element) {
4388: int index = indexOf(array, element);
4389: if (index == INDEX_NOT_FOUND) {
4390: return clone(array);
4391: }
4392: return remove(array, index);
4393: }
4394:
4395: /**
4396: * <p>Removes the element at the specified position from the specified array.
4397: * All subsequent elements are shifted to the left (substracts one from
4398: * their indices).</p>
4399: *
4400: * <p>This method returns a new array with the same elements of the input
4401: * array except the element on the specified position. The component
4402: * type of the returned array is always the same as that of the input
4403: * array.</p>
4404: *
4405: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4406: * will be thrown, because in that case no valid index can be specified.</p>
4407: *
4408: * <pre>
4409: * ArrayUtils.remove([1], 0) = []
4410: * ArrayUtils.remove([2, 6], 0) = [6]
4411: * ArrayUtils.remove([2, 6], 1) = [2]
4412: * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4413: * </pre>
4414: *
4415: * @param array the array to remove the element from, may not be <code>null</code>
4416: * @param index the position of the element to be removed
4417: * @return A new array containing the existing elements except the element
4418: * at the specified position.
4419: * @throws IndexOutOfBoundsException if the index is out of range
4420: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4421: * @since 2.1
4422: */
4423: public static short[] remove(short[] array, int index) {
4424: return (short[]) remove((Object) array, index);
4425: }
4426:
4427: /**
4428: * <p>Removes the first occurrence of the specified element from the
4429: * specified array. All subsequent elements are shifted to the left
4430: * (substracts one from their indices). If the array doesn't contains
4431: * such an element, no elements are removed from the array.</p>
4432: *
4433: * <p>This method returns a new array with the same elements of the input
4434: * array except the first occurrence of the specified element. The component
4435: * type of the returned array is always the same as that of the input
4436: * array.</p>
4437: *
4438: * <pre>
4439: * ArrayUtils.removeElement(null, 1) = null
4440: * ArrayUtils.removeElement([], 1) = []
4441: * ArrayUtils.removeElement([1], 2) = [1]
4442: * ArrayUtils.removeElement([1, 3], 1) = [3]
4443: * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4444: * </pre>
4445: *
4446: * @param array the array to remove the element from, may be <code>null</code>
4447: * @param element the element to be removed
4448: * @return A new array containing the existing elements except the first
4449: * occurrence of the specified element.
4450: * @since 2.1
4451: */
4452: public static short[] removeElement(short[] array, short element) {
4453: int index = indexOf(array, element);
4454: if (index == INDEX_NOT_FOUND) {
4455: return clone(array);
4456: }
4457: return remove(array, index);
4458: }
4459:
4460: /**
4461: * <p>Removes the element at the specified position from the specified array.
4462: * All subsequent elements are shifted to the left (substracts one from
4463: * their indices).</p>
4464: *
4465: * <p>This method returns a new array with the same elements of the input
4466: * array except the element on the specified position. The component
4467: * type of the returned array is always the same as that of the input
4468: * array.</p>
4469: *
4470: * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4471: * will be thrown, because in that case no valid index can be specified.</p>
4472: *
4473: * @param array the array to remove the element from, may not be <code>null</code>
4474: * @param index the position of the element to be removed
4475: * @return A new array containing the existing elements except the element
4476: * at the specified position.
4477: * @throws IndexOutOfBoundsException if the index is out of range
4478: * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4479: * @since 2.1
4480: */
4481: private static Object remove(Object array, int index) {
4482: int length = getLength(array);
4483: if (index < 0 || index >= length) {
4484: throw new IndexOutOfBoundsException("Index: " + index
4485: + ", Length: " + length);
4486: }
4487:
4488: Object result = Array.newInstance(array.getClass()
4489: .getComponentType(), length - 1);
4490: System.arraycopy(array, 0, result, 0, index);
4491: if (index < length - 1) {
4492: System.arraycopy(array, index + 1, result, index, length
4493: - index - 1);
4494: }
4495:
4496: return result;
4497: }
4498:
4499: }
|