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

com.github.skjolber.stcsv.builder.AbstractCsvFieldMapperBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.25
Show newest version
package com.github.skjolber.stcsv.builder;

import java.lang.reflect.Method;

import com.github.skjolber.stcsv.AbstractColumn;
import com.github.skjolber.stcsv.CsvMapper;
import com.github.skjolber.stcsv.NoLineBreakQuotedColumn;
import com.github.skjolber.stcsv.PlainColumn;
import com.github.skjolber.stcsv.PlainFixedColumn;
import com.github.skjolber.stcsv.QuotedColumn;
import com.github.skjolber.stcsv.QuotedFixedColumn;
import com.github.skjolber.stcsv.projection.SetterValueProjection;
import com.github.skjolber.stcsv.projection.ValueProjection;

public abstract class AbstractCsvFieldMapperBuilder> {

	protected final String name;
	protected boolean optional;
	protected Integer fixedSize;
	protected boolean quoted;
	protected boolean linebreaks;

	protected boolean trimTrailingWhitespaces = false;
	protected boolean trimLeadingWhitespaces = false;

	protected B parent;

	public AbstractCsvFieldMapperBuilder(B parent, String name) {
		super();
		this.parent = parent;
		this.name = name;
	}

	protected String getName() {
		return name;
	}
	
	protected boolean isOptional() {
		return optional;
	}

	public B optional() {
		this.optional = true;
		parent.field(this);
		
		return parent;
	}
	
	public AbstractCsvFieldMapperBuilder fixedSize(int fixedSize) {
		this.fixedSize = fixedSize;
		
		return this;
	}

	public B required() {
		this.optional = false;

		parent.field(this);

		return parent;
	}

	protected Integer getFixedSize() {
		return fixedSize;
	}

	protected boolean isQuoted() {
		return quoted;
	}

	public Boolean isLinebreaks() {
		return linebreaks;
	}
	
	/**
	 * Indicate that this field is quoted. If there is no linebreaks, 
	 * rather use the method {@linkplain #quotedWithoutLinebreaks()} to improve performance.
	 * 
	 * @return this instance.
	 */
	
	public AbstractCsvFieldMapperBuilder quoted() {
		quoted = true;
		linebreaks = true;
		
		return this;
	}

	/**
	 * Indicate that this field is quoted, but has no linebreaks.
	 * 
	 * @return this instance.
	 */
	public AbstractCsvFieldMapperBuilder quotedWithoutLinebreaks() {
		quoted = true;
		linebreaks = false;
		
		return this;
	}

	public AbstractCsvFieldMapperBuilder trimTrailingWhitespaces() {
		this.trimTrailingWhitespaces = true;
		
		return this;
	}

	public AbstractCsvFieldMapperBuilder trimLeadingWhitespaces() {
		this.trimLeadingWhitespaces = true;
		
		return this;
	}

	protected boolean isTrimLeadingWhitespaces() {
		return trimLeadingWhitespaces;
	}
	
	protected boolean isTrimTrailingWhitespaces() {
		return trimTrailingWhitespaces;
	}

	public AbstractColumn build(int index, SetterProjectionHelper proxy) throws CsvBuilderException {
		AbstractColumn column;
		if(quoted) {
			if(fixedSize != null) {
				column = new QuotedFixedColumn(name, index, parent.getQuoteCharacter(), parent.getEscapeCharacter(), optional, trimTrailingWhitespaces, trimLeadingWhitespaces, fixedSize);
			} else if(!linebreaks) {
				column = new NoLineBreakQuotedColumn(name, index, parent.getQuoteCharacter(), parent.getEscapeCharacter(), optional, trimTrailingWhitespaces, trimLeadingWhitespaces);
			} else {
				column = new QuotedColumn(name, index, parent.getQuoteCharacter(), parent.getEscapeCharacter(), optional, trimTrailingWhitespaces, trimLeadingWhitespaces);
			}
		} else {
			if(fixedSize != null) {
				column = new PlainFixedColumn(name, index, optional, trimTrailingWhitespaces, trimLeadingWhitespaces, fixedSize);
			} else {
				column = new PlainColumn(name, index, optional, trimTrailingWhitespaces, trimLeadingWhitespaces);
			}
		}
		
		column.setProjection(getProjection(index, proxy));
		
		return column;
	}

	protected ValueProjection getProjection(int index, SetterProjectionHelper proxy) throws CsvBuilderException {
		Method method = proxy.toMethod(this);
		
		return new SetterValueProjection(method.getName(), method.getParameterTypes()[0], CsvMapper.getInternalName(parent.getTarget()));
	}
	
	protected Class getColumnClass() {
		throw new RuntimeException();
	}
	
	protected void invokeSetter(T value) {
		throw new RuntimeException("No setter for column '" + name + "'");
	}

	protected boolean hasSetter() {
		return false;
	}

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy