Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.simpleflatmapper.map.mapper.MapperBuilder Maven / Gradle / Ivy
Go to download
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.FieldMapper;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.map.SourceFieldMapper;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.Function;
import java.util.List;
/**
* @param the targeted type of the mapper
*/
public class MapperBuilder, E extends Exception, IM extends SetRowMapper, OM extends SetRowMapper, B extends MapperBuilder> {
protected final KeyFactory keyFactory;
protected final SetRowMapperBuilder setRowMapperBuilder;
protected final BiFunction, OM> specialisedMapper;
protected final Function> columnDefinitionFactory;
private int calculatedIndex;
public MapperBuilder(
KeyFactory keyFactory,
SetRowMapperBuilder setRowMapperBuilder,
BiFunction, OM> specialisedMapper,
Function> columnDefinitionFactory, int calculatedIndex) {
this.keyFactory = keyFactory;
this.setRowMapperBuilder = setRowMapperBuilder;
this.specialisedMapper = specialisedMapper;
this.columnDefinitionFactory = columnDefinitionFactory;
this.calculatedIndex = calculatedIndex;
}
/**
* @return a new newInstance of the jdbcMapper based on the current state of the builder.
*/
public final OM mapper() {
return specialisedMapper.apply(setRowMapperBuilder.mapper(), setRowMapperBuilder.getKeys());
}
protected final SourceFieldMapper sourceFieldMapper() {
return setRowMapperBuilder.sourceFieldMapper();
}
/**
* add a new mapping to the specified property with a key property definition and an undefined type.
* The index is incremented for each non indexed property mapping.
*
* @param column the property name
* @return the current builder
*/
public final B addKey(String column) {
return addMapping(column, calculatedIndex++, FieldMapperColumnDefinition.key());
}
/**
* add a new mapping to the specified property with an undefined type. The index is incremented for each non indexed property mapping.
*
* @param column the property name
* @return the current builder
*/
public final B addMapping(String column) {
return addMapping(column, calculatedIndex++);
}
/**
* add a new mapping to the specified property with the specified columnDefinition and an undefined type. The index is incremented for each non indexed property mapping.
*
* @param column the property name
* @param columnDefinition the definition
* @return the current builder
*/
public final B addMapping(final String column, final ColumnDefinition columnDefinition) {
return addMapping(column, calculatedIndex++, columnDefinition);
}
/**
* add a new mapping to the specified property with the specified columnDefinition and an undefined type. The index is incremented for each non indexed property mapping.
*
* @param column the property name
* @param properties the definition
* @return the current builder
*/
public final B addMapping(final String column, final Object... properties) {
return addMapping(column, calculatedIndex++, properties);
}
/**
* add a new mapping to the specified property with the specified index and an undefined type.
*
* @param column the property name
* @param index the property index
* @return the current builder
*/
public final B addMapping(String column, int index) {
return addMapping(key(column, index));
}
/**
* add a new mapping to the specified property with the specified index, specified property definition and an undefined type.
*
* @param column the property name
* @param index the property index
* @param columnDefinition the property definition
* @return the current builder
*/
public final B addMapping(String column, int index, final ColumnDefinition columnDefinition) {
return addMapping(key(column, index), columnDefinition);
}
/**
* add a new mapping to the specified property with the specified index, specified property definition and an undefined type.
*
* @param column the property name
* @param index the property index
* @param properties the property properties
* @return the current builder
*/
public final B addMapping(String column, int index, final Object... properties) {
return addMapping(key(column, index), properties);
}
/**
* append a FieldMapper to the mapping list.
*
* @param mapper the field jdbcMapper
* @return the current builder
*/
@SuppressWarnings("unchecked")
public final B addMapper(FieldMapper mapper) {
setRowMapperBuilder.addMapper(mapper);
return (B) this;
}
@SuppressWarnings("unchecked")
public final B addMapping(K key, ColumnDefinition columnDefinition) {
setRowMapperBuilder.addMapping(key, columnDefinition);
return (B) this;
}
public final B addMapping(K key, Object... properties) {
if (properties.length == 1) { // catch Object... on column definition
if (properties[0] instanceof ColumnDefinition) {
return addMapping(key, (ColumnDefinition) properties[0]);
}
}
return addMapping(key, columnDefinitionFactory.apply(properties));
}
private K key(String column, int index) {
return keyFactory.newKey(column, index);
}
}