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 8b920a326f OAK-11585 : added Iterators.contains replacement in
oak-commons (#2228)
8b920a326f is described below
commit 8b920a326f9530cb2814bb5662896a0cd9a0a963
Author: Rishabh Kumar <[email protected]>
AuthorDate: Tue Apr 15 08:53:30 2025 +0530
OAK-11585 : added Iterators.contains replacement in oak-commons (#2228)
Co-authored-by: Rishabh Kumar <[email protected]>
---
.../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());
+ }
}