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 f0ecc26d01 OAK-12081 : added isInOrder() in IterableUtils to check
whether all element of given iterable are in order as specified by given
comparator (#2713)
f0ecc26d01 is described below
commit f0ecc26d01664e40f98d883c09adb23497919f4b
Author: Rishabh Kumar <[email protected]>
AuthorDate: Thu Jan 29 19:00:57 2026 +0530
OAK-12081 : added isInOrder() in IterableUtils to check whether all element
of given iterable are in order as specified by given comparator (#2713)
---
.../oak/commons/collections/IterableUtils.java | 35 +++++++++
.../oak/commons/collections/package-info.java | 2 +-
.../oak/commons/collections/IterableUtilsTest.java | 82 ++++++++++++++++++++++
3 files changed, 118 insertions(+), 1 deletion(-)
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 f7e490d68c..d5538b691b 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
@@ -451,4 +451,39 @@ public class IterableUtils {
return last;
}
+
+ /**
+ * Returns {@code true} if the elements of the given {@link Iterable} are
in
+ * non-decreasing order according to the supplied {@link Comparator}.
+ * Consecutive elements {@code a}, {@code b} are considered in order if
+ * {@code comparator.compare(a, b) <= 0}.
+ * <p>
+ * Empty iterables and single-element iterables are always considered to
be in order.
+ *
+ * @param iterable the elements to check; must not be {@code null}
+ * @param comparator the comparator defining the desired order; must not
be {@code null}
+ * @param <T> the element type
+ * @return {@code true} if the iterable is in non-decreasing order
according to
+ * {@code comparator}, otherwise {@code false}
+ * @throws NullPointerException if {@code iterable} or {@code comparator}
is {@code null}
+ */
+ public static <T> boolean isInOrder(final Iterable<? extends T> iterable,
final Comparator<T> comparator) {
+ Objects.requireNonNull(iterable, "iterable");
+ Objects.requireNonNull(comparator, "comparator");
+ Iterator<? extends T> it = iterable.iterator();
+ if (!it.hasNext()) {
+ return true;
+ }
+
+ T prev = it.next();
+ while (it.hasNext()) {
+ T next = it.next();
+ if (comparator.compare(prev, next) > 0) {
+ return false;
+ }
+ prev = next;
+ }
+ return true;
+ }
+
}
diff --git
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java
index 5d1490f210..f158c4fa7e 100644
---
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java
+++
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/package-info.java
@@ -21,7 +21,7 @@
* Utilities for Java collections and streams.
*/
@Internal(since = "1.0.0")
-@Version("2.3.0")
+@Version("2.4.0")
package org.apache.jackrabbit.oak.commons.collections;
import org.apache.jackrabbit.oak.commons.annotations.Internal;
import org.osgi.annotation.versioning.Version;
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 7b8e17336b..567159f890 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
@@ -42,6 +42,8 @@ import java.util.function.Predicate;
*/
public class IterableUtilsTest {
+ private static final Comparator<Integer> INT_COMPARATOR =
Integer::compareTo;
+
@Test
public void testTwoChainedIterable() {
@@ -1127,4 +1129,84 @@ public class IterableUtilsTest {
String result = IterableUtils.getLast(set);
Assert.assertEquals("c", result);
}
+
+ @Test
+ public void testIsInOrderNullValues() {
+ Assert.assertThrows(NullPointerException.class, () ->
IterableUtils.isInOrder(null, INT_COMPARATOR));
+ Assert.assertThrows(NullPointerException.class, () ->
IterableUtils.isInOrder(Collections.<Integer>emptyList(), null));
+ }
+
+ @Test
+ public void isInOrder_emptyIterableIsInOrder() {
+ List<Integer> list = Collections.emptyList();
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertTrue("Empty iterable should be considered in order",
result);
+ }
+
+ @Test
+ public void isInOrder_singleElementIterableIsInOrder() {
+ List<Integer> list = Collections.singletonList(42);
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertTrue("Single-element iterable should be considered in
order", result);
+ }
+
+ @Test
+ public void isInOrder_sortedAscendingIsTrue() {
+ List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertTrue("Ascending list should be in order", result);
+ }
+
+ @Test
+ public void isInOrder_withEqualAdjacentElementsIsTrue() {
+ List<Integer> list = Arrays.asList(1, 2, 2, 3, 3, 3, 4);
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertTrue("List with equal adjacent elements should be in
non-decreasing order", result);
+ }
+
+ @Test
+ public void isInOrder_notSortedAscendingIsFalse() {
+ List<Integer> list = Arrays.asList(1, 3, 2, 4, 5);
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertFalse("List with a decreasing pair should not be in
order", result);
+ }
+
+ @Test
+ public void isInOrder_strictlyDescendingIsFalse() {
+ List<Integer> list = Arrays.asList(5, 4, 3, 2, 1);
+
+ boolean result = IterableUtils.isInOrder(list, INT_COMPARATOR);
+
+ Assert.assertFalse("Strictly descending list should not be in order",
result);
+ }
+
+ @Test
+ public void isInOrder_customComparatorDescendingOrder() {
+ Comparator<Integer> descending = Comparator.reverseOrder();
+ List<Integer> list = Arrays.asList(5, 4, 4, 3, 1);
+
+ boolean result = IterableUtils.isInOrder(list, descending);
+
+ Assert.assertTrue("List should be in order according to descending
comparator", result);
+ }
+
+ @Test
+ public void isInOrder_customComparatorDescendingOrderDetectsViolation() {
+ Comparator<Integer> descending = Comparator.reverseOrder();
+ List<Integer> list = Arrays.asList(5, 3, 4, 1);
+
+ boolean result = IterableUtils.isInOrder(list, descending);
+
+ Assert.assertFalse("List should not be in order according to
descending comparator", result);
+ }
}