Javadoc & format

Clean up new test
This commit is contained in:
Gary Gregory 2024-01-20 10:07:16 -05:00
parent 7663a113b4
commit 10cf18310e
2 changed files with 31 additions and 43 deletions

View File

@ -22,9 +22,8 @@ package org.apache.commons.collections4;
* Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}). * Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}).
* </p> * </p>
* <p> * <p>
* This class tries to handle {@code null} input gracefully. An exception will not be thrown for a {@code null} array * This class tries to handle {@code null} input gracefully. An exception will not be thrown for a {@code null} array input. However, an Object array that
* input. However, an Object array that contains a {@code null} element may throw an exception. Each method documents * contains a {@code null} element may throw an exception. Each method documents its behavior.
* its behavior.
* </p> * </p>
* <p> * <p>
* Package private, might move to an internal package if this needs to be public. * Package private, might move to an internal package if this needs to be public.
@ -40,20 +39,19 @@ final class ArrayUtils {
/** /**
* Don't allow instances. * Don't allow instances.
*/ */
private ArrayUtils() {} private ArrayUtils() {
}
/** /**
* <p> * <p>
* Checks if the object is in the given array. * Checks if the object is in the given array.
* </p> * </p>
*
* <p> * <p>
* The method returns {@code false} if a {@code null} array is passed in. * The method returns {@code false} if a {@code null} array is passed in.
* </p> * </p>
* *
* @param array * @param array the array to search through
* the array to search through * @param objectToFind the object to find
* @param objectToFind
* the object to find
* @return {@code true} if the array contains the object * @return {@code true} if the array contains the object
*/ */
static boolean contains(final Object[] array, final Object objectToFind) { static boolean contains(final Object[] array, final Object objectToFind) {
@ -64,17 +62,13 @@ final class ArrayUtils {
* <p> * <p>
* Finds the index of the given object in the array. * Finds the index of the given object in the array.
* </p> * </p>
*
* <p> * <p>
* This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
* </p> * </p>
* *
* @param array * @param array the array to search through for the object, may be {@code null}
* the array to search through for the object, may be {@code null} * @param objectToFind the object to find, may be {@code null}
* @param objectToFind * @return the index of the object within the array, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
* the object to find, may be {@code null}
* @return the index of the object within the array, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or
* {@code null} array input
*/ */
static <T> int indexOf(final T[] array, final Object objectToFind) { static <T> int indexOf(final T[] array, final Object objectToFind) {
return indexOf(array, objectToFind, 0); return indexOf(array, objectToFind, 0);
@ -84,24 +78,18 @@ final class ArrayUtils {
* <p> * <p>
* Finds the index of the given object in the array starting at the given index. * Finds the index of the given object in the array starting at the given index.
* </p> * </p>
*
* <p> * <p>
* This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
* </p> * </p>
*
* <p> * <p>
* A negative startIndex is treated as zero. A startIndex larger than the array length will return * A negative startIndex is treated as zero. A startIndex larger than the array length will return {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
* {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
* </p> * </p>
* *
* @param array * @param array the array to search through for the object, may be {@code null}
* the array to search through for the object, may be {@code null} * @param objectToFind the object to find, may be {@code null}
* @param objectToFind * @param startIndex the index to start searching at
* the object to find, may be {@code null} * @return the index of the object within the array starting at the index, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
* @param startIndex * array input
* the index to start searching at
* @return the index of the object within the array starting at the index, {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) if
* not found or {@code null} array input
*/ */
static int indexOf(final Object[] array, final Object objectToFind, int startIndex) { static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
if (array == null) { if (array == null) {

View File

@ -24,23 +24,9 @@ import org.junit.jupiter.api.Test;
public class ArrayUtilsTest { public class ArrayUtilsTest {
@Test
public void testIndexOf() {
final Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.indexOf(null, null));
assertEquals(-1, ArrayUtils.indexOf(null, "0"));
assertEquals(-1, ArrayUtils.indexOf(new Object[0], "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"));
}
@Test @Test
public void testContains() { public void testContains() {
final Object[] array = new Object[] { "0", "1", "2", "3", null, "0" }; final Object[] array = { "0", "1", "2", "3", null, "0" };
assertFalse(ArrayUtils.contains(null, null)); assertFalse(ArrayUtils.contains(null, null));
assertFalse(ArrayUtils.contains(null, "1")); assertFalse(ArrayUtils.contains(null, "1"));
assertTrue(ArrayUtils.contains(array, "0")); assertTrue(ArrayUtils.contains(array, "0"));
@ -64,4 +50,18 @@ public class ArrayUtilsTest {
final Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() }; final Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() };
assertTrue(ArrayUtils.contains(array, new LANG1261ParentObject())); assertTrue(ArrayUtils.contains(array, new LANG1261ParentObject()));
} }
@Test
public void testIndexOf() {
final Object[] array = { "0", "1", "2", "3", null, "0" };
assertEquals(-1, ArrayUtils.indexOf(null, null));
assertEquals(-1, ArrayUtils.indexOf(null, "0"));
assertEquals(-1, ArrayUtils.indexOf(new Object[0], "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"));
}
} }