Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,4 +168,17 @@ public static <E> boolean contains(final Iterable<E> iterable, final Object obje
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -218,4 +219,39 @@ public void testSizeWithSingleElement() {
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);
});
}
}
Loading