org.simpleflatmapper.map.mapper.DynamicSetRowMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-map Show documentation
Show all versions of sfm-map Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.simpleflatmapper.map.mapper;
import org.simpleflatmapper.map.FieldKey;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.util.Enumerable;
import org.simpleflatmapper.util.ErrorHelper;
import org.simpleflatmapper.util.UnaryFactory;
import org.simpleflatmapper.util.CheckedConsumer;
import org.simpleflatmapper.util.UnaryFactoryWithException;
import java.util.Iterator;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END
public class DynamicSetRowMapper> implements SetRowMapper {
private final MapperCache> mapperCache;
private final UnaryFactory, SetRowMapper> mapperFactory;
private final UnaryFactoryWithException, E> mapperKeyFromRow;
private final UnaryFactoryWithException, E> mapperKeyFromSet;
public DynamicSetRowMapper(
UnaryFactory, SetRowMapper> mapperFactory,
UnaryFactoryWithException, E> mapperKeyFromRow,
UnaryFactoryWithException, E> mapperKeyFromSet,
MapperKeyComparator keyComparator) {
this.mapperFactory = mapperFactory;
this.mapperKeyFromRow = mapperKeyFromRow;
this.mapperKeyFromSet = mapperKeyFromSet;
this.mapperCache = new MapperCache>(keyComparator);
}
@Override
public final T map(ROW row) throws MappingException {
try {
return getMapperFromRow(row).map(row);
} catch(Exception e) {
ErrorHelper.rethrow(e);
return null;
}
}
@Override
public final T map(ROW row, MappingContext super ROW> context) throws MappingException {
try {
return getMapperFromRow(row).map(row, context);
} catch(Exception e) {
ErrorHelper.rethrow(e);
return null;
}
}
@Override
public final Iterator iterator(SET rs) throws E, MappingException {
return getMapperFromSet(rs).iterator(rs);
}
//IFJAVA8_START
@Override
public final Stream stream(SET set) throws E, MappingException {
return getMapperFromSet(set).stream(set);
}
//IFJAVA8_END
@Override
public Enumerable enumerate(SET source) throws E, MappingException {
return getMapperFromSet(source).enumerate(source);
}
@Override
public final > H forEach(SET set, H handler) throws E, MappingException {
return getMapperFromSet(set).forEach(set, handler);
}
@Override
public String toString() {
return "DynamicMapper{mapperFactory=" + mapperFactory
+ ", " + mapperCache +
'}';
}
private SetRowMapper getMapperFromSet(SET set) throws E {
return getMapper(mapperKeyFromSet.newInstance(set));
}
private SetRowMapper getMapperFromRow(ROW row) throws E {
return getMapper(mapperKeyFromRow.newInstance(row));
}
public SetRowMapper getMapper(MapperKey key) throws E {
SetRowMapper mapper = mapperCache.get(key);
if (mapper == null) {
mapper = mapperFactory.newInstance(key);
mapperCache.add(key, mapper);
}
return mapper;
}
}