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

org.simpleflatmapper.csv.CsvWriterBuilder Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

The newest version!
package org.simpleflatmapper.csv;


import org.simpleflatmapper.csv.impl.writer.CellSeparatorAppender;
import org.simpleflatmapper.csv.impl.writer.EndOfRowAppender;
import org.simpleflatmapper.csv.mapper.FieldMapperToAppendableFactory;
import org.simpleflatmapper.lightningcsv.CellWriter;
import org.simpleflatmapper.lightningcsv.CsvCellWriter;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.mapper.AbstractConstantTargetMapperBuilder;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactory;
import org.simpleflatmapper.map.mapper.PropertyMapping;
import org.simpleflatmapper.reflect.BiInstantiator;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import java.util.List;

public class CsvWriterBuilder extends AbstractConstantTargetMapperBuilder> {

    private final CellWriter cellWriter;
    private final CellSeparatorAppender cellSeparatorAppender;

    public CsvWriterBuilder(
            ClassMeta classMeta,
            MapperConfig mapperConfig,
            ConstantTargetFieldMapperFactory fieldAppenderFactory,
            CellWriter cellWriter) {
        super(classMeta, Appendable.class, mapperConfig, fieldAppenderFactory);
        this.cellWriter = cellWriter;
        this.cellSeparatorAppender = new CellSeparatorAppender(cellWriter);
    }

    public static  CsvWriterBuilder newBuilder(Class clazz) {
        ClassMeta classMeta = ReflectionService.newInstance().getClassMeta(clazz);
        return CsvWriterBuilder.newBuilder(classMeta);
    }

    public static  CsvWriterBuilder newBuilder(ClassMeta classMeta) {
        return CsvWriterBuilder.newBuilder(classMeta, CsvCellWriter.DEFAULT_WRITER);
    }

    public static  CsvWriterBuilder newBuilder(ClassMeta classMeta, CellWriter cellWriter) {
        MapperConfig config =
                MapperConfig.fieldMapperConfig();
        FieldMapperToAppendableFactory appenderFactory = new FieldMapperToAppendableFactory(cellWriter);
        CsvWriterBuilder builder =
                new CsvWriterBuilder(
                        classMeta,
                        config,
                        appenderFactory,
                        cellWriter);
        return builder;
    }

    @Override
    protected BiInstantiator, Appendable> getInstantiator() {
        return new AppendableInstantiator();
    }

    @Override
    protected CsvColumnKey newKey(String column, int i, FieldMapperColumnDefinition columnDefinition) {
        return new CsvColumnKey(column, i);
    }

    @Override
    protected void preFieldProcess(List> fieldMappers, PropertyMapping pm) {
        if (pm.getColumnKey().getIndex() > 0) {
            fieldMappers.add(cellSeparatorAppender);
        }
    }

    @Override
    protected void postMapperProcess(List> fieldMappers) {
        fieldMappers.add(new EndOfRowAppender(cellWriter));
    }

    private static class AppendableInstantiator implements BiInstantiator, Appendable> {
        @Override
        public Appendable newInstance(T o, MappingContext context) throws Exception {
            return new StringBuilder();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy