I would like to convert the following for
statement to a Java 8 stream (i.e. Stream
). The ideal solution would be simple eno
Here's the same code as presented in the question but implemented using a Spliterator
. I am hoping someone can come up with a simpler, smarter answer.
Class> clazz;
Object value;
value = ...;
>walkLinks(value.getClass(), Class::getSuperclass).
...
public static Stream walkLinks(T start, Function next)
{
WalkLinks walker;
Stream result;
walker = new WalkLinks<>(start, next);
result = StreamSupport.stream(walker, false);
return(result);
}
private static class WalkLinks extends Spliterators.AbstractSpliterator
{
private final Function m_next;
private T m_value;
public WalkLinks(T value, Function next)
{
super(Long.MAX_VALUE, Spliterator.ORDERED + Spliterator.NONNULL);
m_value = value;
m_next = next;
}
@Override
public boolean tryAdvance(Consumer super T> action)
{
if (m_value == null)
return(false);
action.accept(m_value);
m_value = m_next.apply(m_value);
return(true);
}
}