org.simpleflatmapper.jdbi.SfmResultSetMapperFactory Maven / Gradle / Ivy
package org.simpleflatmapper.jdbi;
import org.simpleflatmapper.jdbc.DynamicJdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.simpleflatmapper.map.ContextualSourceMapper;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.util.BiPredicate;
import org.simpleflatmapper.util.UnaryFactory;
import org.skife.jdbi.v2.ResultSetMapperFactory;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import java.sql.ResultSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class SfmResultSetMapperFactory implements ResultSetMapperFactory {
private static final UnaryFactory, ContextualSourceMapper> DEFAULT_FACTORY = new UnaryFactory, ContextualSourceMapper>() {
@Override
public ContextualSourceMapper newInstance(Class> aClass) {
return JdbcMapperFactory.newInstance().newMapper(aClass);
}
};
private static final BiPredicate, StatementContext> DEFAULT_ACCEPT_PREDICATE = new BiPredicate, StatementContext>() {
@Override
public boolean test(Class> aClass, StatementContext statementContext) {
return true;
}
};
private final UnaryFactory, ContextualSourceMapper> mapperFactory;
private final ConcurrentMap, ResultSetMapper>> cache = new ConcurrentHashMap, ResultSetMapper>>();
private final BiPredicate, StatementContext> acceptsPredicate;
public SfmResultSetMapperFactory() {
this(DEFAULT_FACTORY);
}
public SfmResultSetMapperFactory(UnaryFactory, ContextualSourceMapper> mapperFactory) {
this(DEFAULT_ACCEPT_PREDICATE, mapperFactory);
}
public SfmResultSetMapperFactory(BiPredicate, StatementContext> acceptsPredicate, UnaryFactory, ContextualSourceMapper> mapperFactory) {
this.mapperFactory = mapperFactory;
this.acceptsPredicate = acceptsPredicate;
}
@Override
public boolean accepts(Class aClass, StatementContext statementContext) {
return acceptsPredicate.test(aClass, statementContext);
}
@SuppressWarnings("unchecked")
@Override
public ResultSetMapper mapperFor(Class aClass, StatementContext statementContext) {
ResultSetMapper mapper = cache.get(aClass);
if (mapper == null) {
ContextualSourceMapper resultSetMapper = mapperFactory.newInstance(aClass);
mapper = toResultSetMapper(resultSetMapper);
ResultSetMapper> cachedMapper = cache.putIfAbsent(aClass, mapper);
if (cachedMapper != null) {
mapper = cachedMapper;
}
}
return mapper;
}
private ResultSetMapper toResultSetMapper(ContextualSourceMapper resultSetMapper) {
ResultSetMapper mapper;
if (resultSetMapper instanceof DynamicJdbcMapper) {
mapper = new DynamicSfmResultSetMapper((DynamicJdbcMapper) resultSetMapper);
} else {
mapper = new DefaultSfmResultSetMapper(resultSetMapper);
}
return mapper;
}
}