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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit 80429d43032fa642a3802bb786a0272d38cb7ac4
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Oct 5 16:52:22 2024 -0400

    Internal refactoring
---
 .../apache/commons/collections4/IteratorUtils.java  | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/collections4/IteratorUtils.java 
b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
index 10e244544..89b6ed69d 100644
--- a/src/main/java/org/apache/commons/collections4/IteratorUtils.java
+++ b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
@@ -659,6 +659,7 @@ public class IteratorUtils {
      * Finds the first element in the given iterator which matches the given 
predicate.
      * <p>
      * A {@code null} or empty iterator returns null.
+     * </p>
      *
      * @param <E> the element type
      * @param iterator  the iterator to search, may be null
@@ -668,8 +669,24 @@ public class IteratorUtils {
      * @since 4.1
      */
     public static <E> E find(final Iterator<E> iterator, final Predicate<? 
super E> predicate) {
-        Objects.requireNonNull(predicate, "predicate");
+        return find(iterator, predicate, null);
+    }
 
+    /**
+     * Finds the first element in the given iterator which matches the given 
predicate.
+     * <p>
+     * A {@code null} or empty iterator returns {@code defaultValue}.
+     * </p>
+     *
+     * @param <E>          the element type.
+     * @param iterator     the iterator to search, may be null.
+     * @param predicate    the predicate to use, must not be null.
+     * @param defaultValue the default value, may be null.
+     * @return the first element of the iterator which matches the predicate 
or null if none could be found.
+     * @throws NullPointerException if predicate is null.
+     */
+    private static <E> E find(final Iterator<E> iterator, final Predicate<? 
super E> predicate, final E defaultValue) {
+        Objects.requireNonNull(predicate, "predicate");
         if (iterator != null) {
             while (iterator.hasNext()) {
                 final E element = iterator.next();
@@ -678,7 +695,7 @@ public class IteratorUtils {
                 }
             }
         }
-        return null;
+        return defaultValue;
     }
 
     /**

Reply via email to