com.github.skjolber.stcsv.DefaultStaticCsvMapper2 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;
import java.io.Reader;
import java.lang.reflect.Constructor;
/**
*
* Static CSV parser generator - for specific parser implementation
*
* Thread-safe.
* @param csv line output value
* @param intermediate processor
*/
public class DefaultStaticCsvMapper2 implements StaticCsvMapper2{
// https://stackoverflow.com/questions/28030465/performance-of-invoking-constructor-by-reflection
private final Constructor extends AbstractCsvReader> readerConstructor;
private final Constructor extends AbstractCsvReader> readerArrayConstructor;
public DefaultStaticCsvMapper2(Class extends AbstractCsvReader> cls, Class delegate) throws Exception {
this.readerConstructor = cls.getConstructor(Reader.class, delegate);
this.readerArrayConstructor = cls.getConstructor(Reader.class, char[].class, int.class, int.class, delegate);
}
public AbstractCsvReader newInstance(Reader reader, D delegate) {
try {
return readerConstructor.newInstance(reader, delegate);
} catch (Exception e) {
throw new RuntimeException(e); // should never happen
}
}
public AbstractCsvReader newInstance(Reader reader, char[] current, int offset, int length, D delegate) {
try {
return readerArrayConstructor.newInstance(reader, current, offset, length, delegate);
} catch (Exception e) {
throw new RuntimeException(e); // should never happen
}
}
}