Skip to content

Commit f6d2039

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11566 : added util to replace Iterables.find in oak-commons (#2161)
Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent 449b735 commit f6d2039

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IterableUtils.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.jackrabbit.oak.commons.collections;
2020

21-
import org.apache.commons.collections4.Predicate;
2221
import org.apache.commons.collections4.iterators.LazyIteratorChain;
2322
import org.apache.jackrabbit.oak.commons.conditions.Validate;
2423
import org.jetbrains.annotations.NotNull;
@@ -32,6 +31,7 @@
3231
import java.util.NoSuchElementException;
3332
import java.util.Objects;
3433
import java.util.function.Function;
34+
import java.util.function.Predicate;
3535
import java.util.stream.Collectors;
3636

3737
/**
@@ -188,7 +188,7 @@ public static int size(final Iterable<?> itr) {
188188
* @throws NullPointerException if the iterable or predicate is null
189189
*/
190190
public static <E> boolean matchesAll(final Iterable<E> itr, final Predicate<? super E> predicate) {
191-
return org.apache.commons.collections4.IterableUtils.matchesAll(itr, predicate);
191+
return org.apache.commons.collections4.IterableUtils.matchesAll(itr, predicate::test);
192192
}
193193

194194
/**
@@ -274,7 +274,7 @@ public List<T> next() {
274274
* @throws NullPointerException if the iterable or predicate is null
275275
*/
276276
public static <E> Iterable<E> filter(final Iterable<E> itr, final Predicate<? super E> predicate) {
277-
return org.apache.commons.collections4.IterableUtils.filteredIterable(itr, predicate);
277+
return org.apache.commons.collections4.IterableUtils.filteredIterable(itr, predicate::test);
278278
}
279279

280280
/**
@@ -428,4 +428,22 @@ public static <T> T get(final Iterable<T> iterable, final int index) {
428428
Objects.requireNonNull(iterable, "Iterable must not be null.");
429429
return org.apache.commons.collections4.IterableUtils.get(iterable, index);
430430
}
431+
432+
/**
433+
* Returns the first element in the specified iterable that matches the given predicate.
434+
* <p>
435+
* The iterable is traversed until an element is found that satisfies the predicate.
436+
* If no element satisfies the predicate, {@code null} is returned.
437+
*
438+
* @param <T> the type of elements in the iterable
439+
* @param iterable the iterable to search, must not be null
440+
* @param predicate the predicate to apply, must not be null
441+
* @return the first element that satisfies the predicate, or null if no such element exists
442+
* @throws NullPointerException if either the iterable or predicate is null
443+
*/
444+
public static <T> T find(final Iterable<T> iterable, final Predicate<? super T> predicate) {
445+
Objects.requireNonNull(iterable, "Iterable must not be null.");
446+
Objects.requireNonNull(predicate, "Predicate must not be null.");
447+
return org.apache.commons.collections4.IterableUtils.find(iterable, predicate::test);
448+
}
431449
}

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IterableUtilsTest.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.jackrabbit.oak.commons.collections;
2020

21-
import org.apache.commons.collections4.Predicate;
2221
import org.junit.Assert;
2322
import org.junit.Test;
2423

@@ -31,6 +30,7 @@
3130
import java.util.List;
3231
import java.util.NoSuchElementException;
3332
import java.util.function.Function;
33+
import java.util.function.Predicate;
3434

3535
/**
3636
* Unit tests for the {@link IterableUtils} class.
@@ -969,4 +969,65 @@ public void testGetWithSingleElementIterable() {
969969
String result = IterableUtils.get(list, 0);
970970
Assert.assertEquals("only", result);
971971
}
972+
973+
@Test
974+
public void testFindWithMatchingElement() {
975+
List<String> list = Arrays.asList("apple", "banana", "cherry");
976+
String result = IterableUtils.find(list, s -> s.startsWith("b"));
977+
Assert.assertEquals("banana", result);
978+
}
979+
980+
@Test
981+
public void testFindWithNoMatchingElement() {
982+
List<String> list = Arrays.asList("apple", "banana", "cherry");
983+
String result = IterableUtils.find(list, s -> s.startsWith("d"));
984+
Assert.assertNull(result);
985+
}
986+
987+
@Test
988+
public void testFindWithMultipleMatchingElements() {
989+
List<String> list = Arrays.asList("apple", "avocado", "banana", "apricot");
990+
String result = IterableUtils.find(list, s -> s.startsWith("a"));
991+
// Should return the first matching element
992+
Assert.assertEquals("apple", result);
993+
}
994+
995+
@Test
996+
public void testFindWithEmptyIterable() {
997+
List<String> list = Collections.emptyList();
998+
String result = IterableUtils.find(list, s -> true);
999+
Assert.assertNull(result);
1000+
}
1001+
1002+
@Test(expected = NullPointerException.class)
1003+
public void testFindWithNullIterable() {
1004+
IterableUtils.find(null, s -> true);
1005+
}
1006+
1007+
@Test(expected = NullPointerException.class)
1008+
public void testFindWithNullPredicate() {
1009+
List<String> list = Arrays.asList("apple", "banana", "cherry");
1010+
IterableUtils.find(list, null);
1011+
}
1012+
1013+
@Test
1014+
public void testFindFirstElement() {
1015+
List<Integer> list = Arrays.asList(10, 20, 30, 40, 50);
1016+
Integer result = IterableUtils.find(list, i -> i > 5);
1017+
Assert.assertEquals(Integer.valueOf(10), result);
1018+
}
1019+
1020+
@Test
1021+
public void testFindLastElement() {
1022+
List<Integer> list = Arrays.asList(10, 20, 30, 40, 50);
1023+
Integer result = IterableUtils.find(list, i -> i > 45);
1024+
Assert.assertEquals(Integer.valueOf(50), result);
1025+
}
1026+
1027+
@Test
1028+
public void testFindWithCustomIterable() {
1029+
Iterable<Integer> customIterable = () -> Arrays.asList(5, 10, 15, 20, 25).iterator();
1030+
Integer result = IterableUtils.find(customIterable, i -> i % 10 == 0);
1031+
Assert.assertEquals(Integer.valueOf(10), result);
1032+
}
9721033
}

0 commit comments

Comments
 (0)