package org.eclipse.collections.impl.lazy.iterator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.block.predicate.Predicate;
public final class TakeWhileIterator<T> implements Iterator<T>
{
private static final Object NULL = new Object();
private final Iterator<T> iterator;
private final Predicate<? super T> predicate;
private boolean terminate;
private Object next = NULL;
public TakeWhileIterator(Iterable<T> iterable, Predicate<? super T> predicate)
{
this(iterable.iterator(), predicate);
}
public TakeWhileIterator(Iterator<T> iterator, Predicate<? super T> predicate)
{
this.iterator = iterator;
this.predicate = predicate;
}
@Override
public boolean hasNext()
{
if (this.next != NULL)
{
return true;
}
if (!this.terminate && this.iterator.hasNext())
{
T temp = this.iterator.next();
if (this.predicate.accept(temp))
{
this.next = temp;
return true;
}
this.terminate = true;
this.next = NULL;
}
return false;
}
@Override
public T next()
{
if (this.hasNext())
{
Object temp = this.next;
this.next = NULL;
return (T) temp;
}
throw new NoSuchElementException();
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Cannot remove from a takeWhile iterator");
}
}