All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.basking2.sdsai.itrex.iterators.NullSkippingIterator Maven / Gradle / Ivy

There is a newer version: 1.1.23
Show newest version
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