Add Streams.of(Enumeration<E>)
This commit is contained in:
parent
fc7d83bf84
commit
0bee927193
|
@ -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<E>).</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>
|
||||
|
|
|
@ -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;
|
||||
|
@ -122,6 +126,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.
|
||||
*
|
||||
|
@ -572,6 +617,18 @@ public class Streams {
|
|||
return filter(collection, Objects::nonNull);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[])}.
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
|
@ -161,6 +163,20 @@ public class StreamsTest extends AbstractLangTest {
|
|||
assertEquals(2, Streams.of("foo", "bar").count());
|
||||
}
|
||||
|
||||
@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");
|
||||
|
|
Loading…
Reference in New Issue