org.sfm.jooq.SfmRecordMapperProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.sfm.jooq;
import org.jooq.*;
import org.sfm.map.Mapper;
import org.sfm.map.impl.MapperCache;
import org.sfm.map.impl.TargetColumnsMapperKey;
public class SfmRecordMapperProvider implements RecordMapperProvider {
private final MapperCache> mapperCache = new MapperCache>();
@Override
public RecordMapper provide(RecordType recordType, Class extends E> type) {
TargetColumnsMapperKey key = getMapperKey(recordType, type);
@SuppressWarnings("unchecked")
Mapper mapper = (Mapper) mapperCache.get(key);
if (mapper == null) {
JooqMapperBuilder mapperBuilder = new JooqMapperBuilder(type);
int i = 0;
for(Field> field : recordType.fields()) {
mapperBuilder.addField(new JooqFieldKey(field, i++));
}
mapper = mapperBuilder.mapper();
mapperCache.add(key, mapper);
}
return new RecordMapperWrapper(mapper);
}
private TargetColumnsMapperKey getMapperKey(RecordType recordType, Class> type) {
String[] columns = new String[recordType.size()];
int i = 0;
for(Field> field : recordType.fields()) {
columns[i++] = field.getName();
}
return new TargetColumnsMapperKey(type, columns);
}
}