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 2f16a088d9 OAK-11523 : added util method for replceing Guava's
Iterables.filter … (#2114)
2f16a088d9 is described below
commit 2f16a088d9644ce419672205bfa27dc11362b57a
Author: Rishabh Kumar <[email protected]>
AuthorDate: Tue Feb 25 15:24:23 2025 +0530
OAK-11523 : added util method for replceing Guava's Iterables.filter …
(#2114)
* OAK-11523 : added util method for replceing Guava's Iterables.filter in
oak-commons module
* OAK-11523 : added util method for replacing Iterables.filter's Class Type
API
---------
Co-authored-by: Rishabh Kumar <[email protected]>
---
.../oak/commons/collections/IterableUtils.java | 28 ++++++
.../oak/commons/collections/IterableUtilsTest.java | 99 ++++++++++++++++++++++
2 files changed, 127 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 bea321d4b0..171dc3604c 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
@@ -30,6 +30,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
+import java.util.stream.Collectors;
/**
* Utility methods for {@link Iterable} conversions.
@@ -260,4 +261,31 @@ public class IterableUtils {
}
};
}
+
+ /**
+ * Filters an Iterable based on a given predicate.
+ *
+ * @param <E> the type of elements in the iterable
+ * @param itr the iterable to filter, may not be null
+ * @param predicate the predicate to apply to elements, may not be null
+ * @return an iterable containing only the elements that match the
predicate
+ * @throws NullPointerException if the iterable or predicate is null
+ */
+ public static <E> Iterable<E> filter(final Iterable<E> itr, final
Predicate<? super E> predicate) {
+ return
org.apache.commons.collections4.IterableUtils.filteredIterable(itr, predicate);
+ }
+
+ /**
+ * Filters an Iterable to include only elements of the specified class
type.
+ *
+ * @param <E> the type of elements to include
+ * @param itr the iterable to filter, may not be null
+ * @param type the class type to filter by, may not be null
+ * @return an iterable containing only the elements of the specified class
type
+ * @throws NullPointerException if the iterable or class type is null
+ */
+ @SuppressWarnings("unchecked")
+ public static <E> Iterable<E> filter(final Iterable<?> itr, final Class<E>
type) {
+ return (Iterable<E>)
StreamUtils.toStream(itr).filter(type::isInstance).collect(Collectors.toList());
+ }
}
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 1f30edca6b..3a741e7532 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
@@ -393,4 +393,103 @@ public class IterableUtilsTest {
Assert.assertFalse(iterator.hasNext());
Assert.assertThrows(NoSuchElementException.class, iterator::next);
}
+
+ @Test
+ public void testFilterWithNonEmptyIterable() {
+ Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5);
+ Predicate<Integer> predicate = x -> x % 2 == 0;
+ Iterable<Integer> filtered = IterableUtils.filter(iterable, predicate);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertEquals(Arrays.asList(2, 4), result);
+ }
+
+ @Test
+ public void testFilterWithEmptyIterable() {
+ Iterable<Integer> iterable = Collections.emptyList();
+ Predicate<Integer> predicate = x -> x % 2 == 0;
+ Iterable<Integer> filtered = IterableUtils.filter(iterable, predicate);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testFilterWithAllMatchingElements() {
+ Iterable<Integer> iterable = Arrays.asList(2, 4, 6);
+ Predicate<Integer> predicate = x -> x % 2 == 0;
+ Iterable<Integer> filtered = IterableUtils.filter(iterable, predicate);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertEquals(Arrays.asList(2, 4, 6), result);
+ }
+
+ @Test
+ public void testFilterWithNoMatchingElements() {
+ Iterable<Integer> iterable = Arrays.asList(1, 3, 5);
+ Predicate<Integer> predicate = x -> x % 2 == 0;
+ Iterable<Integer> filtered = IterableUtils.filter(iterable, predicate);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testFilterWithNullIterable() {
+ Predicate<Integer> predicate = x -> x % 2 == 0;
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.filter(null, predicate);
+ });
+ }
+
+ @Test
+ public void testFilterWithNullPredicate() {
+ Iterable<Integer> iterable = Arrays.asList(1, 2, 3);
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.filter(iterable, (Predicate)null);
+ });
+ }
+
+ @Test
+ public void testFilterByClassTypeWithNonEmptyIterable() {
+ Iterable<Object> iterable = Arrays.asList(1, "two", 3, "four", 5.0, 6);
+ Iterable<Integer> filtered = IterableUtils.filter(iterable,
Integer.class);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertEquals(Arrays.asList(1, 3, 6), result);
+ }
+
+ @Test
+ public void testFilterByClassTypeWithEmptyIterable() {
+ Iterable<Object> iterable = Collections.emptyList();
+ Iterable<Integer> filtered = IterableUtils.filter(iterable,
Integer.class);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testFilterByClassTypeWithAllMatchingElements() {
+ Iterable<Object> iterable = Arrays.asList(1, 2, 3);
+ Iterable<Integer> filtered = IterableUtils.filter(iterable,
Integer.class);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertEquals(Arrays.asList(1, 2, 3), result);
+ }
+
+ @Test
+ public void testFilterByClassTypeWithNoMatchingElements() {
+ Iterable<Object> iterable = Arrays.asList("one", "two", "three");
+ Iterable<Integer> filtered = IterableUtils.filter(iterable,
Integer.class);
+ List<Integer> result = ListUtils.toList(filtered.iterator());
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testFilterByClassTypeWithNullIterable() {
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.filter(null, Integer.class);
+ });
+ }
+
+ @Test
+ public void testFilterByClassTypeWithNullClassType() {
+ Iterable<Object> iterable = Arrays.asList(1, 2, 3);
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.filter(iterable, (Class)null);
+ });
+ }
}