code samples for issue BAEL-4877
This commit is contained in:
parent
00482852bd
commit
b37e11f2cb
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.streams.conversion;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Spliterators.AbstractSpliterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class EnumerationSpliterator<T> extends AbstractSpliterator<T> {
|
||||||
|
|
||||||
|
private final Enumeration<T> enumeration;
|
||||||
|
|
||||||
|
public EnumerationSpliterator(long est, int additionalCharacteristics, Enumeration<T> enumeration) {
|
||||||
|
super(est, additionalCharacteristics);
|
||||||
|
this.enumeration = enumeration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(Consumer<? super T> action) {
|
||||||
|
if (enumeration.hasMoreElements()) {
|
||||||
|
action.accept(enumeration.nextElement());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachRemaining(Consumer<? super T> action) {
|
||||||
|
while (enumeration.hasMoreElements())
|
||||||
|
action.accept(enumeration.nextElement());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.streams.conversion;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Spliterator;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
public class EnumerationStreamConversion {
|
||||||
|
|
||||||
|
public static <T> Stream<T> convert(Enumeration<T> enumeration) {
|
||||||
|
EnumerationSpliterator<T> spliterator = new EnumerationSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration);
|
||||||
|
Stream<T> stream = StreamSupport.stream(spliterator, false);
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.streams.conversion;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class EnumerationStreamConversionUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEnumeration_whenConvertedToStream_thenNotNull() {
|
||||||
|
Vector<Integer> input = new Vector<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||||
|
|
||||||
|
Stream<Integer> resultingStream = EnumerationStreamConversion.convert(input.elements());
|
||||||
|
|
||||||
|
Assert.assertNotNull(resultingStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenConvertedToList_thenCorrect() {
|
||||||
|
Vector<Integer> input = new Vector<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||||
|
|
||||||
|
Stream<Integer> stream = EnumerationStreamConversion.convert(input.elements());
|
||||||
|
List<Integer> list = stream.filter(e -> e >= 3)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertThat(list, contains(3, 4, 5));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user