org.simpleflatmapper.csv.CsvMapperBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-csv Show documentation
Show all versions of sfm-csv Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.simpleflatmapper.csv;
import org.simpleflatmapper.csv.mapper.CsvMappingContextFactoryBuilder;
import org.simpleflatmapper.csv.mapper.CsvRowEnumerableFactory;
import org.simpleflatmapper.csv.mapper.CsvRowGetterFactory;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.getter.ContextualGetterFactory;
import org.simpleflatmapper.map.mapper.AbstractColumnDefinitionProvider;
import org.simpleflatmapper.map.mapper.ColumnDefinitionProvider;
import org.simpleflatmapper.map.mapper.DefaultSetRowMapperBuilder;
import org.simpleflatmapper.map.mapper.KeyFactory;
import org.simpleflatmapper.map.mapper.MapperBuilder;
import org.simpleflatmapper.map.mapper.MapperSourceImpl;
import org.simpleflatmapper.map.property.DefaultDateFormatProperty;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.csv.impl.*;
import org.simpleflatmapper.reflect.meta.PropertyMeta;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.ConstantPredicate;
import org.simpleflatmapper.util.Consumer;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
public class CsvMapperBuilder extends MapperBuilder, CsvMapper, CsvMapperBuilder> {
public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final MapperSourceImpl FIELD_MAPPER_SOURCE =
new MapperSourceImpl(CsvRow.class, CsvRowGetterFactory.INSTANCE);
private static final KeyFactory KEY_FACTORY = new KeyFactory() {
@Override
public CsvColumnKey newKey(String name, int i) {
return new CsvColumnKey(name, i);
}
};
public static final CsvColumnKey[] EMPTY_KEYS = new CsvColumnKey[0];
private static ColumnDefinitionProvider addDefaultDateFormat(AbstractColumnDefinitionProvider columnDefinitionProvider, String defaultDateFormat) {
columnDefinitionProvider.addColumnProperty(ConstantPredicate.truePredicate(), new DefaultDateFormatProperty(defaultDateFormat));
return columnDefinitionProvider;
}
private final ClassMeta classMeta;
public CsvMapperBuilder(final Type target) {
this(target, ReflectionService.newInstance());
}
@SuppressWarnings("unchecked")
public CsvMapperBuilder(final Type target, ReflectionService reflectionService) {
this((ClassMeta)reflectionService.getClassMeta(target));
}
public CsvMapperBuilder(final ClassMeta classMeta) {
this(classMeta, addDefaultDateFormat(new CsvColumnDefinitionProviderImpl(), DEFAULT_DATE_FORMAT));
}
public CsvMapperBuilder(final ClassMeta classMeta, ColumnDefinitionProvider columnDefinitionProvider) {
this(
classMeta,
MapperConfig.config(columnDefinitionProvider),
CsvRowGetterFactory.INSTANCE,
new CsvMappingContextFactoryBuilder(!MapperConfig.config(columnDefinitionProvider).unorderedJoin())
);
}
/**
* @param classMeta the meta for the target class.
* @param mapperConfig the mapperConfig.
* @param getterFactory the Getter factory.
* @param parentBuilder the parent builder, null if none.
*/
public CsvMapperBuilder(
final ClassMeta classMeta,
final MapperConfig mapperConfig,
final ContextualGetterFactory super CsvRow, CsvColumnKey> getterFactory,
final MappingContextFactoryBuilder parentBuilder) {
super(KEY_FACTORY,
new DefaultSetRowMapperBuilder(
classMeta, parentBuilder, mapperConfig,
FIELD_MAPPER_SOURCE.getterFactory(getterFactory), KEY_FACTORY, new CsvRowEnumerableFactory(),
CsvRowKeySourceGetter.INSTANCE),
new BiFunction, List, CsvMapper>() {
@Override
public CsvMapper apply(SetRowMapper setRowMapper, List keys) {
return new CsvMapperImpl(setRowMapper, keys.toArray(EMPTY_KEYS));
}
}, CsvColumnDefinition.COLUMN_DEFINITION_FACTORY, 0 );
this.classMeta = classMeta;
}
public void addDefaultHeaders() {
addDefaultHeaders(classMeta, "");
}
private void addDefaultHeaders(final ClassMeta
classMeta, final String prefix) {
classMeta.forEachProperties(new Consumer>() {
@Override
public void accept(PropertyMeta propertyMeta) {
String currentName = prefix + propertyMeta.getPath();
if (propertyMeta.isSubProperty()) {
addDefaultHeaders(propertyMeta.getPropertyClassMeta(), currentName + "_");
} else {
addMapping(currentName);
}
}
});
}
}