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

org.sfm.csv.parser.CsvStringArrayIterator 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.csv.parser;

import org.sfm.csv.CsvReader;
import org.sfm.utils.ErrorHelper;
import org.sfm.utils.RowHandler;

import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class CsvStringArrayIterator implements Iterator {

    private final CsvReader reader;
    private final CellConsumer cellConsumer;

    private boolean isFetched;
    private String[] value;

    @SuppressWarnings("unchecked")
    public CsvStringArrayIterator(CsvReader csvReader) {
        cellConsumer = new StringArrayConsumer(new RowHandler() {
            @Override
            public void handle(String[] strings) throws Exception {
                value = strings;
            }
        });
        reader = csvReader;
    }

    @Override
    public boolean hasNext() {
        fetch();
        return value != null;
    }

    private void fetch() {
        if (!isFetched) {
            try {
                value = null;
                reader.parseRow(cellConsumer);
            } catch (IOException e) {
                ErrorHelper.rethrow(e);
            }
            isFetched = true;
        }
    }

    @Override
    public String[] next() {
        fetch();
        if (value == null) throw new NoSuchElementException();
        isFetched = false;
        return value;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy