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

org.simpleflatmapper.map.MapperConfig 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;

import org.simpleflatmapper.map.error.RethrowConsumerErrorHandler;
import org.simpleflatmapper.map.error.RethrowFieldMapperErrorHandler;
import org.simpleflatmapper.map.error.RethrowMapperBuilderErrorHandler;
import org.simpleflatmapper.map.impl.IdentityFieldMapperColumnDefinitionProvider;
import org.simpleflatmapper.map.mapper.ColumnDefinitionProvider;
import org.simpleflatmapper.map.mapper.DefaultPropertyNameMatcherFactory;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.util.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.simpleflatmapper.util.Asserts.requireNonNull;

public final class MapperConfig, S> {
    public static final int NO_ASM_MAPPER_THRESHOLD = 792; // see https://github.com/arnaudroger/SimpleFlatMapper/issues/152
    public static final int MAX_METHOD_SIZE = 128;


    public static , S> MapperConfig fieldMapperConfig() {
        return new MapperConfig(
                new IdentityFieldMapperColumnDefinitionProvider(),
                DefaultPropertyNameMatcherFactory.DEFAULT,
                RethrowMapperBuilderErrorHandler.INSTANCE,
                false,
                NO_ASM_MAPPER_THRESHOLD,
                RethrowFieldMapperErrorHandler.INSTANCE,
                RethrowConsumerErrorHandler.INSTANCE, MAX_METHOD_SIZE, 
                false, Collections.>emptyList(), null, false);
    }

    public static , S> MapperConfig config(ColumnDefinitionProvider columnDefinitionProvider) {
        return new MapperConfig(
                columnDefinitionProvider,
                DefaultPropertyNameMatcherFactory.DEFAULT,
                RethrowMapperBuilderErrorHandler.INSTANCE,
                false,
                NO_ASM_MAPPER_THRESHOLD,
                RethrowFieldMapperErrorHandler.INSTANCE,
                RethrowConsumerErrorHandler.INSTANCE, MAX_METHOD_SIZE, false, Collections.>emptyList(), null, false);
    }

    private final ColumnDefinitionProvider columnDefinitions;
    private final PropertyNameMatcherFactory propertyNameMatcherFactory;
    private final MapperBuilderErrorHandler mapperBuilderErrorHandler;
    private final boolean failOnAsm;
    private final int asmMapperNbFieldsLimit;
    private final FieldMapperErrorHandler fieldMapperErrorHandler;
    private final ConsumerErrorHandler consumerErrorHandler;
    private final int maxMethodSize;
    private final boolean assumeInjectionModifiesValues;
    private final List> discriminators;
    private final Predicate rowFilter;
    
    private final boolean unorderedJoin;

    private MapperConfig(
            ColumnDefinitionProvider columnDefinitions,
            PropertyNameMatcherFactory propertyNameMatcherFactory,
            MapperBuilderErrorHandler mapperBuilderErrorHandler,
            boolean failOnAsm,
            int asmMapperNbFieldsLimit,
            FieldMapperErrorHandler fieldMapperErrorHandler,
            ConsumerErrorHandler consumerErrorHandler,
            int maxMethodSize, boolean assumeInjectionModifiesValues,
            List> discriminators, Predicate rowFilter, boolean unorderedJoin) {
        this.columnDefinitions = columnDefinitions;
        this.propertyNameMatcherFactory = propertyNameMatcherFactory;
        this.mapperBuilderErrorHandler = mapperBuilderErrorHandler;
        this.failOnAsm = failOnAsm;
        this.asmMapperNbFieldsLimit = asmMapperNbFieldsLimit;
        this.fieldMapperErrorHandler = fieldMapperErrorHandler;
        this.consumerErrorHandler = consumerErrorHandler;
        this.maxMethodSize = maxMethodSize;
        this.assumeInjectionModifiesValues = assumeInjectionModifiesValues;
        this.discriminators = discriminators;
        this.rowFilter = rowFilter;
        this.unorderedJoin = unorderedJoin;
    }

    public ColumnDefinitionProvider columnDefinitions() {
        return columnDefinitions;
    }

    public PropertyNameMatcherFactory propertyNameMatcherFactory() {
        return propertyNameMatcherFactory;
    }

    public MapperBuilderErrorHandler mapperBuilderErrorHandler() {
        return mapperBuilderErrorHandler;
    }

    public boolean failOnAsm() {
        return failOnAsm;
    }
    
    public boolean assumeInjectionModifiesValues() {
        return assumeInjectionModifiesValues;
    }

    public int asmMapperNbFieldsLimit() {
        return asmMapperNbFieldsLimit;
    }

    public MapperConfig columnDefinitions(ColumnDefinitionProvider columnDefinitions) {
        requireNonNull("columnDefinitions", columnDefinitions);
        return
            new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                    consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig propertyNameMatcherFactory(PropertyNameMatcherFactory propertyNameMatcherFactory) {
        return new MapperConfig(
                columnDefinitions,
                requireNonNull("propertyNameMatcherFactory", propertyNameMatcherFactory),
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig mapperBuilderErrorHandler(MapperBuilderErrorHandler mapperBuilderErrorHandler) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                requireNonNull("mapperBuilderErrorHandler", mapperBuilderErrorHandler),
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig failOnAsm(boolean failOnAsm) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }



    public MapperConfig assumeInjectionModifiesValues(boolean assumeInjectionModifiesValues) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig asmMapperNbFieldsLimit(int asmMapperNbFieldsLimit) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig fieldMapperErrorHandler(FieldMapperErrorHandler fieldMapperErrorHandler) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler, maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig consumerErrorHandler(ConsumerErrorHandler consumerErrorHandler) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public MapperConfig rowFilter(Predicate rowFilter) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }
    
    public MapperConfig unorderedJoin(boolean unorderedJoin) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }
    
    public boolean unorderedJoin() {
        return unorderedJoin;
    }

    public ConsumerErrorHandler consumerErrorHandler() {
        return consumerErrorHandler;
    }

    @Deprecated
    public MapperConfig rowHandlerErrorHandler(ConsumerErrorHandler rowHandlerErrorHandler) {
        return consumerErrorHandler(rowHandlerErrorHandler);
    }

    public ConsumerErrorHandler rowHandlerErrorHandler() {
        return consumerErrorHandler();
    }

    public boolean hasFieldMapperErrorHandler() {
        return fieldMapperErrorHandler != null
                && !(fieldMapperErrorHandler instanceof RethrowFieldMapperErrorHandler);
    }

    public FieldMapperErrorHandler fieldMapperErrorHandler() {
        return fieldMapperErrorHandler;
    }

    public int maxMethodSize() {
        return maxMethodSize;
    }

    public MapperConfig maxMethodSize(int maxMethodSize) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize, assumeInjectionModifiesValues, discriminators, rowFilter, unorderedJoin);
    }

    public  MapperConfig discriminator(Class rootClass, Predicate discriminatorPredicate, DiscriminatorCase... cases) {
        return discriminator((Type)rootClass, discriminatorPredicate, cases);
    }
    public  MapperConfig discriminator(Type rootClass, Predicate discriminatorPredicate, DiscriminatorCase... cases) {
        List> discriminators = new ArrayList>(this.discriminators);
        discriminators.add(new Discriminator(rootClass, cases, discriminatorPredicate));
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize,
                assumeInjectionModifiesValues,
                discriminators,
                rowFilter,
                unorderedJoin);
    }

    public  Discriminator[] getDiscriminators(ClassMeta classMeta) {
        return getDiscriminators(classMeta.getType());
    }

    public  Discriminator[] getDiscriminators(Type type) {
        List> list = new ArrayList>();
        for(Discriminator d : discriminators) {
            if (TypeHelper.areEquals(type, d.type)) {
                list.add((Discriminator) d);
            }
        }
        return list.toArray(new Discriminator[0]);
    }

    public List> getDiscriminators() {
        return discriminators;
    }

    public MapperConfig discriminators(List> discriminators) {
        return new MapperConfig(
                columnDefinitions,
                propertyNameMatcherFactory,
                mapperBuilderErrorHandler,
                failOnAsm,
                asmMapperNbFieldsLimit,
                fieldMapperErrorHandler,
                consumerErrorHandler,
                maxMethodSize,
                assumeInjectionModifiesValues,
                discriminators, 
                rowFilter, unorderedJoin);
    }

    public DiscriminatorCase getDiscriminatorCase(Type type) {
        return null;
    }

    public  UnaryFactory> applyEnumerableFilter(final UnaryFactory> enumerableFactory) {
        final Predicate rowFilter = this.rowFilter;
        if (rowFilter == null) return enumerableFactory;
        return new UnaryFactory>() {
            @Override
            public Enumerable newInstance(SET set) {
                Enumerable enumerable = enumerableFactory.newInstance(set);
                return new PredicatedEnumerable(enumerable, rowFilter);
            }
        };
    }


    public static final class DiscriminatorCase, T> {
        public final Function, Predicate> predicateFactory;
        public final ClassMeta classMeta;

        public DiscriminatorCase(Function, Predicate> predicateFactory, ClassMeta classMeta) {
            this.predicateFactory = predicateFactory;
            this.classMeta = classMeta;
        }
    }
    
    public static final class Discriminator, T> {
        public final Type type;
        public final DiscriminatorCase[] cases;
        public final Predicate discriminatorPredicate;

        public Discriminator(Type type, DiscriminatorCase[] cases, Predicate discriminatorPredicate) {
            this.type = type;
            this.cases = cases;
            this.discriminatorPredicate = discriminatorPredicate;
        }

        public DiscriminatorCase getCase(Type type) {
            for(DiscriminatorCase c : cases) {
                if (TypeHelper.areEquals(type, c.classMeta.getType())) {
                    return c;
                }
            }
            throw new IllegalArgumentException("Not cases for type " + type);
        }

        public boolean isCompatibleWithKeys(List allDiscriminatoryKeys) {
            if (discriminatorPredicate == null) return true;
            for(K k : allDiscriminatoryKeys) {
                if (!discriminatorPredicate.test(k)) return false;
            }
            return true;
        }
    }

}