/**
* Returns an Iterator containing all elements starting from the head of the source up to and excluding the first
* element that violates the predicate. The resulting Iterator is a lazily computed view, so any modifications to the
* underlying Iterators will be reflected on iteration. The result does not support {@link Iterator#remove()}
*
* @param iterator
* the elements from which to take. May not be <code>null</code>.
* @param predicate
* the predicate which decides whether to keep taking elements. May not be <code>null</code>.
* @return the taken elements
* @since 2.7
*/
public static <T> Iterator<T> takeWhile(final Iterator<? extends T> iterator, final Function1<? super T, Boolean> predicate) {
if (iterator == null)
throw new NullPointerException("iterator");
if (predicate == null)
throw new NullPointerException("predicate");
return new AbstractIterator<T>() {
@Override
protected T computeNext() {
if (!iterator.hasNext())
return endOfData();
T next = iterator.next();
if (predicate.apply(next)) {
return next;
} else {
return endOfData();
}
}
};
}
IteratorExtensions.java 文件源码
java
阅读 36
收藏 0
点赞 0
评论 0
项目:xtext-lib
作者:
评论列表
文章目录