Use Stream.

This commit is contained in:
Gary Gregory 2022-08-21 11:06:28 -04:00
parent 1297d7c365
commit cb0fbd4c50
2 changed files with 42 additions and 15 deletions

View File

@ -20,16 +20,16 @@ package org.apache.commons.lang3.builder;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.Objects;
import org.apache.commons.lang3.ArraySorter; import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.stream.Streams;
/** /**
* <p> * <p>
@ -132,13 +132,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* @return The given array or a new array without null. * @return The given array or a new array without null.
*/ */
static String[] toNoNullStringArray(final Object[] array) { static String[] toNoNullStringArray(final Object[] array) {
final List<String> list = new ArrayList<>(array.length); return Streams.nonNull(array).map(Objects::toString).toArray(String[]::new);
for (final Object e : array) {
if (e != null) {
list.add(e.toString());
}
}
return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
} }
/** /**

View File

@ -579,10 +579,6 @@ public class Streams {
return new FailableStream<>(stream); 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. * Streams only instances of the give Class in a collection.
* <p> * <p>
@ -604,7 +600,7 @@ public class Streams {
@SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast. @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) { 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 * @since 3.13.0
*/ */
public static <E> Stream<E> nonNull(final Collection<E> collection) { 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); 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[])}. * Null-safe version of {@link Stream#of(Object[])}.
* *