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 2025/04/14 13:04:43 UTC
(jackrabbit-oak) branch OAK-11585 created (now a637a4eaa9)
This is an automated email from the ASF dual-hosted git repository.
daim pushed a change to branch OAK-11585
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at a637a4eaa9 OAK-11585 : added Iterators.contains replacement in oak-commons
This branch includes the following new commits:
new a637a4eaa9 OAK-11585 : added Iterators.contains replacement in oak-commons
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-11585 : added Iterators.contains replacement in oak-commons
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
daim pushed a commit to branch OAK-11585
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit a637a4eaa9b847ce453740190a5f89add7de7cef
Author: Rishabh Kumar <di...@adobe.com>
AuthorDate: Mon Apr 14 18:32:43 2025 +0530
OAK-11585 : added Iterators.contains replacement in oak-commons
---
.../oak/commons/collections/IteratorUtils.java | 20 ++++++++++
.../oak/commons/collections/package-info.java | 2 +-
.../oak/commons/collections/IteratorUtilsTest.java | 44 ++++++++++++++++++++++
3 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
index 660a0c165d..9701d92632 100644
--- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
+++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
@@ -230,4 +230,24 @@ public class IteratorUtils {
}
}
}
+
+ /**
+ * Checks if the given iterator contains the specified element.
+ * <p>
+ * This method iterates through the iterator, checking each element for equality with
+ * the specified object using {@link Objects#equals(Object, Object)}. The iteration stops
+ * once a match is found or the iterator is exhausted.
+ * <p>
+ * Note that this method will consume the iterator.
+ *
+ * @param <?> the type of objects in the iterator
+ * @param iterator the iterator to check, must not be null
+ * @param element the element to find, may be null
+ * @return {@code true} if the iterator contains the element, {@code false} otherwise
+ * @throws NullPointerException if the iterator is null
+ */
+ public static boolean contains(Iterator<?> iterator, Object element) {
+ Objects.requireNonNull(iterator, "Iterator must not be null");
+ return org.apache.commons.collections4.IteratorUtils.contains(iterator, element);
+ }
}
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 fa16acc93d..81c20db5ca 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.1.0")
+@Version("2.2.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/IteratorUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
index df21b5d115..6883df39b1 100644
--- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
+++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
@@ -414,4 +414,48 @@ public class IteratorUtilsTest {
Assert.assertEquals(obj2, IteratorUtils.getLast(objectIterator));
Assert.assertFalse(objectIterator.hasNext());
}
+
+ @Test
+ public void testContainsWithElementPresent() {
+ Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
+ Assert.assertTrue(IteratorUtils.contains(iterator, "b"));
+ // Iterator shouldn't be consumed
+ Assert.assertTrue(iterator.hasNext());
+ }
+
+ @Test
+ public void testContainsWithElementNotPresent() {
+ Iterator<String> iterator = Arrays.asList("a", "b", "c").iterator();
+ Assert.assertFalse(IteratorUtils.contains(iterator, "z"));
+ // Iterator should be consumed
+ Assert.assertFalse(iterator.hasNext());
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testContainsWithNullIterator() {
+ IteratorUtils.contains(null, "test");
+ }
+
+ @Test
+ public void testContainsWithEmptyIterator() {
+ Iterator<String> emptyIterator = Collections.emptyIterator();
+ Assert.assertFalse(IteratorUtils.contains(emptyIterator, "anything"));
+ }
+
+ @Test
+ public void testContainsWithNullElement() {
+ Iterator<String> iterator = Arrays.asList("a", null, "c").iterator();
+ Assert.assertTrue(IteratorUtils.contains(iterator, null));
+ // Iterator should be stopped after finding null
+ Assert.assertTrue(iterator.hasNext());
+ }
+
+ @Test
+ public void testContainsWithMultipleOccurrences() {
+ Iterator<String> iterator = Arrays.asList("a", "b", "b", "c").iterator();
+ Assert.assertTrue(IteratorUtils.contains(iterator, "b"));
+ // Iterator should stop at first occurrence
+ Assert.assertTrue(iterator.hasNext());
+ Assert.assertEquals("b", iterator.next());
+ }
}