Add support for indexOf, lastIndexOf and contains for ArrayUtils

from Nikolay Metchev, bug ref 15438


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137249 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-02-04 22:06:24 +00:00
parent bdbde6db66
commit 4c63a72053
2 changed files with 209 additions and 18 deletions

View File

@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -67,8 +67,9 @@
* @author Stephen Colebourne
* @author Moritz Petersen
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
* @author Nikolay Metchev
* @since 2.0
* @version $Id: ArrayUtils.java,v 1.7 2002/12/23 00:32:24 scolebourne Exp $
* @version $Id: ArrayUtils.java,v 1.8 2003/02/04 22:06:24 scolebourne Exp $
*/
public class ArrayUtils {
@ -104,7 +105,7 @@ public ArrayUtils() {
}
// Basic methods handling multi-dimensional arrays
//--------------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
@ -166,7 +167,7 @@ public static boolean isEquals(Object array1, Object array2) {
return new EqualsBuilder().append(array1, array2).isEquals();
}
//--------------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* <p>Converts the given array into a {@link Map}. Each element of the array
@ -324,7 +325,7 @@ public static Map toMap(Object[] array) {
// return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
// }
//--------------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* <p>Shallow clones an array returning a typecast result and handling
@ -480,7 +481,7 @@ public static boolean[] clone(boolean[] array) {
return (boolean[]) array.clone();
}
//--------------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* <p>Checks whether two arrays are the same length, treating
@ -680,6 +681,8 @@ public static boolean isSameType(Object array1, Object array2) {
return array1.getClass().getName().equals(array2.getClass().getName());
}
//-----------------------------------------------------------------------
/**
* Reverses the order of the given array.
* <p>
@ -687,7 +690,7 @@ public static boolean isSameType(Object array1, Object array2) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(Object[] array) {
if (array == null) {
@ -710,7 +713,7 @@ public static void reverse(Object[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(long[] array) {
if (array == null) {
@ -733,7 +736,7 @@ public static void reverse(long[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(int[] array) {
if (array == null) {
@ -756,7 +759,7 @@ public static void reverse(int[] array) {
* <p>
* There is no special handling for multi-dimensional arrays.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(short[] array) {
if (array == null) {
@ -779,7 +782,7 @@ public static void reverse(short[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(char[] array) {
if (array == null) {
@ -802,7 +805,7 @@ public static void reverse(char[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(byte[] array) {
if (array == null) {
@ -825,7 +828,7 @@ public static void reverse(byte[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(double[] array) {
if (array == null) {
@ -848,7 +851,7 @@ public static void reverse(double[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(float[] array) {
if (array == null) {
@ -871,7 +874,7 @@ public static void reverse(float[] array) {
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(boolean[] array) {
if (array == null) {
@ -888,5 +891,125 @@ public static void reverse(boolean[] array) {
i++;
}
}
//-----------------------------------------------------------------------
/**
* Find the index of the given object in the array.
* <p>
* The method returns -1 if a <code>null</code> array is passed in.
*
* @param array the array to search through for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @return the index of the object within the array, or -1 if not found
*/
public static int indexOf(Object[] array, Object objectToFind) {
return indexOf(array, objectToFind, 0);
}
/**
* Find the index of the given object in the array starting at the given index.
* <p>
* The method returns -1 if a <code>null</code> array is passed in.
* <p>
* A negative startIndex is treated as zero. A startIndex larger than the array
* length will return -1.
*
* @param array the array to search through for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @param startIndex the index to start searching at
* @return the index of the object within the array starting at the
* given index, or -1 if not found
*/
public static int indexOf(Object[] array, Object objectToFind, int startIndex) {
if (array == null) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
if (objectToFind == null) {
for (int i = startIndex; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = startIndex; i < array.length; i++) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* Find the last index of the given object within the array.
* <p>
* The method returns -1 if a <code>null</code> array is passed in.
*
* @param array the array to travers backwords looking for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @return the last index of the object to find, or -1 if not found
*/
public static int lastIndexOf(Object[] array, Object objectToFind) {
if (array == null) {
return -1;
}
return lastIndexOf(array, objectToFind, array.length - 1);
}
/**
* Find the last index of the given object in the array starting at the given index.
* <p>
* The method returns -1 if a <code>null</code> array is passed in.
* <p>
* A negative startIndex will return -1. A startIndex larger than the array
* length will search from the end of the array.
*
* @param array the array to traverse for looking for the object, may be <code>null</code>
* @param objectToFind the object to find, may be <code>null</code>
* @param startIndex the start index to travers backwards from
* @return the last index of the object within the array starting at the given index,
* or -1 if not found
*/
public static int lastIndexOf(Object[] array, Object objectToFind, int startIndex) {
if (array == null) {
return -1;
}
if (startIndex < 0) {
return -1;
} else if (startIndex >= array.length) {
startIndex = array.length - 1;
}
if (objectToFind == null) {
for (int i = startIndex; i >= 0; i--) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = startIndex; i >= 0; i--) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return -1;
}
/**
* Checks if the object is in the given array.
* <p>
* The method returns <code>false</code> if a <code>null</code> array is passed in.
*
* @param array the array to search through
* @param objectToFind the object to find
* @return <code>true</code> if the array contains the object
*/
public static boolean contains(Object[] array, Object objectToFind) {
return (indexOf(array, objectToFind) != -1);
}
}

View File

@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -65,7 +65,8 @@
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author Moritz Petersen
* @version $Id: ArrayUtilsTest.java,v 1.4 2002/12/15 15:00:46 scolebourne Exp $
* @author Nikolay Metchev
* @version $Id: ArrayUtilsTest.java,v 1.5 2003/02/04 22:06:24 scolebourne Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -637,4 +638,71 @@ public void testReverseBoolean() {
assertEquals(null, array);
}
//-----------------------------------------------------------------------
public void testIndexOf() {
Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.indexOf(null, null));
assertEquals(-1, ArrayUtils.indexOf(null, "0"));
assertEquals(0, ArrayUtils.indexOf(array, "0"));
assertEquals(1, ArrayUtils.indexOf(array, "1"));
assertEquals(2, ArrayUtils.indexOf(array, "2"));
assertEquals(3, ArrayUtils.indexOf(array, "3"));
assertEquals(4, ArrayUtils.indexOf(array, null));
assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
}
public void testIndexOfWithStartIndex() {
Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.indexOf(null, null, 2));
assertEquals(-1, ArrayUtils.indexOf(null, "0", 2));
assertEquals(5, ArrayUtils.indexOf(array, "0", 2));
assertEquals(-1, ArrayUtils.indexOf(array, "1", 2));
assertEquals(2, ArrayUtils.indexOf(array, "2", 2));
assertEquals(3, ArrayUtils.indexOf(array, "3", 2));
assertEquals(4, ArrayUtils.indexOf(array, null, 2));
assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
assertEquals(4, ArrayUtils.indexOf(array, null, -1));
assertEquals(-1, ArrayUtils.indexOf(array, "0", 6));
}
public void testLastIndexOf() {
Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.lastIndexOf(null, null));
assertEquals(-1, ArrayUtils.lastIndexOf(null, "0"));
assertEquals(5, ArrayUtils.lastIndexOf(array, "0"));
assertEquals(1, ArrayUtils.lastIndexOf(array, "1"));
assertEquals(2, ArrayUtils.lastIndexOf(array, "2"));
assertEquals(3, ArrayUtils.lastIndexOf(array, "3"));
assertEquals(4, ArrayUtils.lastIndexOf(array, null));
assertEquals(-1, ArrayUtils.lastIndexOf(array, "notInArray"));
}
public void testLastIndexOfWithStartIndex() {
Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.lastIndexOf(null, null, 2));
assertEquals(-1, ArrayUtils.lastIndexOf(null, "0", 2));
assertEquals(0, ArrayUtils.lastIndexOf(array, "0", 2));
assertEquals(1, ArrayUtils.lastIndexOf(array, "1", 2));
assertEquals(2, ArrayUtils.lastIndexOf(array, "2", 2));
assertEquals(-1, ArrayUtils.lastIndexOf(array, "3", 2));
assertEquals(4, ArrayUtils.lastIndexOf(array, null, 5));
assertEquals(-1, ArrayUtils.lastIndexOf(array, null, 2));
assertEquals(-1, ArrayUtils.lastIndexOf(array, "notInArray"));
assertEquals(-1, ArrayUtils.lastIndexOf(array, null, -1));
assertEquals(5, ArrayUtils.lastIndexOf(array, "0", 88));
}
public void testContains() {
Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(false, ArrayUtils.contains(null, null));
assertEquals(false, ArrayUtils.contains(null, "1"));
assertEquals(true, ArrayUtils.contains(array, "0"));
assertEquals(true, ArrayUtils.contains(array, "1"));
assertEquals(true, ArrayUtils.contains(array, "2"));
assertEquals(true, ArrayUtils.contains(array, "3"));
assertEquals(true, ArrayUtils.contains(array, null));
assertEquals(false, ArrayUtils.contains(array, "notInArray"));
}
}