
com.github.rutledgepaulv.qbuilders.visitors.PredicateVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of q-builders Show documentation
Show all versions of q-builders Show documentation
A type-safe and database agnostic query building library.
package com.github.rutledgepaulv.qbuilders.visitors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.rutledgepaulv.qbuilders.nodes.AndNode;
import com.github.rutledgepaulv.qbuilders.nodes.ComparisonNode;
import com.github.rutledgepaulv.qbuilders.nodes.OrNode;
import com.github.rutledgepaulv.qbuilders.operators.ComparisonOperator;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.BiFunction;
import java.util.function.Predicate;
@SuppressWarnings("WeakerAccess")
public class PredicateVisitor extends AbstractVoidContextNodeVisitor> {
protected static final ObjectMapper mapper = new ObjectMapper();
@Override
protected Predicate visit(AndNode node) {
return (t) -> node.getChildren().stream().map(this::visitAny).allMatch(p -> p.test(t));
}
@Override
protected Predicate visit(OrNode node) {
return (t) -> node.getChildren().stream().map(this::visitAny).anyMatch(p -> p.test(t));
}
@Override
protected Predicate visit(ComparisonNode node) {
ComparisonOperator operator = node.getOperator();
if(ComparisonOperator.EQ.equals(operator)) {
return single(node, this::equality);
} else if(ComparisonOperator.NE.equals(operator)) {
return single(node, this::inequality);
} else if (ComparisonOperator.EX.equals(operator)) {
return ((Boolean)node.getValues().iterator().next()) ? exists(node) : doesNotExist(node);
} else if (ComparisonOperator.GT.equals(operator)) {
return single(node, this::greaterThan);
} else if (ComparisonOperator.LT.equals(operator)) {
return single(node, this::lessThan);
} else if (ComparisonOperator.GTE.equals(operator)) {
return single(node, this::greaterThanOrEqualTo);
} else if (ComparisonOperator.LTE.equals(operator)) {
return single(node, this::lessThanOrEqualTo);
} else if (ComparisonOperator.IN.equals(operator)) {
return multi(node, this::in);
} else if (ComparisonOperator.NIN.equals(operator)) {
return multi(node, this::nin);
} else if (ComparisonOperator.SUB_CONDITION_ANY.equals(operator)) {
Predicate test = condition(node);
// subquery condition is ignored because a predicate has already been built.
return single(node, (fieldValue, subQueryCondition) -> this.subquery(fieldValue, test));
}
throw new UnsupportedOperationException("This visitor does not support the operator " + operator + ".");
}
protected boolean subquery(Object actual, Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy