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

org.sfm.map.impl.MapperImpl 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: 1.10.3
Show newest version
package org.sfm.map.impl;

import org.sfm.map.FieldMapper;
import org.sfm.map.MappingContext;
import org.sfm.map.MappingContextFactory;
import org.sfm.reflect.Instantiator;

import java.util.Arrays;

public class MapperImpl extends AbstractMapperImpl {
	
	private final FieldMapper[] fieldMappers;

    private final FieldMapper[] constructorMappers;

    public MapperImpl(final FieldMapper[] fieldMappers,
                      final FieldMapper[] constructorMappers,
                      final Instantiator instantiator,
                      final MappingContextFactory mappingContextFactory) {
		super(instantiator, mappingContextFactory);
		this.fieldMappers = fieldMappers;
        this.constructorMappers = constructorMappers;
	}

	protected final void mapFields(final S source, final T target, final MappingContext mappingContext) throws Exception {
        for (FieldMapper fieldMapper : fieldMappers) {
            fieldMapper.mapTo(source, target, mappingContext);
        }
	}

    @Override
    protected final void mapToFields(S source, T target, final MappingContext mappingContext) throws Exception {
        for (FieldMapper constructorMapper : constructorMappers) {
            constructorMapper.mapTo(source, target, mappingContext);
        }

        mapFields(source, target, mappingContext);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(getClass().getSimpleName())
                .append("{");

        super.appendToStringBuilder(sb);
        sb.append(", fieldMappers=").append(Arrays.toString(fieldMappers));
        sb.append("}");

        return sb.toString();
    }
}