You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/21 15:07:50 UTC

[commons-lang] 09/10: Use Stream.

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-lang.git

commit cb0fbd4c50cc87a5602ff43da53a0c85ee16d38f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 21 11:06:28 2022 -0400

    Use Stream.
---
 .../lang3/builder/ReflectionToStringBuilder.java   | 12 ++----
 .../org/apache/commons/lang3/stream/Streams.java   | 45 +++++++++++++++++++---
 2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
index bbc52ca2d..919a6e82c 100644
--- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java
@@ -20,16 +20,16 @@ package org.apache.commons.lang3.builder;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Comparator;
-import java.util.List;
+import java.util.Objects;
 
 import org.apache.commons.lang3.ArraySorter;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ClassUtils;
 import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.stream.Streams;
 
 /**
  * <p>
@@ -132,13 +132,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
      * @return The given array or a new array without null.
      */
     static String[] toNoNullStringArray(final Object[] array) {
-        final List<String> list = new ArrayList<>(array.length);
-        for (final Object e : array) {
-            if (e != null) {
-                list.add(e.toString());
-            }
-        }
-        return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
+        return Streams.nonNull(array).map(Objects::toString).toArray(String[]::new);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/lang3/stream/Streams.java b/src/main/java/org/apache/commons/lang3/stream/Streams.java
index c8455003d..bd5ab630c 100644
--- a/src/main/java/org/apache/commons/lang3/stream/Streams.java
+++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java
@@ -579,10 +579,6 @@ public class Streams {
         return new FailableStream<>(stream);
     }
 
-    private static <E> Stream<E> filter(final Collection<E> collection, final Predicate<? super E> predicate) {
-        return of(collection).filter(predicate);
-    }
-
     /**
      * Streams only instances of the give Class in a collection.
      * <p>
@@ -604,7 +600,7 @@ public class Streams {
 
     @SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast.
     private static <E> Stream<E> instancesOf(final Class<? super E> clazz, final Stream<?> stream) {
-        return (Stream<E>) stream.filter(clazz::isInstance);
+        return (Stream<E>) of(stream).filter(clazz::isInstance);
     }
 
     /**
@@ -616,7 +612,32 @@ public class Streams {
      * @since 3.13.0
      */
     public static <E> Stream<E> nonNull(final Collection<E> collection) {
-        return filter(collection, Objects::nonNull);
+        return of(collection).filter(Objects::nonNull);
+    }
+
+    /**
+     * Streams the non-null elements of an array.
+     *
+     * @param <E> the type of elements in the collection.
+     * @param array the array to stream or null.
+     * @return A non-null stream that filters out null elements.
+     * @since 3.13.0
+     */
+    @SafeVarargs
+    public static <E> Stream<E> nonNull(final E... array) {
+        return nonNull(of(array));
+    }
+
+    /**
+     * Streams the non-null elements of a stream.
+     *
+     * @param <E> the type of elements in the collection.
+     * @param stream the stream to stream or null.
+     * @return A non-null stream that filters out null elements.
+     * @since 3.13.0
+     */
+    public static <E> Stream<E> nonNull(final Stream<E> stream) {
+        return of(stream).filter(Objects::nonNull);
     }
 
     /**
@@ -667,6 +688,18 @@ public class Streams {
         return iterator == null ? Stream.empty() : StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
     }
 
+    /**
+     * Returns the stream or {@link Stream#empty()} if the stream is null.
+     *
+     * @param <E> the type of elements in the collection.
+     * @param stream the stream to stream or null.
+     * @return the stream or {@link Stream#empty()} if the stream is null.
+     * @since 3.13.0
+     */
+    private static <E> Stream<E> of(final Stream<E> stream) {
+        return stream == null ? Stream.empty() : stream;
+    }
+
     /**
      * Null-safe version of {@link Stream#of(Object[])}.
      *