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

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

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.github.skjolber.stcsv.AbstractColumn;

public abstract class AbstractCsvMappingBuilder> extends AbstractCsvBuilder  {

	protected static final boolean byteBuddy;
	
	static {
		boolean present;
		try {
			Class.forName("net.bytebuddy.ByteBuddy");
			
			present = true;
		} catch(Exception e) {
			present = false;
		}
		byteBuddy = present;
	}
	
	protected boolean skippableFieldsWithoutLinebreaks = false;

	protected Class target;
	
	protected ClassLoader classLoader;

	protected List>> fields = new ArrayList<>();

	public AbstractCsvMappingBuilder(Class cls) {
		this.target = cls;
	}
	
	public B classLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
		
		return (B) this;
	}

	protected List toColumns() throws CsvBuilderException {
		List columns = new ArrayList<>(fields.size());
		Set fieldNames = new HashSet<>(fields.size() * 2);

		SetterProjectionHelper proxy;
		if(byteBuddy) {
			proxy = new ByteBuddySetterProjectionHelper(target);
		} else {
			proxy = new SetterProjectionHelper(target);
		}
		
		for (int i = 0; i < fields.size(); i++) {
			AbstractCsvFieldMapperBuilder builder = fields.get(i);
			
			String name = builder.getName();
			if(fieldNames.contains(name)) {
				throw new CsvBuilderException("Duplicate field '" + name + "'");
			}
			fieldNames.add(name);
			
			if(builder.isLinebreaks() && !builder.isQuoted()) {
				throw new CsvBuilderException();
			}

			columns.add(builder.build(i, proxy));
		}
		return columns;
	}

	protected ClassLoader getDefaultClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}
	
	protected B field(AbstractCsvFieldMapperBuilder> field) {
		this.fields.add(field);
		
		return (B) this;
	}

	public Class getTarget() {
		return target;
	}	
	

	/**
	 * 
	 * All fields (including any ignored columns) are without linebreaks.
	 * 
	 * @return this
	 */

	@SuppressWarnings("unchecked")
	public B skippableFieldsWithoutLinebreaks() {
		this.skippableFieldsWithoutLinebreaks = true;
		
		return (B) this;
	}
		
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy