com.github.basking2.sdsai.itrex.iterators.NullSkippingIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdsai-itrex Show documentation
Show all versions of sdsai-itrex Show documentation
An S-Expression inspiried library focused on iterators.
package com.github.basking2.sdsai.itrex.iterators;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* An iterator that pre-fetches the next value from the iterator it encloses so as to skip nulls.
*/
public class NullSkippingIterator implements Iterator {
final private Iterator iterator;
private T nextT;
public NullSkippingIterator(final Iterator iterator)
{
this.iterator = iterator;
prefetch();
}
private void prefetch() {
nextT = null;
while (iterator.hasNext() && nextT == null) {
nextT = iterator.next();
}
}
@Override
public boolean hasNext() {
return nextT != null;
}
@Override
public T next() {
if (nextT == null) {
throw new NoSuchElementException();
}
final T t = nextT;
prefetch();
return t;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy