-
Notifications
You must be signed in to change notification settings - Fork 94
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
constructor of predicate #342
Comments
In Java 8, predicates naturally come with generic constraints. However, in real-world queries, combining conditions of two different types should be a normal business scenario, as in the example above. It can be written as |
If 'and' and 'or' don't return Predicate types but instead assemble into a string query expression like the following, is it feasible? public interface QueryCondition {
String get();
default QueryCondition and(QueryCondition otherCondition) {
return () -> {
return this.get() + " " + otherCondition.get();
};
}
default QueryCondition or(QueryCondition otherCondition) {
return () -> {
return "(" + this.get() + ")" + "| " + "(" + otherCondition.get() + ")";
};
}
} then use it, QueryCondition queryCondition = () -> xxx$.CONTENT.containing(keyword).apply(QueryBuilders.union(new Node[0])).toString(Node.Parenthesize.NEVER);
queryCondition = queryCondition.or(() -> xxx$.NAME.startsWith(keyword).apply(QueryBuilders.union(new Node[0])).toString(Node.Parenthesize.NEVER))
.or(() -> xxx$.NAME.endsWith(keyword).apply(QueryBuilders.union(new Node[0])).toString(Node.Parenthesize.NEVER))
.or(() -> xxx$.NAME.containing(keyword).apply(QueryBuilders.union(new Node[0])).toString(Node.Parenthesize.NEVER))
.and(() -> xxx$.NAME_SPACE.eq("namespace").apply(QueryBuilders.union(new Node[0])).toString(Node.Parenthesize.NEVER));
long total = entityStream.of(xxx.class).filter(predicate).count();
List<xxx> collect = entityStream.of(xxx.class)
//.filter("(@name:" + keyword + ")|(@content:" + keyword + ")")
.filter(queryCondition.get())
//.filter(predicate)
.skip(pageable.getOffset())
.limit(pageable.getPageSize())
.collect(Collectors.toList()); |
@HengCC sorry, this one slipped by... Would you be interested in working in a PR with us? |
How can I construct combinations of predicates of different types, such as 'and' and 'or'? Currently, writing something like this results in a generic type incompatibility error. Do I need to specify the generic type within these constructs
At compile time, I'm getting the following error
The text was updated successfully, but these errors were encountered: