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

org.simpleflatmapper.csv.impl.writer.FormatingAppender Maven / Gradle / Ivy

package org.simpleflatmapper.csv.impl.writer;


import org.simpleflatmapper.lightningcsv.CellWriter;
import org.simpleflatmapper.map.FieldMapper;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.reflect.Getter;

import java.text.Format;

import static org.simpleflatmapper.util.Asserts.requireNonNull;

public class FormatingAppender implements FieldMapper {

    private final Getter getter;
    private final Getter, Format> formatAccessor;
    private final CellWriter cellWriter;

    public FormatingAppender(Getter getter, Getter, Format> formatAccessor, CellWriter cellWriter) {
        this.getter = getter;
        this.formatAccessor = formatAccessor;
        this.cellWriter = cellWriter;
    }

    @Override
    public void mapTo(S source, Appendable target, MappingContext context) throws Exception {
        Object o = getter.get(source);
        if (o != null) {
            Format format = formatAccessor.get(context);
            requireNonNull("Format in mapping context", format);
            cellWriter.writeValue(format.format(o), target);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy