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

co.streamx.fluent.JPA.spi.impl.SQLConfiguratorImpl Maven / Gradle / Ivy

The newest version!
package co.streamx.fluent.JPA.spi.impl;

import java.lang.reflect.Executable;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import co.streamx.fluent.JPA.spi.SQLConfigurator;
import co.streamx.fluent.extree.expression.Expression;
import co.streamx.fluent.extree.expression.InvocationExpression;
import co.streamx.fluent.extree.expression.LambdaExpression;
import co.streamx.fluent.extree.expression.MemberExpression;
import co.streamx.fluent.extree.expression.UnaryExpression;
import co.streamx.fluent.functions.Function0;
import co.streamx.fluent.functions.Function1;
import co.streamx.fluent.functions.Function2;
import co.streamx.fluent.functions.Function3;
import lombok.Getter;

public final class SQLConfiguratorImpl implements SQLConfigurator {

    private final static Map>> substitutions_ = new ConcurrentHashMap<>();

    @Getter
    private final static Map>> substitutions = Collections
            .unmodifiableMap(substitutions_);

    @Override
    public  void registerMethodSubstitution(Function3 from,
                                                           Function3 to) {

        registerMethodSubstitution0(from, to);

    }

    @Override
    public  void registerMethodSubstitution(Function2 from,
                                                     Function2 to) {

        registerMethodSubstitution0(from, to);

    }

    @Override
    public  void registerMethodSubstitution(Function1 from,
                                                  Function1 to) {

        registerMethodSubstitution0(from, to);

    }

    @Override
    public  void registerMethodSubstitution(Function0 from,
                                               Function0 to) {

        registerMethodSubstitution0(from, to);

    }

    @Override
    public  boolean unregisterMethodSubstitution(Function3 from) {
        return unregisterMethodSubstitution0(from);
    }

    @Override
    public  boolean unregisterMethodSubstitution(Function2 from) {
        return unregisterMethodSubstitution0(from);
    }

    @Override
    public  boolean unregisterMethodSubstitution(Function1 from) {
        return unregisterMethodSubstitution0(from);
    }

    @Override
    public  boolean unregisterMethodSubstitution(Function0 from) {
        return unregisterMethodSubstitution0(from);
    }

    private static  void registerMethodSubstitution0(T from,
                                                        T to) {

        LambdaExpression parsedFrom = LambdaExpression.parse(from);
        LambdaExpression parsedTo = LambdaExpression.parse(to);

        Expression arg = parsedFrom.getBody();
        while (arg instanceof UnaryExpression)
            arg = ((UnaryExpression) arg).getFirst();
        InvocationExpression ie = (InvocationExpression) arg;
        MemberExpression me = (MemberExpression) ie.getTarget();
        Executable m = (Executable) me.getMember();

        Map> subByName = substitutions_.computeIfAbsent(m.getName(),
                key -> new ConcurrentHashMap<>());

        subByName.put(m, parsedTo);
    }

    private static  boolean unregisterMethodSubstitution0(T from) {

        LambdaExpression parsedFrom = LambdaExpression.parse(from);

        InvocationExpression ie = (InvocationExpression) parsedFrom.getBody();
        MemberExpression me = (MemberExpression) ie.getTarget();
        Executable m = (Executable) me.getMember();

        Map> subByName = substitutions_.get(m.getName());

        if (subByName == null)
            return false;

        return subByName.remove(m) != null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy