This is an automated email from the ASF dual-hosted git repository.

reschke 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 10416ebceb OAK-11408: Extract IteratorUtils from CollectionUtils 
(#2003)
10416ebceb is described below

commit 10416ebcebda6c3c65698d769c95c06a88276304
Author: Julian Reschke <resc...@apache.org>
AuthorDate: Mon Jan 20 13:08:51 2025 +0100

    OAK-11408: Extract IteratorUtils from CollectionUtils (#2003)
    
    * OAK-11408: Extract IteratorUtils from CollectionUtils
    
    * Update 
oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
    
    * OAK-11408: Extract IteratorUtils from CollectionUtils - address Sonar 
warnings
---
 .../oak/commons/collections/CollectionUtils.java   | 34 ---------------
 .../{StreamUtils.java => IteratorUtils.java}       | 50 +++++++++++-----------
 .../oak/commons/collections/StreamUtils.java       |  2 +-
 .../commons/collections/CollectionUtilsTest.java   | 19 --------
 .../oak/commons/collections/IteratorUtilsTest.java | 47 ++++++++++++++++++++
 5 files changed, 74 insertions(+), 78 deletions(-)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
index d6272a9e41..8b973f488c 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
@@ -19,7 +19,6 @@
 package org.apache.jackrabbit.oak.commons.collections;
 
 import java.util.ArrayDeque;
-import java.util.Iterator;
 import java.util.Objects;
 
 import org.jetbrains.annotations.NotNull;
@@ -52,39 +51,6 @@ public class CollectionUtils {
         return arrayDeque;
     }
 
-    /**
-     * Convert an {@code Iterator} to an {@code Iterable}.
-     * <p>
-     * This method is not thread-safe
-     *
-     * @param iterator
-     *            iterator to convert
-     * @return a single-use iterable for the iterator (representing the 
remaining
-     *         elements in the iterator)
-     * @throws IllegalStateException
-     *             when {@linkplain Iterable#iterator()} is called more than
-     *             once
-     */
-    @NotNull
-    public static <T> Iterable<T> toIterable(@NotNull final Iterator<T> 
iterator) {
-        Objects.requireNonNull(iterator);
-
-        return new Iterable<>() {
-
-            private boolean consumed = false;
-
-            @Override
-            public @NotNull Iterator<T> iterator() {
-                if (consumed) {
-                    throw new IllegalStateException("Iterator already returned 
once");
-                } else {
-                    consumed = true;
-                    return iterator;
-                }
-            }
-        };
-    }
-
     /**
      * Ensure the capacity of a map or set given the expected number of 
elements.
      *
diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
similarity index 52%
copy from 
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
copy to 
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
index df39d1a51e..4f0c7f7d44 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtils.java
@@ -22,41 +22,43 @@ import org.jetbrains.annotations.NotNull;
 
 import java.util.Iterator;
 import java.util.Objects;
-import java.util.stream.Stream;
-import java.util.stream.StreamSupport;
 
 /**
- * Utility methods for {@link Stream} conversions.
+ * Utility methods for {@link Iterator} conversions.
  */
-public class StreamUtils {
+public class IteratorUtils {
 
-    private StreamUtils() {
+    private IteratorUtils() {
         // no instances for you
     }
-
     /**
-     * Generates a (non-parallel) {@linkplain Stream} for the {@linkplain 
Iterable}
-     * @param iterable iterable to convert
-     * @return the stream
-     */
-    @NotNull
-    public static <T> Stream<T> toStream(@NotNull Iterable<T> iterable) {
-        Objects.requireNonNull(iterable);
-        return StreamSupport.stream(iterable.spliterator(), false);
-    }
-
-    /**
-     * Generates a (non-parallel) {@linkplain Stream} for the
-     * {@linkplain Iterable}
+     * Convert an {@code Iterator} to an {@code Iterable}.
      * <p>
      * This method is not thread-safe
      *
-     * @param iterator
-     *            iterator to convert
-     * @return the stream (representing the remaining elements in the iterator)
+     * @param iterator iterator to convert
+     * @return a single-use iterable for the iterator (representing the 
remaining
+     * elements in the iterator)
+     * @throws IllegalStateException when {@linkplain Iterable#iterator()} is 
called more than
+     *                               once
      */
     @NotNull
-    public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
-        return 
StreamSupport.stream(CollectionUtils.toIterable(iterator).spliterator(), false);
+    public static <T> Iterable<T> toIterable(@NotNull final Iterator<T> 
iterator) {
+        Objects.requireNonNull(iterator);
+
+        return new Iterable<>() {
+
+            private boolean consumed = false;
+
+            @Override
+            public @NotNull Iterator<T> iterator() {
+                if (consumed) {
+                    throw new IllegalStateException("Iterator already returned 
once");
+                } else {
+                    consumed = true;
+                    return iterator;
+                }
+            }
+        };
     }
 }
diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
index df39d1a51e..72db75a10e 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/StreamUtils.java
@@ -57,6 +57,6 @@ public class StreamUtils {
      */
     @NotNull
     public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
-        return 
StreamSupport.stream(CollectionUtils.toIterable(iterator).spliterator(), false);
+        return 
StreamSupport.stream(IteratorUtils.toIterable(iterator).spliterator(), false);
     }
 }
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
index 263d1bc750..958cdfced8 100644
--- 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
@@ -24,7 +24,6 @@ import org.junit.Test;
 import java.util.ArrayDeque;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 
 import static org.junit.Assert.fail;
@@ -50,24 +49,6 @@ public class CollectionUtilsTest {
         Assert.assertNotNull(result);
         Assert.assertTrue(result.isEmpty());
     }
-
-    @Test
-    public void iteratorToIIteratable() {
-        Iterator<String> iterator = List.of("a", "b", "c").iterator();
-        iterator.next();
-        Iterable<String> iterable = CollectionUtils.toIterable(iterator);
-        Iterator<String> testit = iterable.iterator();
-        Assert.assertEquals("b", testit.next());
-        Assert.assertEquals("c", testit.next());
-        Assert.assertFalse(testit.hasNext());
-        try {
-            testit = iterable.iterator();
-            fail("should only work once");
-        } catch (IllegalStateException expected) {
-            // that's what we want
-        }
-    }
-
     @Test
     public void ensureCapacity() {
         int capacity = CollectionUtils.ensureCapacity(8);
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
new file mode 100644
index 0000000000..1b943cd1c0
--- /dev/null
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/IteratorUtilsTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.commons.collections;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.List;
+
+import static org.junit.Assert.fail;
+
+public class IteratorUtilsTest {
+
+    @Test
+    public void iteratorToIIterable() {
+        Iterator<String> iterator = List.of("a", "b", "c").iterator();
+        iterator.next();
+        Iterable<String> iterable = IteratorUtils.toIterable(iterator);
+        Iterator<String> testit = iterable.iterator();
+        Assert.assertEquals("b", testit.next());
+        Assert.assertEquals("c", testit.next());
+        Assert.assertFalse(testit.hasNext());
+        try {
+            iterable.iterator();
+            fail("should only work once");
+        } catch (IllegalStateException expected) {
+            // that's what we want
+        }
+    }
+}

Reply via email to