Add ArrayUtils.containsAny(Object[], Object...).

Primitive versions are TODOs.
This commit is contained in:
Gary Gregory 2021-12-08 13:29:16 -05:00
parent 4369537d8b
commit 5def1c8d63
3 changed files with 31 additions and 1 deletions

View File

@ -113,6 +113,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add MutableTriple.ofNonNull(L, M, R).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Pair.ofNonNull(L, R).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Triple.ofNonNull(L, M, R).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.containsAny(Object[], Object...).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.5.0.0 #735, #808, #822, #834.</action>
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump actions/cache from v2.1.4 to v2.1.7 #742, #752, #764, #833.</action>

View File

@ -36,6 +36,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.stream.Streams;
/**
* Operations on arrays, primitive arrays (like {@code int[]}) and
@ -1652,6 +1653,21 @@ public class ArrayUtils {
return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
}
/**
* Checks if any of the objects are in the given array.
* <p>
* The method returns {@code false} if a {@code null} array is passed in.
* </p>
*
* @param array the array to search through
* @param objectsToFind any of the objects to find
* @return {@code true} if the array contains any of the objects
* @since 3.13.0
*/
public static boolean containsAny(final Object[] array, final Object... objectsToFind) {
return Streams.of(objectsToFind).anyMatch(e -> contains(array, e));
}
/**
* Checks if the value is in the given array.
* <p>

View File

@ -208,8 +208,21 @@ public class ArrayUtilsTest {
assertTrue(ArrayUtils.contains(array, "1"));
assertTrue(ArrayUtils.contains(array, "2"));
assertTrue(ArrayUtils.contains(array, "3"));
assertTrue(ArrayUtils.contains(array, null));
assertFalse(ArrayUtils.contains(array, "notInArray"));
assertTrue(ArrayUtils.contains(array, null));
}
@Test
public void testContainsAny() {
final Object[] array = {"0", "1", "2", "3", null, "0"};
assertFalse(ArrayUtils.containsAny(null, null));
assertFalse(ArrayUtils.containsAny(null, "1"));
assertTrue(ArrayUtils.containsAny(array, "0"));
assertTrue(ArrayUtils.containsAny(array, "1"));
assertTrue(ArrayUtils.containsAny(array, "2"));
assertTrue(ArrayUtils.containsAny(array, "3"));
assertFalse(ArrayUtils.containsAny(array, "notInArray"));
assertTrue(ArrayUtils.containsAny(array, new String[] {null}));
}
@Test