com.github.skjolber.stcsv.builder.AbstractCsvMappingBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databinder Show documentation
Show all versions of databinder Show documentation
High-performance CSV databinding library
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;
}
}