All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.

There is a newer version: 9.0.2
Show newest version
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);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy