Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize logical nodes visit methods. #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static io.github.perplexhub.rsql.RSQLOperators.*;

import cz.jirutka.rsql.parser.ast.LogicalNode;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -417,14 +419,24 @@ private Predicate equalPredicate(Expression expr, Class type, Object argument) {
@Override
public Predicate visit(AndNode node, From root) {
log.debug("visit(node:{},root:{})", node, root);

return node.getChildren().stream().map(n -> n.accept(this, root)).collect(Collectors.reducing(builder::and)).get();
return visitChildren(node, root, builder::and);
}

@Override
public Predicate visit(OrNode node, From root) {
log.debug("visit(node:{},root:{})", node, root);
return visitChildren(node, root, builder::or);
}

private Predicate visitChildren(LogicalNode node, From root, BiFunction<Predicate, Predicate, Predicate> reducer) {
Predicate result = null;

for (var child : node) {
result = result != null
? reducer.apply(result, child.accept(this, root))
: child.accept(this, root);
}

return node.getChildren().stream().map(n -> n.accept(this, root)).collect(Collectors.reducing(builder::or)).get();
return result;
}
}
Loading