
org.sfm.csv.CsvMapperFactory Maven / Gradle / Ivy
package org.sfm.csv;
import org.sfm.csv.impl.CellValueReaderFactoryImpl;
import org.sfm.csv.impl.CsvColumnDefinitionProviderImpl;
import org.sfm.csv.impl.DynamicCsvMapper;
import org.sfm.map.*;
import org.sfm.reflect.TypeReference;
import org.sfm.reflect.meta.ClassMeta;
import java.lang.reflect.Type;
/**
* CsvMapperFactory is not Thread-Safe but the mappers are.
* It is strongly advised to instantiate one jdbcMapper per class for the life of your application.
*
* You can instantiate dynamic jdbcMapper which will use the first line of the csv file
* to figure out the list of the columns or a static one using a builder.
*
*
* // create a dynamic jdbcMapper targeting MyClass
* CsvMapperFactory
* .newInstance()
* .newMapper(MyClass.class);
*
* // create a static jdbcMapper targeting MyClass
* CsvMapperFactory
* .newInstance()
* .newBuilder(MyClass.class)
* .addMapping("id")
* .addMapping("field1")
* .addMapping("field2")
* .mapper();
*
*
*/
public final class CsvMapperFactory extends AbstractMapperFactory {
/**
* instantiate a new JdbcMapperFactory
* @return a new JdbcMapperFactory
*/
public static CsvMapperFactory newInstance() {
return new CsvMapperFactory();
}
private CellValueReaderFactory cellValueReaderFactory = new CellValueReaderFactoryImpl();
private String defaultDateFormat = "yyyy-MM-dd HH:mm:ss";
private CsvMapperFactory() {
super(new CsvColumnDefinitionProviderImpl(), CsvColumnDefinition.IDENTITY);
}
public CsvMapperFactory defaultDateFormat(final String defaultDateFormat) {
this.defaultDateFormat = defaultDateFormat;
return this;
}
public CsvMapperFactory cellValueReaderFactory(final CellValueReaderFactory cellValueReaderFactory) {
this.cellValueReaderFactory = cellValueReaderFactory;
return this;
}
public CsvMapperFactory addCustomValueReader(String key, CellValueReader> cellValueReader) {
return addColumnDefinition(key, CsvColumnDefinition.customReaderDefinition(cellValueReader));
}
/**
*
* @param target the targeted class for the jdbcMapper
* @param the targeted type
* @return a jdbc jdbcMapper that will map to the targeted class.
* @throws MapperBuildingException if an error occurs building the jdbcMapper
*/
public CsvMapper newMapper(final Class target) throws MapperBuildingException {
return newMapper((Type)target);
}
public CsvMapper newMapper(final TypeReference target) throws MapperBuildingException {
return newMapper(target.getType());
}
public CsvMapper newMapper(final Type target) throws MapperBuildingException {
ClassMeta classMeta = getClassMeta(target);
return new DynamicCsvMapper(
target,
classMeta,
defaultDateFormat,
cellValueReaderFactory,
mapperConfig());
}
/**
* Will create a instance of ResultSetMapperBuilder
* @param target the target class of the jdbcMapper
* @param the targeted type
* @return a builder ready to instantiate a jdbcMapper or to be customized
* @throws MapperBuildingException if an error occurs building the jdbcMapper
*/
public CsvMapperBuilder newBuilder(final Class target) {
return newBuilder((Type)target);
}
public CsvMapperBuilder newBuilder(final TypeReference target) {
return newBuilder(target.getType());
}
public CsvMapperBuilder newBuilder(final Type target) {
ClassMeta classMeta = getClassMeta(target);
CsvMapperBuilder builder =
new CsvMapperBuilder(target, classMeta,0, cellValueReaderFactory, mapperConfig());
builder.setDefaultDateFormat(defaultDateFormat);
return builder;
}
}