Add getLength method

bug 26594, from Maarten Coene


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137789 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-02-14 01:15:20 +00:00
parent 7ba2d8aa0d
commit 20393e09aa
2 changed files with 171 additions and 13 deletions

View File

@ -80,8 +80,9 @@ import org.apache.commons.lang.builder.ToStringStyle;
* @author Pete Gieser * @author Pete Gieser
* @author Gary Gregory * @author Gary Gregory
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a> * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Maarten Coene
* @since 2.0 * @since 2.0
* @version $Id: ArrayUtils.java,v 1.40 2004/02/03 22:14:24 ggregory Exp $ * @version $Id: ArrayUtils.java,v 1.41 2004/02/14 01:15:19 scolebourne Exp $
*/ */
public class ArrayUtils { public class ArrayUtils {
@ -955,6 +956,56 @@ public class ArrayUtils {
return true; return true;
} }
//-----------------------------------------------------------------------
/**
* <p>Returns the length of the specified array.
* This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
*
* <p>If the input array is <code>null</code>, <code>0</code> is returned.</p>
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or <code>0</code> if the array is <code>null</code>
* @throws IllegalArgumentException if the object arguement is not an array.
*/
public static int getLength(final Object array) {
if (array == null) {
return 0;
} else {
return Array.getLength(array);
}
}
/**
* Returns the last index of the given array or -1 if empty or null.
* This method can deal with <code>Object</code> arrays and with primitive arrays.
* This value is one less than the size since arrays indices are 0-based.</p>
*
* <pre>
* ArrayUtils.lastIndex(null) = -1
* ArrayUtils.lastIndex([]) = -1
* ArrayUtils.lastIndex([null]) = 0
* ArrayUtils.lastIndex([true, false]) = 1
* ArrayUtils.lastIndex([1, 2, 3]) = 2
* ArrayUtils.lastIndex(["a", "b", "c"]) = 2
* </pre>
*
* @param array the array to return the last index for, may be null
* @return the last index, -1 if empty or null
* @throws IllegalArgumentException if the object arguement is not an array.
*/
public static int lastIndex(final Object array) {
return ArrayUtils.getLength(array) - 1;
}
/** /**
* <p>Checks whether two arrays are the same type taking into account * <p>Checks whether two arrays are the same type taking into account
* multi-dimensional arrays.</p> * multi-dimensional arrays.</p>
@ -3051,17 +3102,6 @@ public class ArrayUtils {
} }
} }
/**
* Returns the last index of the given array. This value is one less than the size since
* arrays indices are 0-based.
*
* @param array The array to return the last index for, must not be <code>null</code>.
* @return The last index
*/
public static int lastIndex(final Object array) {
return Array.getLength(array) - 1;
}
/** /**
* <p>Inserts the specified element at the specified position in the array. * <p>Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent * Shifts the element currently at that position (if any) and any subsequent
@ -3111,4 +3151,5 @@ public class ArrayUtils {
} }
return (Object[]) result; return (Object[]) result;
} }
} }

View File

@ -75,7 +75,8 @@ import junit.textui.TestRunner;
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a> * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Fredrik Westermarck * @author Fredrik Westermarck
* @author Gary Gregory * @author Gary Gregory
* @version $Id: ArrayUtilsTest.java,v 1.24 2004/02/03 22:14:24 ggregory Exp $ * @author Maarten Coene
* @version $Id: ArrayUtilsTest.java,v 1.25 2004/02/14 01:15:20 scolebourne Exp $
*/ */
public class ArrayUtilsTest extends TestCase { public class ArrayUtilsTest extends TestCase {
@ -2362,4 +2363,120 @@ public class ArrayUtilsTest extends TestCase {
assertEquals(true, ArrayUtils.isEmpty(emptyBooleanArray)); assertEquals(true, ArrayUtils.isEmpty(emptyBooleanArray));
assertEquals(false, ArrayUtils.isEmpty(notEmptyBooleanArray)); assertEquals(false, ArrayUtils.isEmpty(notEmptyBooleanArray));
} }
// ------------------------------------------------------------------------
public void testGetLength() {
assertEquals(0, ArrayUtils.getLength(null));
Object[] emptyObjectArray = new Object[0];
Object[] notEmptyObjectArray = new Object[] {"aValue"};
assertEquals(0, ArrayUtils.getLength((Object[]) null));
assertEquals(0, ArrayUtils.getLength(emptyObjectArray));
assertEquals(1, ArrayUtils.getLength(notEmptyObjectArray));
int[] emptyIntArray = new int[] {};
int[] notEmptyIntArray = new int[] { 1 };
assertEquals(0, ArrayUtils.getLength((int[]) null));
assertEquals(0, ArrayUtils.getLength(emptyIntArray));
assertEquals(1, ArrayUtils.getLength(notEmptyIntArray));
short[] emptyShortArray = new short[] {};
short[] notEmptyShortArray = new short[] { 1 };
assertEquals(0, ArrayUtils.getLength((short[]) null));
assertEquals(0, ArrayUtils.getLength(emptyShortArray));
assertEquals(1, ArrayUtils.getLength(notEmptyShortArray));
char[] emptyCharArray = new char[] {};
char[] notEmptyCharArray = new char[] { 1 };
assertEquals(0, ArrayUtils.getLength((char[]) null));
assertEquals(0, ArrayUtils.getLength(emptyCharArray));
assertEquals(1, ArrayUtils.getLength(notEmptyCharArray));
byte[] emptyByteArray = new byte[] {};
byte[] notEmptyByteArray = new byte[] { 1 };
assertEquals(0, ArrayUtils.getLength((byte[]) null));
assertEquals(0, ArrayUtils.getLength(emptyByteArray));
assertEquals(1, ArrayUtils.getLength(notEmptyByteArray));
double[] emptyDoubleArray = new double[] {};
double[] notEmptyDoubleArray = new double[] { 1.0 };
assertEquals(0, ArrayUtils.getLength((double[]) null));
assertEquals(0, ArrayUtils.getLength(emptyDoubleArray));
assertEquals(1, ArrayUtils.getLength(notEmptyDoubleArray));
float[] emptyFloatArray = new float[] {};
float[] notEmptyFloatArray = new float[] { 1.0F };
assertEquals(0, ArrayUtils.getLength((float[]) null));
assertEquals(0, ArrayUtils.getLength(emptyFloatArray));
assertEquals(1, ArrayUtils.getLength(notEmptyFloatArray));
boolean[] emptyBooleanArray = new boolean[] {};
boolean[] notEmptyBooleanArray = new boolean[] { true };
assertEquals(0, ArrayUtils.getLength((boolean[]) null));
assertEquals(0, ArrayUtils.getLength(emptyBooleanArray));
assertEquals(1, ArrayUtils.getLength(notEmptyBooleanArray));
try {
ArrayUtils.getLength("notAnArray");
fail("IllegalArgumentException should have been thrown");
} 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) {}
}
} }