This is an automated email from the ASF dual-hosted git repository. daim pushed a commit to branch OAK-11528 in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 561dfd5136c29ba4fafca58a59418899c9dc8b01 Author: Rishabh Kumar <[email protected]> AuthorDate: Tue Feb 25 20:05:32 2025 +0530 OAK-11528 : added util method for replacing Guava's Iterables.transform in oak-commons module --- .../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); + } }
