code samples for issue BAEL-4877
This commit is contained in:
+30
@@ -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());
|
||||
}
|
||||
}
|
||||
+16
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user