org.sfm.csv.impl.CsvMapperCellHandlerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.csv.impl;
import org.sfm.csv.CsvColumnKey;
import org.sfm.map.FieldMapperErrorHandler;
import org.sfm.reflect.Instantiator;
public final class CsvMapperCellHandlerImpl extends CsvMapperCellHandler {
/**
* mapping information
*/
protected final DelayedCellSetter[] delayedCellSetters;
protected final CellSetter[] setters;
public CsvMapperCellHandlerImpl(Instantiator, T> instantiator, DelayedCellSetter[] delayedCellSetters,
CellSetter[] setters, CsvColumnKey[] columns,
ParsingContext parsingContext, FieldMapperErrorHandler fieldErrorHandler) {
super(instantiator, columns, delayedCellSetters.length, setters.length, parsingContext, fieldErrorHandler);
this.delayedCellSetters = delayedCellSetters;
this.setters = setters;
}
@Override
public void delayedCellValue(char[] chars, int offset, int length, int cellIndex) {
try {
DelayedCellSetter delayedCellSetter = delayedCellSetters[cellIndex];
if (delayedCellSetter != null) {
delayedCellSetter.set(chars, offset, length, parsingContext);
}
} catch (Exception e) {
fieldError(cellIndex, e);
}
}
@Override
public void cellValue(char[] chars, int offset, int length, int cellIndex) {
try {
CellSetter cellSetter = getCellSetter(cellIndex);
if (cellSetter != null) {
cellSetter.set(currentInstance, chars, offset, length, parsingContext);
}
} catch (Exception e) {
fieldError(cellIndex, e);
}
}
@Override
public void applyDelayedSetters() {
for (int i = 0; i < delayedCellSetters.length; i++) {
DelayedCellSetter delayedSetter = delayedCellSetters[i];
if (delayedSetter != null && delayedSetter.isSettable()) {
try {
delayedSetter.set(currentInstance);
} catch (Exception e) {
fieldError(i, e);
}
}
}
}
@Override
public DelayedCellSetter getDelayedCellSetter(int index) {
return delayedCellSetters[index];
}
@Override
public final Object peekDelayedCellSetterValue(CsvColumnKey key) {
return delayedCellSetters[key.getIndex()].peekValue();
}
private CellSetter getCellSetter(int cellIndex) {
final int i = cellIndex - delayedCellSetters.length;
if (i < setters.length) {
return setters[i];
} else {
return null;
}
}
}