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

Add IntStrams.of(int...)
This commit is contained in:
Gary Gregory 2024-10-31 10:50:50 -04:00
parent 62b5acac4c
commit 85f91f4208
5 changed files with 53 additions and 1 deletions

View File

@ -68,6 +68,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_23.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IntegerRange.toIntStream().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LongRange.toLongStream().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IntStrams.of(int...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.containsAny(int[], int...).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 78 #1267, #1277, #1283, #1288, #1302.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>

View File

@ -39,6 +39,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.IntStreams;
import org.apache.commons.lang3.stream.Streams;
/**
@ -1734,6 +1735,25 @@ public class ArrayUtils {
return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
}
/**
* Checks if any of the ints are in the given array.
* <p>
* The method returns {@code false} if a {@code null} array is passed in.
* </p>
* <p>
* If the {@code array} elements you are searching implement {@link Comparator}, consider whether it is worth using
* {@link Arrays#sort(int[])} and {@link Arrays#binarySearch(int[], int)}.
* </p>
*
* @param array the array to search through
* @param objectsToFind any of the ints to find
* @return {@code true} if the array contains any of the ints
* @since 3.18.0
*/
public static boolean containsAny(final int[] array, final int... objectsToFind) {
return IntStreams.of(objectsToFind).anyMatch(e -> contains(array, e));
}
/**
* Checks if any of the objects are in the given array.
* <p>

View File

@ -28,6 +28,18 @@ import java.util.stream.IntStream;
*/
public class IntStreams {
/**
* Null-safe version of {@link IntStream#of(int[])}.
*
* @param values the elements of the new stream, may be {@code null}.
* @return the new stream on {@code values} or {@link IntStream#empty()}.
* @since 3.18.0
*/
@SafeVarargs // Creating a stream from an array is safe
public static IntStream of(final int... values) {
return values == null ? IntStream.empty() : IntStream.of(values);
}
/**
* Shorthand for {@code IntStream.range(0, i)}.
*

View File

@ -252,7 +252,17 @@ public class ArrayUtilsTest extends AbstractLangTest {
}
@Test
public void testContainsAny() {
public void testContainsAnyInt() {
final int[] array = {0, 1, 2, 3, 0};
assertFalse(ArrayUtils.containsAny((int[]) null, 1));
assertTrue(ArrayUtils.containsAny(array, 0));
assertTrue(ArrayUtils.containsAny(array, 1));
assertTrue(ArrayUtils.containsAny(array, 2));
assertTrue(ArrayUtils.containsAny(array, 3));
}
@Test
public void testContainsAnyObject() {
final Object[] array = {"0", "1", "2", "3", null, "0"};
assertFalse(ArrayUtils.containsAny(null, (Object) null));
assertFalse(ArrayUtils.containsAny(null, "1"));

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;
@ -26,6 +27,13 @@ import org.junit.jupiter.api.Test;
*/
public class IntStreamsTest extends AbstractLangTest {
@Test
public void testOfArray() {
assertEquals(0, IntStreams.of((int[]) null).count());
assertEquals(1, IntStreams.of(1).count());
assertEquals(2, IntStreams.of(1, 2).count());
}
@Test
public void testRange() {
assertArrayEquals(new int[] {0, 1}, IntStreams.range(2).toArray());