This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11494 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit fcf4dd5c3af519cf6607b979d2187f10fc8d20b5 Author: Rishabh Kumar <[email protected]> AuthorDate: Tue Feb 18 14:35:33 2025 +0530 OAK-11494 : added utils class for repalcing Guava's Iterables.contains --- .../oak/commons/collections/IterableUtils.java | 13 ++++++++++ .../oak/commons/collections/IterableUtilsTest.java | 29 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java index 9c717115a1..42e237df7e 100644 --- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java @@ -142,4 +142,17 @@ public class IterableUtils { } }; } + + /** + * Checks if the specified object is present in the given iterable. + * + * @param <E> the type of elements in the iterable + * @param iterable the iterable to search, may not be null + * @param object the object to find, may be null + * @return true if the iterable contains the object, false otherwise + * @throws NullPointerException if the iterable is null + */ + public static <E> boolean contains(final Iterable<E> iterable, final Object object) { + return org.apache.commons.collections4.IterableUtils.contains(iterable, object); + } } diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java index 6a620a6fc6..b2ee530863 100644 --- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java +++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java @@ -166,4 +166,33 @@ public class IterableUtilsTest { // now next iterator should be null Assert.assertThrows(NullPointerException.class, iterator::hasNext); } + + @Test + public void testContainsWithNonNullElement() { + Iterable<String> iterable = List.of("a", "b", "c"); + Assert.assertTrue(IterableUtils.contains(iterable, "b")); + } + + @Test + public void testContainsWithNullElement() { + Iterable<String> iterable = Arrays.asList("a", "b", "c", null); + Assert.assertTrue(IterableUtils.contains(iterable, null)); + } + + @Test + public void testContainsWithEmptyIterable() { + Iterable<String> iterable = List.of(); + Assert.assertFalse(IterableUtils.contains(iterable, "a")); + } + + @Test + public void testContainsWithElementNotPresent() { + Iterable<String> iterable = List.of("a", "b", "c"); + Assert.assertFalse(IterableUtils.contains(iterable, "d")); + } + + @Test + public void testContainsWithNullIterable() { + Assert.assertFalse(IterableUtils.contains(null, "a")); + } }
