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 2021/12/03 18:47:52 UTC
[commons-lang] branch master updated: Add Streams.instancesOf(Class, Collection).
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
The following commit(s) were added to refs/heads/master by this push:
new e6ae756 Add Streams.instancesOf(Class, Collection).
e6ae756 is described below
commit e6ae7561a05b1356625563cd868dce8a0fa4ddae
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Dec 3 13:47:48 2021 -0500
Add Streams.instancesOf(Class, Collection).
---
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/stream/Streams.java | 112 ++++++++++++---------
.../apache/commons/lang3/stream/StreamsTest.java | 10 ++
3 files changed, 77 insertions(+), 46 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a975fdf..de2be06 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -104,6 +104,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(Stream) and deprecate misnamed stream(Stream).</action>
<action type="add" dev="ggregory" due-to="Maxwell Cody, Gary Gregory">Add EnumUtils.getEnumMap(Class, Function). #730</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FluentBitSet.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.instancesOf(Class, Collection).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.5.0.0 #735, #808, #822, #834.</action>
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump actions/cache from v2.1.4 to v2.1.7 #742, #752, #764, #833.</action>
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 805e994..f38f092 100644
--- a/src/main/java/org/apache/commons/lang3/stream/Streams.java
+++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java
@@ -444,35 +444,6 @@ public class Streams {
}
}
- private static <E> Stream<E> filter(final Collection<E> collection, final Predicate<? super E> predicate) {
- return toStream(collection).filter(predicate);
- }
-
- /**
- * Streams non-null elements of a collection.
- *
- * @param <E> the type of elements in the collection.
- * @param collection the collection to stream or null.
- * @return A non-null stream that filters out null elements.
- * @since 3.13.0
- */
- public static <E> Stream<E> nullSafeStream(final Collection<E> collection) {
- return filter(collection, Objects::nonNull);
- }
-
- /**
- * Null-safe version of {@link Stream#of(Object[])}.
- *
- * @param <T> the type of stream elements.
- * @param values the elements of the new stream, may be {@code null}.
- * @return the new stream on {@code values} or {@link Stream#empty()}.
- * @since 3.13.0
- */
- @SafeVarargs // Creating a stream from an array is safe
- public static <T> Stream<T> of(final T... values) {
- return values == null ? Stream.empty() : Stream.of(values);
- }
-
/**
* Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced version
* of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
@@ -508,19 +479,18 @@ public class Streams {
* intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
* better than the first version.
*
- * @param <E> The streams element type.
- * @param collection The stream, which is being converted.
+ * @param <T> The streams element type.
+ * @param stream The stream, which is being converted.
* @return The {@link FailableStream}, which has been created by converting the stream.
- * @deprecated Use {@link #failableStream(Collection)}.
+ * @since 3.13.0
*/
- @Deprecated
- public static <E> FailableStream<E> stream(final Collection<E> collection) {
- return failableStream(collection);
+ public static <T> FailableStream<T> failableStream(final Collection<T> stream) {
+ return failableStream(toStream(stream));
}
/**
- * Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced version
- * of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
+ * Converts the given {@link Stream stream} into a {@link FailableStream}. This is basically a simplified, reduced
+ * version of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
* {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
* {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
*
@@ -558,13 +528,62 @@ public class Streams {
* @return The {@link FailableStream}, which has been created by converting the stream.
* @since 3.13.0
*/
- public static <T> FailableStream<T> failableStream(final Collection<T> stream) {
- return failableStream(toStream(stream));
+ public static <T> FailableStream<T> failableStream(final Stream<T> stream) {
+ return new FailableStream<>(stream);
+ }
+
+ private static <E> Stream<E> filter(final Collection<E> collection, final Predicate<? super E> predicate) {
+ return toStream(collection).filter(predicate);
}
/**
- * Converts the given {@link Stream stream} into a {@link FailableStream}. This is basically a simplified, reduced
- * version of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
+ * Streams only instances of the give Class in a collection.
+ * <p>
+ * This method shorthand for:
+ * </p>
+ * <pre>
+ * {@code (Stream<E>) Streams.toStream(collection).filter(collection, SomeClass.class::isInstance);}
+ * </pre>
+ *
+ * @param <E> the type of elements in the collection we want to stream.
+ * @param clazz the type of elements in the collection we want to stream.
+ * @param collection the collection to stream or null.
+ * @return A non-null stream that only provides instances we want.
+ * @since 3.13.0
+ */
+ @SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast.
+ public static <E> Stream<E> instancesOf(Class<? super E> clazz, Collection<? super E> collection) {
+ return (Stream<E>) filter(collection, clazz::isInstance);
+ }
+
+ /**
+ * Streams non-null elements of a collection.
+ *
+ * @param <E> the type of elements in the collection.
+ * @param collection the collection to stream or null.
+ * @return A non-null stream that filters out null elements.
+ * @since 3.13.0
+ */
+ public static <E> Stream<E> nullSafeStream(final Collection<E> collection) {
+ return filter(collection, Objects::nonNull);
+ }
+
+ /**
+ * Null-safe version of {@link Stream#of(Object[])}.
+ *
+ * @param <T> the type of stream elements.
+ * @param values the elements of the new stream, may be {@code null}.
+ * @return the new stream on {@code values} or {@link Stream#empty()}.
+ * @since 3.13.0
+ */
+ @SafeVarargs // Creating a stream from an array is safe
+ public static <T> Stream<T> of(final T... values) {
+ return values == null ? Stream.empty() : Stream.of(values);
+ }
+
+ /**
+ * Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced version
+ * of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
* {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
* {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
*
@@ -597,13 +616,14 @@ public class Streams {
* intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
* better than the first version.
*
- * @param <T> The streams element type.
- * @param stream The stream, which is being converted.
+ * @param <E> The streams element type.
+ * @param collection The stream, which is being converted.
* @return The {@link FailableStream}, which has been created by converting the stream.
- * @since 3.13.0
+ * @deprecated Use {@link #failableStream(Collection)}.
*/
- public static <T> FailableStream<T> failableStream(final Stream<T> stream) {
- return new FailableStream<>(stream);
+ @Deprecated
+ public static <E> FailableStream<E> stream(final Collection<E> collection) {
+ return failableStream(collection);
}
/**
diff --git a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
index 490799d..2a7d0b1 100644
--- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
+++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
@@ -138,6 +138,16 @@ public class StreamsTest {
}
@Test
+ public void testInstanceOfStream() {
+ assertEquals(2, Streams.instancesOf(String.class, Arrays.asList("A", "B")).collect(Collectors.toList()).size());
+ assertEquals(2, Streams.instancesOf(String.class, Arrays.asList(null, "A", null, "B", null)).collect(Collectors.toList()).size());
+ assertEquals(0, Streams.instancesOf(String.class, Arrays.asList(null, null)).collect(Collectors.toList()).size());
+ //
+ List<Object> objects = Arrays.asList("A", "B");
+ assertEquals(2, Streams.instancesOf(String.class, objects).collect(Collectors.toList()).size());
+ }
+
+ @Test
public void testNullSafeStreamNull() {
final List<String> input = null;
assertEquals(0, Streams.nullSafeStream(input).collect(Collectors.toList()).size());