This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new 846ec69591 OAK-11496 : added utils method for repalcing Guava's
Iterables.all (#2096)
846ec69591 is described below
commit 846ec69591f10f5d1c7424ae79de2b2650bedd0d
Author: Rishabh Kumar <[email protected]>
AuthorDate: Wed Feb 19 13:25:21 2025 +0530
OAK-11496 : added utils method for repalcing Guava's Iterables.all (#2096)
Co-authored-by: Rishabh Kumar <[email protected]>
---
.../oak/commons/collections/IterableUtils.java | 15 +++++++++
.../oak/commons/collections/IterableUtilsTest.java | 36 ++++++++++++++++++++++
2 files changed, 51 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 b420af18ec..15e3cb9f63 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
@@ -18,6 +18,8 @@
*/
package org.apache.jackrabbit.oak.commons.collections;
+import org.apache.commons.collections4.IteratorUtils;
+import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.iterators.LazyIteratorChain;
import java.util.Iterator;
@@ -166,4 +168,17 @@ public class IterableUtils {
public static int size(final Iterable<?> itr) {
return org.apache.commons.collections4.IterableUtils.size(itr);
}
+
+ /**
+ * Checks if all elements in the specified iterable match the given
predicate.
+ *
+ * @param <E> the type of elements in the iterable
+ * @param itr the iterable to check, may not be null
+ * @param predicate the predicate to apply to elements, may not be null
+ * @return true if all elements match the predicate, false otherwise
+ * @throws NullPointerException if the iterable or predicate is null
+ */
+ public static <E> boolean matchesAll(final Iterable<E> itr, final
Predicate<? super E> predicate) {
+ return org.apache.commons.collections4.IterableUtils.matchesAll(itr,
predicate);
+ }
}
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 de2f1bfab2..3f468924ed 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
@@ -18,6 +18,7 @@
*/
package org.apache.jackrabbit.oak.commons.collections;
+import org.apache.commons.collections4.Predicate;
import org.junit.Assert;
import org.junit.Test;
@@ -218,4 +219,39 @@ public class IterableUtilsTest {
public void testSizeWithNullIterable() {
Assert.assertEquals(0, IterableUtils.size(null));
}
+
+ @Test
+ public void testMatchesAllWithAllMatchingElements() {
+ Iterable<Integer> iterable = Arrays.asList(2, 4, 6);
+ Predicate<Integer> isEven = x -> x % 2 == 0;
+ Assert.assertTrue(IterableUtils.matchesAll(iterable, isEven));
+ }
+
+ @Test
+ public void testMatchesAllWithSomeNonMatchingElements() {
+ Iterable<Integer> iterable = Arrays.asList(2, 3, 6);
+ Predicate<Integer> isEven = x -> x % 2 == 0;
+ Assert.assertFalse(IterableUtils.matchesAll(iterable, isEven));
+ }
+
+ @Test
+ public void testMatchesAllWithEmptyIterable() {
+ Iterable<Integer> iterable = Collections.emptyList();
+ Predicate<Integer> isEven = x -> x % 2 == 0;
+ Assert.assertTrue(IterableUtils.matchesAll(iterable, isEven));
+ }
+
+ @Test
+ public void testMatchesAllWithNullIterable() {
+ Predicate<Integer> isEven = x -> x % 2 == 0;
+ Assert.assertTrue(IterableUtils.matchesAll(null, isEven));
+ }
+
+ @Test
+ public void testMatchesAllWithNullPredicate() {
+ Iterable<Integer> iterable = Arrays.asList(2, 4, 6);
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.matchesAll(iterable, null);
+ });
+ }
}