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 2026/01/29 11:17:44 UTC
(jackrabbit-oak) branch OAK-12081 created (now 8c30579c21)
This is an automated email from the ASF dual-hosted git repository.
daim pushed a change to branch OAK-12081
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at 8c30579c21 OAK-12081 : added isInOrder() in IterableUtils to check whether all element of given iterable are in order as specified by given comparator
This branch includes the following new commits:
new 8c30579c21 OAK-12081 : added isInOrder() in IterableUtils to check whether all element of given iterable are in order as specified by given comparator
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(jackrabbit-oak) 01/01: OAK-12081 : added isInOrder() in IterableUtils to check whether all element of given iterable are in order as specified by given comparator
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch OAK-12081
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 8c30579c21ce7969aadb4549c9bde148f8e55fb3
Author: rishabhdaim <ri...@gmail.com>
AuthorDate: Thu Jan 29 16:47:25 2026 +0530
OAK-12081 : added isInOrder() in IterableUtils to check whether all element of given iterable are in order as specified by given comparator
---
.../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);
+ }
}