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

constructor of predicate #342

Open
HengCC opened this issue Sep 7, 2023 · 3 comments
Open

constructor of predicate #342

HengCC opened this issue Sep 7, 2023 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@HengCC
Copy link

HengCC commented Sep 7, 2023

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

xxx$.NAME_SPACE.eq(NameSpaceEnum.PERSONAL.name())
                .and(xxx$.RELATE_ID.eq(user.getId()))

At compile time, I'm getting the following error

Required type: Predicate <? super String,

Provided: EqualPredicate <xxx,java.lang.Long>
@HengCC
Copy link
Author

HengCC commented Sep 7, 2023

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 (@nameSpace:{space}) (@relateId:{id}).

@HengCC
Copy link
Author

HengCC commented Sep 7, 2023

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());

@bsbodden bsbodden self-assigned this Apr 15, 2024
@bsbodden bsbodden added the question Further information is requested label Apr 15, 2024
@bsbodden
Copy link
Contributor

@HengCC sorry, this one slipped by... Would you be interested in working in a PR with us?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants