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

org.simpleflatmapper.map.mapper.AbstractColumnDefinitionProvider 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.CaseInsensitiveFieldKeyNamePredicate;
import org.simpleflatmapper.map.FieldKey;
import org.simpleflatmapper.map.property.GetterProperty;
import org.simpleflatmapper.map.property.SetterProperty;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.util.BiConsumer;
import org.simpleflatmapper.util.ConstantUnaryFactory;
import org.simpleflatmapper.util.Predicate;
import org.simpleflatmapper.util.UnaryFactory;

import java.util.ArrayList;
import java.util.List;


public abstract class AbstractColumnDefinitionProvider> implements ColumnDefinitionProvider {

    protected final List> properties;

    public AbstractColumnDefinitionProvider() {
        this(new ArrayList>());
    }
    public AbstractColumnDefinitionProvider(List> properties) {
        this.properties = properties;
    }

    public void addColumnDefinition(Predicate predicate, ColumnDefinition definition) {
        for(Object prop : definition.properties()) {
            addColumnProperty(predicate, newFactory(prop));
        }
    }
    public void addColumnDefinition(String name, ColumnDefinition definition) {
        Predicate predicate = newPredicate(name);
        for(Object prop : definition.properties()) {
            addColumnProperty(predicate, newFactory(prop));
        }
    }

    public void addColumnProperty(String name, Object property) {
        addColumnProperty(newPredicate(name), newFactory(property));
    }

    public void addColumnProperty(Predicate predicate, Object property) {
        addColumnProperty(predicate, newFactory(property));
    }

    public void addColumnProperty(Predicate predicate, UnaryFactory propertyFactory) {
        properties.add(new PredicatedColumnPropertyFactory(predicate, propertyFactory));
    }


    private CaseInsensitiveFieldKeyNamePredicate newPredicate(String name) {
        return CaseInsensitiveFieldKeyNamePredicate.of(name);
    }

    private UnaryFactory newFactory(Object prop) {
        return ConstantUnaryFactory.of(upgrade(prop));
    }

    private Object upgrade(Object property) {
        if (property instanceof Setter) {
            return new SetterProperty((Setter) property);
        }
        if (property instanceof Getter) {
            return new GetterProperty((Getter) property);
        }
        return property;
    }




    @Override
    public ColumnDefinition getColumnDefinition(K key) {
        ColumnDefinition definition = identity();

        for(int i = properties.size() - 1; i >= 0; i--) {
            PredicatedColumnPropertyFactory tuple2 = properties.get(i);
            if (tuple2.predicate.test(key)) {
                Object columnProperty = tuple2.columnPropertyFactory.newInstance(key);
                if (columnProperty != null) {
                    definition = definition.add(columnProperty);
                }
            }
        }

        return  definition;
    }

    public abstract AbstractColumnDefinitionProvider copy();

    protected abstract ColumnDefinition identity();

    public List> getProperties() {
        return properties;
    }

    @Override
    public , CP>> BC forEach(Class propertyType, BC consumer) {
        for (PredicatedColumnPropertyFactory tuple2 : properties) {
            final UnaryFactory unaryFactory = tuple2.columnPropertyFactory;
            if (unaryFactory instanceof ConstantUnaryFactory) {
                final Object columnProperty = unaryFactory.newInstance(null);
                if (propertyType.isInstance(columnProperty)) {
                    consumer.accept(tuple2.predicate, propertyType.cast(columnProperty));
                }
            }
        }
        return consumer;
    }

    public void addColumnProperty(PredicatedColumnPropertyFactory predicatedColumnPropertyFactory) {
        properties.add(predicatedColumnPropertyFactory);
    }

    public static class PredicatedColumnPropertyFactory> {
        private final Predicate predicate;
        private final UnaryFactory columnPropertyFactory;

        public PredicatedColumnPropertyFactory(Predicate predicate, UnaryFactory columnPropertyFactory) {
            this.predicate = predicate;
            this.columnPropertyFactory = columnPropertyFactory;
        }

        public Predicate getPredicate() {
            return predicate;
        }

        public UnaryFactory getColumnPropertyFactory() {
            return columnPropertyFactory;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy