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

org.sfm.csv.parser.StringArrayConsumer 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.impl.cellreader.StringCellValueReader;
import org.sfm.utils.ErrorHelper;
import org.sfm.utils.RowHandler;

public final class StringArrayConsumer> implements CellConsumer {
	private final RH handler;
	private String[] currentRow = new String[10];
	private int currentIndex;


	public StringArrayConsumer(RH handler) {
		this.handler = handler;
	}

	@Override
	public void newCell(char[] chars, int offset, int length) {
		if (currentIndex >= currentRow.length) {
			doubleSize();
		}
		currentRow[currentIndex++] = StringCellValueReader.readString(chars, offset, length);
	}

	private void doubleSize() {
		String[] newArray = new String[currentRow.length * 2];
		System.arraycopy(currentRow, 0, newArray, 0, currentIndex);
		currentRow = newArray;
	}

	@Override
	public void endOfRow() {
		try {
			String[] result = new String[currentIndex];
			System.arraycopy(currentRow, 0, result, 0, currentIndex);
			handler.handle(result);
			currentIndex = 0;
		} catch (Exception e) {
            ErrorHelper.rethrow(e);
		}
	}

	public RH handler() {
		return handler;
	}

	@Override
	public void end() {
		if (!isEmpty()) {
			endOfRow();
		}
	}

	private boolean isEmpty() {
		return currentIndex == 0;
	}

	public static > StringArrayConsumer newInstance(RH handler) {
		return new StringArrayConsumer(handler);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy