Stream<T>
is based on internal iteration approach provided by Spliterator<T>
which in turn delegates the iteration on boolean tryAdvance(Consumer<? super T> action)
implementation. Put it simple:
Stream<T>
---->
Spliterator<T>
---->
boolean tryAdvance(Consumer<T>)
So, I would like to have some sort of utility that allows to create a Stream<T>
from a Function<Consumer<T>, Boolean>
. Note that Function<Consumer<T>, Boolean>
has the same descriptor of boolean tryAdvance(Consumer<T>)
, i.e. Consumer<T> -> Boolean
.
I am looking for an auxiliary function such as (updated according to @shmosel's comment):
static <T> Stream<T> stream(Predicate<Consumer<? super T>> moveNext) {
Spliterator<T> iter = new AbstractSpliterator<T>(Long.MAX_VALUE, 0) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
return moveNext.test(action);
}
};
return StreamSupport.stream(iter, false);
}
This is the most concise way that I found to achieve that implementation. Yet, I still do not like the use of the anonymous inner class. Is there any simplest alternative?