co.streamx.fluent.JPA.spi.impl.SQLConfiguratorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-jpa Show documentation
Show all versions of fluent-jpa Show documentation
Fluent query implementation for JPA repositories
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 super T1, ? super T2, ? super T3, ? extends R> to) {
registerMethodSubstitution0(from, to);
}
@Override
public void registerMethodSubstitution(Function2 from,
Function2 super T, ? super U, ? extends R> to) {
registerMethodSubstitution0(from, to);
}
@Override
public void registerMethodSubstitution(Function1 from,
Function1 super T, ? extends R> to) {
registerMethodSubstitution0(from, to);
}
@Override
public void registerMethodSubstitution(Function0 from,
Function0 extends R> 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;
}
}