aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2022-07-16 09:27:15 -0400
committerGary Gregory <garydgregory@gmail.com>2022-07-16 09:27:15 -0400
commit0bee92719352c658886763618bf4eacc7dc2b7ec (patch)
treea1723ec2c69918c9229051ed13c574a1981d6893
parentfc7d83bf84a57d50ea924887a8b348f6029faa87 (diff)
downloadapache-commons-lang-0bee92719352c658886763618bf4eacc7dc2b7ec.tar.gz
Add Streams.of(Enumeration<E>)
-rw-r--r--src/changes/changes.xml1
-rw-r--r--src/main/java/org/apache/commons/lang3/stream/Streams.java57
-rw-r--r--src/test/java/org/apache/commons/lang3/stream/StreamsTest.java16
3 files changed, 74 insertions, 0 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 454d5690a..e5e32ada0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -157,6 +157,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_18.</action>
<action issue="LANG-1627" type="add" dev="ggregory" due-to="Alberto Scotto, Avijit Chakraborty, Steve Bosman, Bruno P. Kinoshita, Gary Gregory">Add ArrayUtils.oneHot().</action>
<action issue="LANG-1662" type="add" dev="ggregory" due-to="Daniel Augusto Veronezi Salvador, Gary Gregory, Bruno P. Kinoshita">Let ReflectionToStringBuilder only reflect given field names #849.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Enumeration&lt;E&gt;).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.5 #742, #752, #764, #833, #867.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</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 e44ed7bf0..720405129 100644
--- a/src/main/java/org/apache/commons/lang3/stream/Streams.java
+++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java
@@ -19,9 +19,12 @@ package org.apache.commons.lang3.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.Set;
+import java.util.Spliterator;
+import java.util.Spliterators.AbstractSpliterator;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
@@ -31,6 +34,7 @@ import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.function.Failable;
@@ -123,6 +127,47 @@ public class Streams {
}
/**
+ * Helps implement {@link Streams#of(Enumeration)}.
+ *
+ * @param <T> The element type.
+ */
+ private static class EnumerationSpliterator<T> extends AbstractSpliterator<T> {
+
+ private final Enumeration<T> enumeration;
+
+ /**
+ * Creates a spliterator reporting the given estimated size and additionalCharacteristics.
+ *
+ * @param estimatedSize the estimated size of this spliterator if known, otherwise {@code Long.MAX_VALUE}.
+ * @param additionalCharacteristics properties of this spliterator's source or elements. If {@code SIZED} is reported then this spliterator will
+ * additionally report {@code SUBSIZED}.
+ * @param enumeration The Enumeration to wrap.
+ */
+ protected EnumerationSpliterator(final long estimatedSize, final int additionalCharacteristics, final Enumeration<T> enumeration) {
+ super(estimatedSize, additionalCharacteristics);
+ this.enumeration = Objects.requireNonNull(enumeration, "enumeration");
+ }
+
+ @Override
+ public void forEachRemaining(final Consumer<? super T> action) {
+ while (enumeration.hasMoreElements()) {
+ next(action);
+ }
+ }
+
+ private boolean next(final Consumer<? super T> action) {
+ action.accept(enumeration.nextElement());
+ return true;
+
+ }
+
+ @Override
+ public boolean tryAdvance(final Consumer<? super T> action) {
+ return enumeration.hasMoreElements() ? next(action) : false;
+ }
+ }
+
+ /**
* A reduced, and simplified version of a {@link Stream} with failable method signatures.
*
* @param <T> The streams element type.
@@ -573,6 +618,18 @@ public class Streams {
}
/**
+ * Streams the elements of the given enumeration in order.
+ *
+ * @param <E> The enumeration element type.
+ * @param enumeration The enumeration to stream.
+ * @return a new stream.
+ * @since 3.13.0
+ */
+ public static <E> Stream<E> of(final Enumeration<E> enumeration) {
+ return StreamSupport.stream(new EnumerationSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration), false);
+ }
+
+ /**
* Null-safe version of {@link Stream#of(Object[])}.
*
* @param <T> the type of stream elements.
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 625bff4cb..99b34204b 100644
--- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
+++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
@@ -24,11 +24,13 @@ import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -162,6 +164,20 @@ public class StreamsTest extends AbstractLangTest {
}
@Test
+ public void testOfEnumeration() {
+ final Hashtable<String, Integer> table = new Hashtable<>();
+ assertEquals(0, Streams.of(table.elements()).count());
+ table.put("One", 1);
+ assertEquals(1, Streams.of(table.elements()).count());
+ table.put("Two", 2);
+ assertEquals(2, Streams.of(table.elements()).count());
+ final List<String> collect = Streams.of(table.keys()).collect(Collectors.toList());
+ assertTrue(collect.contains("One"));
+ assertTrue(collect.contains("Two"));
+ assertEquals(2, collect.size());
+ }
+
+ @Test
public void testSimpleStreamFilter() {
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
final List<Integer> output = Failable.stream(input).map(Integer::valueOf).filter(i -> (i.intValue() % 2 == 0)).collect(Collectors.toList());