You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2025/02/26 09:25:04 UTC
(jackrabbit-oak) branch trunk updated: OAK-11528 : added util method for replacing Guava's Iterables.transform in oak-commons module (#2117)
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 77b7999340 OAK-11528 : added util method for replacing Guava's Iterables.transform in oak-commons module (#2117)
77b7999340 is described below
commit 77b799934028af9349de31da66f6499560bbf930
Author: Rishabh Kumar <ri...@gmail.com>
AuthorDate: Wed Feb 26 14:54:58 2025 +0530
OAK-11528 : added util method for replacing Guava's Iterables.transform in oak-commons module (#2117)
Co-authored-by: Rishabh Kumar <di...@adobe.com>
---
.../oak/commons/collections/IterableUtils.java | 15 ++++++++
.../oak/commons/collections/IterableUtilsTest.java | 44 ++++++++++++++++++++++
2 files changed, 59 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 171dc3604c..9d20b68acb 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.function.Function;
import java.util.stream.Collectors;
/**
@@ -288,4 +289,18 @@ public class IterableUtils {
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());
}
+
+ /**
+ * Transforms an Iterable by applying a given function to each element.
+ *
+ * @param <I> the type of input elements
+ * @param <O> the type of output elements
+ * @param iterable the iterable to transform, must not be null
+ * @param function the function to apply to each element, must not be null
+ * @return an iterable containing the transformed elements
+ * @throws NullPointerException if the iterable or function is null
+ */
+ public static <I, O> Iterable<O> transform(final Iterable<I> iterable, final Function<? super I, ? extends O> function) {
+ return org.apache.commons.collections4.IterableUtils.transformedIterable(iterable, function::apply);
+ }
}
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 3a741e7532..ab9208e754 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
@@ -27,6 +27,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
+import java.util.function.Function;
/**
* Unit tests for the {@link IterableUtils} class.
@@ -492,4 +493,47 @@ public class IterableUtilsTest {
IterableUtils.filter(iterable, (Class)null);
});
}
+
+ @Test
+ public void testTransformWithNonEmptyIterable() {
+ Iterable<Integer> iterable = Arrays.asList(1, 2, 3);
+ Function<Integer, String> function = Object::toString;
+ Iterable<String> transformed = IterableUtils.transform(iterable, function);
+ List<String> result = ListUtils.toList(transformed.iterator());
+ Assert.assertEquals(Arrays.asList("1", "2", "3"), result);
+ }
+
+ @Test
+ public void testTransformWithEmptyIterable() {
+ Iterable<Integer> iterable = Collections.emptyList();
+ Function<Integer, String> function = Object::toString;
+ Iterable<String> transformed = IterableUtils.transform(iterable, function);
+ List<String> result = ListUtils.toList(transformed.iterator());
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testTransformWithNullIterable() {
+ Function<Integer, String> function = Object::toString;
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.transform(null, function);
+ });
+ }
+
+ @Test
+ public void testTransformWithNullFunction() {
+ Iterable<Integer> iterable = Arrays.asList(1, 2, 3);
+ Assert.assertThrows(NullPointerException.class, () -> {
+ IterableUtils.transform(iterable, null);
+ });
+ }
+
+ @Test
+ public void testTransformWithComplexFunction() {
+ Iterable<String> iterable = Arrays.asList("a", "bb", "ccc");
+ Function<String, Integer> function = String::length;
+ Iterable<Integer> transformed = IterableUtils.transform(iterable, function);
+ List<Integer> result = ListUtils.toList(transformed.iterator());
+ Assert.assertEquals(Arrays.asList(1, 2, 3), result);
+ }
}