Remove lastIndex(), which is better done as getLength() - 1
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f4f34dc60a
commit
097aa30a36
|
@ -44,7 +44,7 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
|||
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
|
||||
* @author Maarten Coene
|
||||
* @since 2.0
|
||||
* @version $Id: ArrayUtils.java,v 1.48 2004/10/09 11:55:51 scolebourne Exp $
|
||||
* @version $Id: ArrayUtils.java,v 1.49 2004/10/10 18:56:16 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtils {
|
||||
|
||||
|
@ -956,29 +956,6 @@ public class ArrayUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last index of the given array or -1 if empty or null.
|
||||
* This method can deal with <code>Object</code> arrays and with primitive arrays.
|
||||
* This value is one less than the size since arrays indices are 0-based.</p>
|
||||
*
|
||||
* <pre>
|
||||
* ArrayUtils.lastIndex(null) = -1
|
||||
* ArrayUtils.lastIndex([]) = -1
|
||||
* ArrayUtils.lastIndex([null]) = 0
|
||||
* ArrayUtils.lastIndex([true, false]) = 1
|
||||
* ArrayUtils.lastIndex([1, 2, 3]) = 2
|
||||
* ArrayUtils.lastIndex(["a", "b", "c"]) = 2
|
||||
* </pre>
|
||||
*
|
||||
* @param array the array to return the last index for, may be null
|
||||
* @return the last index, -1 if empty or null
|
||||
* @throws IllegalArgumentException if the object arguement is not an array.
|
||||
* @since 2.1
|
||||
*/
|
||||
public static int lastIndex(Object array) {
|
||||
return ArrayUtils.getLength(array) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks whether two arrays are the same type taking into account
|
||||
* multi-dimensional arrays.</p>
|
||||
|
|
|
@ -38,7 +38,7 @@ import junit.textui.TestRunner;
|
|||
* @author Fredrik Westermarck
|
||||
* @author Gary Gregory
|
||||
* @author Maarten Coene
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.28 2004/08/25 21:20:13 ggregory Exp $
|
||||
* @version $Id: ArrayUtilsTest.java,v 1.29 2004/10/10 18:56:16 scolebourne Exp $
|
||||
*/
|
||||
public class ArrayUtilsTest extends TestCase {
|
||||
|
||||
|
@ -2491,61 +2491,4 @@ public class ArrayUtilsTest extends TestCase {
|
|||
} catch (IllegalArgumentException e) {}
|
||||
}
|
||||
|
||||
public void testLastIndex() {
|
||||
assertEquals(-1, ArrayUtils.lastIndex(null));
|
||||
|
||||
Object[] emptyObjectArray = new Object[0];
|
||||
Object[] notEmptyObjectArray = new Object[] {"aValue"};
|
||||
assertEquals(-1, ArrayUtils.lastIndex((Object[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyObjectArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyObjectArray));
|
||||
|
||||
int[] emptyIntArray = new int[] {};
|
||||
int[] notEmptyIntArray = new int[] { 1 };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((int[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyIntArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyIntArray));
|
||||
|
||||
short[] emptyShortArray = new short[] {};
|
||||
short[] notEmptyShortArray = new short[] { 1 };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((short[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyShortArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyShortArray));
|
||||
|
||||
char[] emptyCharArray = new char[] {};
|
||||
char[] notEmptyCharArray = new char[] { 1 };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((char[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyCharArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyCharArray));
|
||||
|
||||
byte[] emptyByteArray = new byte[] {};
|
||||
byte[] notEmptyByteArray = new byte[] { 1 };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((byte[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyByteArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyByteArray));
|
||||
|
||||
double[] emptyDoubleArray = new double[] {};
|
||||
double[] notEmptyDoubleArray = new double[] { 1.0 };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((double[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyDoubleArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyDoubleArray));
|
||||
|
||||
float[] emptyFloatArray = new float[] {};
|
||||
float[] notEmptyFloatArray = new float[] { 1.0F };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((float[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyFloatArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyFloatArray));
|
||||
|
||||
boolean[] emptyBooleanArray = new boolean[] {};
|
||||
boolean[] notEmptyBooleanArray = new boolean[] { true };
|
||||
assertEquals(-1, ArrayUtils.lastIndex((boolean[]) null));
|
||||
assertEquals(-1, ArrayUtils.lastIndex(emptyBooleanArray));
|
||||
assertEquals(0, ArrayUtils.lastIndex(notEmptyBooleanArray));
|
||||
|
||||
try {
|
||||
ArrayUtils.lastIndex("notAnArray");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
} catch (IllegalArgumentException e) {}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue