org.simpleflatmapper.map.mapper.MapperBiInstantiatorFactory 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.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 super S, K> getterFactory, boolean builderIgnoresNullValues, MappingContextFactoryBuilder super S, K> 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 super S, K> getterFactory, final boolean useAsmIfEnabled, final boolean builderIgnoresNullValues, final MappingContextFactoryBuilder super S, K> 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;
}
}