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

org.sfm.utils.ForEachIteratorIterator Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.utils;

import java.util.Iterator;
import java.util.NoSuchElementException;


public class ForEachIteratorIterator implements Iterator {

    private final ForEachIterator iterator;

    private boolean isFetched;
    private boolean hasValue;
    private T currentValue;

    public ForEachIteratorIterator(ForEachIterator iterator) {
        this.iterator = iterator;
    }


    @Override
    public boolean hasNext() {
        fetch();
        return hasValue;
    }

    private void fetch() {
        if (!isFetched) {
            try {
                doFetch();
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
        }
    }

    private void doFetch() throws Exception {

        if (!iterator.next(new RowHandler() {
            @Override
            public void handle(T t) throws Exception {
                currentValue = t;
                hasValue = true;
            }
        }))  {
            currentValue = null;
            hasValue = false;
        }
        isFetched = true;
    }

    @Override
    public T next() {
        fetch();
        if (hasValue) {
            T v = currentValue;
            currentValue = null;
            isFetched = false;
            return v;
        } else {
            throw new NoSuchElementException("No more rows");
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy