com.alilitech.mybatis.jpa.criteria.specification.PredicateBuilder Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alilitech.mybatis.jpa.criteria.specification;
import com.alilitech.mybatis.jpa.criteria.CriteriaBuilder;
import com.alilitech.mybatis.jpa.criteria.CriteriaQuery;
import com.alilitech.mybatis.jpa.criteria.SerializableFunction;
import com.alilitech.mybatis.jpa.criteria.expression.PredicateExpression;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
/**
* @author Zhou Xiaoxiang
* @since 1.1
*/
@SuppressWarnings("java:S1221")
public class PredicateBuilder extends AbstractSpecificationBuilder {
protected PredicateExpression.BooleanOperator operator = PredicateExpression.BooleanOperator.AND;
protected List> predicates = new ArrayList<>();
public PredicateBuilder(SpecificationBuilder specificationBuilder) {
super(specificationBuilder);
}
public PredicateBuilder(SpecificationBuilder specificationBuilder, PredicateExpression.BooleanOperator operator) {
super(specificationBuilder);
this.operator = operator;
}
public PredicateBuilder equal(String property, Object value) {
specifications.add((cb, query) -> cb.equal(property, value));
return this;
}
@SuppressWarnings("java:S1221")
public PredicateBuilder equal(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.equal(property, value));
return this;
}
@SuppressWarnings("java:S1221")
public PredicateBuilder equal(boolean condition, String property, Object value) {
if(condition) {
this.equal(property, value);
}
return this;
}
@SuppressWarnings("java:S1221")
public PredicateBuilder equal(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.equal(property, value);
}
return this;
}
public PredicateBuilder notEqual(String property, Object value) {
specifications.add((cb, query) -> cb.notEqual(property, value));
return this;
}
public PredicateBuilder notEqual(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.notEqual(property, value));
return this;
}
public PredicateBuilder notEqual(boolean condition, String property, Object value) {
if(condition) {
this.notEqual(property, value);
}
return this;
}
public PredicateBuilder notEqual(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.notEqual(property, value);
}
return this;
}
public PredicateBuilder greaterThan(String property, Object value) {
specifications.add((cb, query) -> cb.greaterThan(property, value));
return this;
}
public PredicateBuilder greaterThan(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.greaterThan(property, value));
return this;
}
public PredicateBuilder greaterThan(boolean condition, String property, Object value) {
if(condition) {
this.greaterThan(property, value);
}
return this;
}
public PredicateBuilder greaterThan(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.greaterThan(property, value);
}
return this;
}
public PredicateBuilder greaterThanEqual(String property, Object value) {
specifications.add((cb, query) -> cb.greaterThanEqual(property, value));
return this;
}
public PredicateBuilder greaterThanEqual(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.greaterThanEqual(property, value));
return this;
}
public PredicateBuilder greaterThanEqual(boolean condition, String property, Object value) {
if(condition) {
this.greaterThanEqual(property, value);
}
return this;
}
public PredicateBuilder greaterThanEqual(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.greaterThanEqual(property, value);
}
return this;
}
public PredicateBuilder lessThan(String property, Object value) {
specifications.add((cb, query) -> cb.lessThan(property, value));
return this;
}
public PredicateBuilder lessThan(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.lessThan(property, value));
return this;
}
public PredicateBuilder lessThan(boolean condition, String property, Object value) {
if(condition) {
this.lessThan(property, value);
}
return this;
}
public PredicateBuilder lessThan(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.lessThan(property, value);
}
return this;
}
public PredicateBuilder lessThanEqual(String property, Object value) {
specifications.add((cb, query) -> cb.lessThanEqual(property, value));
return this;
}
public PredicateBuilder lessThanEqual(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.lessThanEqual(property, value));
return this;
}
public PredicateBuilder lessThanEqual(boolean condition, String property, Object value) {
if(condition) {
this.lessThanEqual(property, value);
}
return this;
}
public PredicateBuilder lessThanEqual(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.lessThanEqual(property, value);
}
return this;
}
public PredicateBuilder isNull(String property) {
specifications.add((cb, query) -> cb.isNull(property));
return this;
}
public PredicateBuilder isNull(SerializableFunction property) {
specifications.add((cb, query) -> cb.isNull(property));
return this;
}
public PredicateBuilder isNull(boolean condition, String property) {
if(condition) {
this.isNull(property);
}
return this;
}
public PredicateBuilder isNull(boolean condition, SerializableFunction property) {
if(condition) {
this.isNull(property);
}
return this;
}
public PredicateBuilder isNotNull(String property) {
specifications.add((cb, query) -> cb.isNotNull(property));
return this;
}
public PredicateBuilder isNotNull(SerializableFunction property) {
specifications.add((cb, query) -> cb.isNotNull(property));
return this;
}
public PredicateBuilder isNotNull(boolean condition, String property) {
if(condition) {
this.isNotNull(property);
}
return this;
}
public PredicateBuilder isNotNull(boolean condition, SerializableFunction property) {
if(condition) {
this.isNotNull(property);
}
return this;
}
public PredicateBuilder between(String property, Object value1, Object value2) {
specifications.add((cb, query) -> cb.between(property, value1, value2));
return this;
}
public PredicateBuilder between(SerializableFunction property, Object value1, Object value2) {
specifications.add((cb, query) -> cb.between(property, value1, value2));
return this;
}
public PredicateBuilder between(boolean condition, String property, Object value1, Object value2) {
if(condition) {
this.between(property, value1, value2);
}
return this;
}
public PredicateBuilder between(boolean condition, SerializableFunction property, Object value1, Object value2) {
if(condition) {
this.between(property, value1, value2);
}
return this;
}
public PredicateBuilder notBetween(String property, Object value1, Object value2) {
specifications.add((cb, query) -> cb.notBetween(property, value1, value2));
return this;
}
public PredicateBuilder notBetween(SerializableFunction property, Object value1, Object value2) {
specifications.add((cb, query) -> cb.notBetween(property, value1, value2));
return this;
}
public PredicateBuilder notBetween(boolean condition, String property, Object value1, Object value2) {
if(condition) {
this.notBetween(property, value1, value2);
}
return this;
}
public PredicateBuilder notBetween(boolean condition, SerializableFunction property, Object value1, Object value2) {
if(condition) {
this.notBetween(property, value1, value2);
}
return this;
}
public PredicateBuilder in(String property, Object ...values) {
specifications.add((cb, query) -> cb.in(property, values));
return this;
}
public PredicateBuilder in(SerializableFunction property, Object ...values) {
specifications.add((cb, query) -> cb.in(property, values));
return this;
}
public PredicateBuilder in(boolean condition, String property, Object ...values) {
if(condition) {
this.in(property, values);
}
return this;
}
public PredicateBuilder in(boolean condition, SerializableFunction property, Object ...values) {
if(condition) {
this.in(property, values);
}
return this;
}
public PredicateBuilder in(String property, Collection> values) {
specifications.add((cb, query) -> cb.in(property, values));
return this;
}
public PredicateBuilder in(SerializableFunction property, Collection> values) {
specifications.add((cb, query) -> cb.in(property, values));
return this;
}
public PredicateBuilder in(boolean condition, String property, Collection> values) {
if(condition) {
this.in(property, values);
}
return this;
}
public PredicateBuilder in(boolean condition, SerializableFunction property, Collection> values) {
if(condition) {
this.in(property, values);
}
return this;
}
public PredicateBuilder freeLike(String property, Object value) {
specifications.add((cb, query) -> cb.freeLike(property, value));
return this;
}
public PredicateBuilder freeLike(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.freeLike(property, value));
return this;
}
public PredicateBuilder freeLike(boolean condition, String property, Object value) {
if(condition) {
this.freeLike(property, value);
}
return this;
}
public PredicateBuilder freeLike(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.freeLike(property, value);
}
return this;
}
public PredicateBuilder like(String property, Object value) {
specifications.add((cb, query) -> cb.like(property, value));
return this;
}
public PredicateBuilder like(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.like(property, value));
return this;
}
public PredicateBuilder like(boolean condition, String property, Object value) {
if(condition) {
this.like(property, value);
}
return this;
}
public PredicateBuilder like(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.like(property, value);
}
return this;
}
public PredicateBuilder startsWith(String property, Object value) {
specifications.add((cb, query) -> cb.startsWith(property, value));
return this;
}
public PredicateBuilder startsWith(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.startsWith(property, value));
return this;
}
public PredicateBuilder startsWith(boolean condition, String property, Object value) {
if(condition) {
this.startsWith(property, value);
}
return this;
}
public PredicateBuilder startsWith(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.startsWith(property, value);
}
return this;
}
public PredicateBuilder endsWith(String property, Object value) {
specifications.add((cb, query) -> cb.endsWith(property, value));
return this;
}
public PredicateBuilder endsWith(SerializableFunction property, Object value) {
specifications.add((cb, query) -> cb.endsWith(property, value));
return this;
}
public PredicateBuilder endsWith(boolean condition, String property, Object value) {
if(condition) {
this.endsWith(property, value);
}
return this;
}
public PredicateBuilder endsWith(boolean condition, SerializableFunction property, Object value) {
if(condition) {
this.endsWith(property, value);
}
return this;
}
public PredicateBuilder nested(Consumer> consumer) {
CompoundPredicateBuilder predicateBuilder = new CompoundPredicateBuilder<>();
consumer.accept(predicateBuilder);
specifications.add(predicateBuilder.build());
return this;
}
@Override
public void build(CriteriaBuilder cb, CriteriaQuery query) {
specificationBuilder.build(cb, query);
// 防止重复执行,在查询数量和查询列表的时候这块会重复生成predicates
if(predicates.isEmpty()) {
for (int i = 0; i < specifications.size(); i++) {
PredicateExpression predicateExpression = specifications.get(i).toPredicate(cb, query);
if (predicateExpression != null) {
this.predicates.add(predicateExpression);
}
}
}
if (predicates != null && !predicates.isEmpty()) {
if(PredicateExpression.BooleanOperator.OR.equals(operator)) {
query.where(cb.or(predicates.toArray(new PredicateExpression[predicates.size()])));
} else {
query.where(cb.and(predicates.toArray(new PredicateExpression[predicates.size()])));
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy