org.simpleflatmapper.csv.CsvMapperFactory Maven / Gradle / Ivy
package org.simpleflatmapper.csv;
import org.simpleflatmapper.csv.impl.CellValueReaderFactoryImpl;
import org.simpleflatmapper.csv.impl.CsvColumnDefinitionProviderImpl;
import org.simpleflatmapper.csv.impl.DynamicCsvMapper;
import org.simpleflatmapper.map.MapperBuildingException;
import org.simpleflatmapper.map.mapper.AbstractMapperFactory;
import org.simpleflatmapper.util.TypeReference;
import org.simpleflatmapper.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 name 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 {
final ClassMeta classMeta = getClassMeta(target);
return newMapper(classMeta);
}
public CsvMapper newMapper(final ClassMeta classMeta) throws MapperBuildingException {
return new DynamicCsvMapper(
classMeta.getType(),
classMeta,
defaultDateFormat,
cellValueReaderFactory,
mapperConfig());
}
/**
* Will create a newInstance 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) {
final ClassMeta classMeta = getClassMeta(target);
return newBuilder(classMeta);
}
public CsvMapperBuilder newBuilder(final ClassMeta classMeta) {
CsvMapperBuilder builder =
new CsvMapperBuilder(classMeta.getType(), classMeta,0, cellValueReaderFactory, mapperConfig());
builder.setDefaultDateFormat(defaultDateFormat);
return builder;
}
}