org.simpleflatmapper.jooq.SfmRecordUnmapperProvider Maven / Gradle / Ivy
package org.simpleflatmapper.jooq;
import org.jooq.*;
import org.simpleflatmapper.map.ContextualSourceMapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.util.Function;
import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Integration point with jooq.
* Provide a JooqRecordMapper backed by an Sfm {@link Record} {@link SourceMapper}
*/
public class SfmRecordUnmapperProvider implements RecordUnmapperProvider {
private final ConcurrentMap mapperCache = new ConcurrentHashMap();
private final Function> mapperConfigFactory;
private final ReflectionService reflectionService;
private final Configuration configuration;
@Deprecated
/**
* please use SfmRecorMapperProviderFactory.
*/
public SfmRecordUnmapperProvider(
Function> mapperConfigFactory, ReflectionService reflectionService, Configuration configuration) {
this.mapperConfigFactory = mapperConfigFactory;
this.reflectionService = reflectionService;
this.configuration = configuration;
}
@Override
public RecordUnmapper provide(Class extends E> type, RecordType recordType) {
TargetColumnsMapperKey key = getMapperKey(recordType, type);
ContextualSourceMapper mapper = mapperCache.get(key);
if (mapper == null) {
MapperConfig mapperConfig = mapperConfigFactory.apply(type);
RecordUnmapperBuilder mapperBuilder =
new RecordUnmapperBuilder(
reflectionService.getClassMeta(type),
mapperConfig, configuration);
mapperBuilder.setFields(recordType.fields());
mapper = mapperBuilder.mapper();
mapperCache.putIfAbsent(key, mapper);
}
return new JooqRecordUnmapperWrapper(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);
}
}