I would like to convert the following for
statement to a Java 8 stream (i.e. Stream
). The ideal solution would be simple eno
You need a takeWhile method which will appear in JDK-9 only. Currently you can use a back-port which is available, for example, in my StreamEx library:
Stream<Class<?>> stream = StreamEx.iterate(value.getClass(), Class::getSuperClass)
.takeWhile(Objects::nonNull);
In JDK-9 just replace StreamEx
with Stream
.
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 = ...;
<Class<?>>walkLinks(value.getClass(), Class::getSuperclass).
...
public static <T> Stream<T> walkLinks(T start, Function<T, T> next)
{
WalkLinks<T> walker;
Stream<T> result;
walker = new WalkLinks<>(start, next);
result = StreamSupport.stream(walker, false);
return(result);
}
private static class WalkLinks<T> extends Spliterators.AbstractSpliterator<T>
{
private final Function<T, T> m_next;
private T m_value;
public WalkLinks(T value, Function<T, T> 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);
}
}