org.perfectable.introspection.query.Streams Maven / Gradle / Ivy
package org.perfectable.introspection.query;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Enumeration;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
final class Streams {
static Stream generateSingle(E initial,
Function super E, ? extends Stream extends E>> mutator) {
return generate(Stream.of(initial), mutator, element -> true);
}
static Stream generateSingleConditional(E initial,
Function super E, ? extends Stream extends E>> mutator,
Predicate super E> condition) {
return generate(Stream.of(initial), mutator, condition);
}
private static Stream generate(Stream initial,
Function super E, ? extends Stream extends E>> mutator,
Predicate super E> condition) {
Spliterator wrappedSpliterator =
GeneratorSpliterator.wrap(initial.spliterator(), mutator, condition);
return StreamSupport.stream(wrappedSpliterator, /* parallel= */false);
}
public static Stream from(Enumeration enumeration) {
Spliterator spliterator = EnumerationSpliterator.create(enumeration);
return StreamSupport.stream(spliterator, /* parallel= */false);
}
private static final class GeneratorSpliterator extends Spliterators.AbstractSpliterator {
private static final int ADDITIONAL_CHARACTERISTICS = 0;
private final Spliterator extends T> wrapped;
private final Function super T, ? extends Stream extends T>> mutator;
private final Predicate super T> condition;
private final Deque buffer = new ArrayDeque<>();
static GeneratorSpliterator wrap(Spliterator extends T> wrapped,
Function super T, ? extends Stream extends T>> mutator,
Predicate super T> condition) {
return new GeneratorSpliterator<>(wrapped, mutator, condition);
}
private GeneratorSpliterator(Spliterator extends T> wrapped,
Function super T, ? extends Stream extends T>> mutator,
Predicate super T> condition) {
super(Long.MAX_VALUE, ADDITIONAL_CHARACTERISTICS);
this.wrapped = wrapped;
this.mutator = mutator;
this.condition = condition;
}
@Override
public boolean tryAdvance(Consumer super T> consumer) {
Consumer super T> wrappedAction = element -> {
consumer.accept(element);
mutator.apply(element).filter(condition).forEach(buffer::add);
};
if (buffer.isEmpty()) {
return wrapped.tryAdvance(wrappedAction);
}
else {
T generated = buffer.pop();
wrappedAction.accept(generated);
return true;
}
}
}
private static final class EnumerationSpliterator extends Spliterators.AbstractSpliterator {
private static final int ADDITIONAL_CHARACTERISTICS = 0;
private final Enumeration enumeration;
private EnumerationSpliterator(Enumeration enumeration) {
super(Long.MAX_VALUE, ADDITIONAL_CHARACTERISTICS);
this.enumeration = enumeration;
}
public static Spliterator create(Enumeration enumeration) {
return new EnumerationSpliterator(enumeration);
}
@Override
public boolean tryAdvance(Consumer super E> consumer) {
if (!enumeration.hasMoreElements()) {
return false;
}
E nextElement = enumeration.nextElement();
consumer.accept(nextElement);
return true;
}
}
private Streams() {
// utility
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy