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

org.simpleflatmapper.map.mapper.MapperBiInstantiatorFactory 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.MappingContext;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.getter.ContextualGetter;
import org.simpleflatmapper.map.getter.ContextualGetterBiFunction;
import org.simpleflatmapper.map.getter.ContextualGetterBiInstantiator;
import org.simpleflatmapper.map.getter.ContextualGetterFactory;
import org.simpleflatmapper.reflect.BiInstantiator;
import org.simpleflatmapper.reflect.InstantiatorDefinition;
import org.simpleflatmapper.reflect.InstantiatorFactory;
import org.simpleflatmapper.reflect.Parameter;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.ForEachCallBack;
import org.simpleflatmapper.util.TypeHelper;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapperBiInstantiatorFactory {

    private final InstantiatorFactory instantiatorFactory;

    public MapperBiInstantiatorFactory(InstantiatorFactory instantiatorFactory) {
        this.instantiatorFactory = instantiatorFactory;
    }


    public > BiInstantiator, T> getBiInstantiator(Type source, Type target, PropertyMappingsBuilder propertyMappingsBuilder, Map> constructorParameterGetterMap, ContextualGetterFactory getterFactory, boolean builderIgnoresNullValues, MappingContextFactoryBuilder mappingContextFactoryBuilder) throws NoSuchMethodException {
        return  getBiInstantiator(source, target, propertyMappingsBuilder, constructorParameterGetterMap, getterFactory, true, builderIgnoresNullValues, mappingContextFactoryBuilder);
    }

    @SuppressWarnings("unchecked")
    public > BiInstantiator, T> getBiInstantiator(final Type source, final Type target, final PropertyMappingsBuilder propertyMappingsBuilder, final Map> constructorParameterGetterMap, final ContextualGetterFactory getterFactory, final boolean useAsmIfEnabled, final boolean builderIgnoresNullValues, final MappingContextFactoryBuilder mappingContextFactoryBuilder) throws NoSuchMethodException {

        if (propertyMappingsBuilder.isSelfProperty()) {
            ContextualGetter getter = propertyMappingsBuilder.forEachProperties(new ForEachCallBack>() {
                public ContextualGetter getter;
                @Override
                public void handle(PropertyMapping propertyMapping) {
                    getter = getterFactory.newGetter(propertyMapping.getPropertyMeta().getPropertyType(), propertyMapping.getColumnKey(), mappingContextFactoryBuilder, propertyMapping.getColumnDefinition().properties());
                }
            }).getter;
            return new ContextualGetterBiInstantiator(getter);
        }

        if (TypeHelper.isArray(target)) {
            return instantiatorFactory., T>getArrayBiInstantiator(TypeHelper.toClass(TypeHelper.getComponentTypeOfListOrArray(target)), propertyMappingsBuilder.forEachProperties(new CalculateMaxIndex()).maxIndex + 1);
        } else {
            List instantiatorDefinitions = propertyMappingsBuilder.getPropertyFinder().getEligibleInstantiatorDefinitions();
            return
                    instantiatorFactory.
                            , T>
                                    getBiInstantiator(
                                            target,
                                            TypeHelper.toClass(source),
                                            MappingContext.class,
                                            instantiatorDefinitions,
                                            convertToBiInstantiator(constructorParameterGetterMap),
                                            useAsmIfEnabled, builderIgnoresNullValues);
        }
    }

    public static  Map, ?>> convertToBiInstantiator(Map> constructorParameterGetterMap) {
        Map, ?>> newMap = new HashMap, ?>>(constructorParameterGetterMap.size());
        
        for(Map.Entry> e : constructorParameterGetterMap.entrySet()) {
            newMap.put(e.getKey(), ContextualGetterBiFunction.of(e.getKey().getType(), e.getValue()));                 
        }
        
        return newMap;
    }

}