Skip to content

Commit

Permalink
Javadoc & format
Browse files Browse the repository at this point in the history
Clean up new test
  • Loading branch information
garydgregory committed Jan 20, 2024
1 parent 7663a11 commit 10cf183
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
44 changes: 16 additions & 28 deletions src/main/java/org/apache/commons/collections4/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
* Operations on arrays, primitive arrays (like {@code int[]}) and primitive wrapper arrays (like {@code Integer[]}).
* </p>
* <p>
* 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 contains a {@code null} element may throw an exception. Each method documents
* its behavior.
* 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
* contains a {@code null} element may throw an exception. Each method documents its behavior.
* </p>
* <p>
* Package private, might move to an internal package if this needs to be public.
Expand All @@ -40,20 +39,19 @@ final class ArrayUtils {
/**
* Don't allow instances.
*/
private ArrayUtils() {}
private ArrayUtils() {
}

/**
* <p>
* Checks if the object is in the given array.
* </p>
*
* <p>
* The method returns {@code false} if a {@code null} array is passed in.
* </p>
*
* @param array
* the array to search through
* @param objectToFind
* the object to find
* @param array the array to search through
* @param objectToFind the object to find
* @return {@code true} if the array contains the object
*/
static boolean contains(final Object[] array, final Object objectToFind) {
Expand All @@ -64,17 +62,13 @@ static boolean contains(final Object[] array, final Object objectToFind) {
* <p>
* Finds the index of the given object in the array.
* </p>
*
* <p>
* This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
* </p>
*
* @param array
* the array to search through for the object, may be {@code null}
* @param objectToFind
* 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
* @param array the array to search through for the object, may be {@code null}
* @param objectToFind 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) {
return indexOf(array, objectToFind, 0);
Expand All @@ -84,24 +78,18 @@ static <T> int indexOf(final T[] array, final Object objectToFind) {
* <p>
* Finds the index of the given object in the array starting at the given index.
* </p>
*
* <p>
* This method returns {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
* </p>
*
* <p>
* A negative startIndex is treated as zero. A startIndex larger than the array length will return
* {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
* A negative startIndex is treated as zero. A startIndex larger than the array length will return {@link CollectionUtils#INDEX_NOT_FOUND} ({@code -1}).
* </p>
*
* @param array
* the array to search through for the object, may be {@code null}
* @param objectToFind
* the object to find, may be {@code null}
* @param startIndex
* 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
* @param array the array to search through for the object, may be {@code null}
* @param objectToFind the object to find, may be {@code null}
* @param startIndex 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) {
if (array == null) {
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/org/apache/commons/collections4/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,9 @@

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
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, "1"));
assertTrue(ArrayUtils.contains(array, "0"));
Expand All @@ -64,4 +50,18 @@ class LANG1261ChildObject extends LANG1261ParentObject {
final Object[] array = new LANG1261ChildObject[] { new LANG1261ChildObject() };
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"));
}
}

0 comments on commit 10cf183

Please sign in to comment.