Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alilitech.integration.jpa.criteria.specification.PredicateBuilder Maven / Gradle / Ivy
/**
* Copyright 2017-2020 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.integration.jpa.criteria.specification;
import com.alilitech.integration.jpa.criteria.CriteriaBuilder;
import com.alilitech.integration.jpa.criteria.CriteriaQuery;
import com.alilitech.integration.jpa.criteria.Specification;
import com.alilitech.integration.jpa.criteria.expression.PredicateExpression;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
* @author Zhou Xiaoxiang
* @since 1.1
*/
public class PredicateBuilder extends AbstractSpecificationBuilder {
protected List> specifications = new ArrayList<>();
protected PredicateExpression.BooleanOperator operator;
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;
}
public PredicateBuilder equal(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.lessThanEqual(property, value));
}
return this;
}
public PredicateBuilder isNull(String property) {
specifications.add((cb, query) -> cb.isNull(property));
return this;
}
public PredicateBuilder isNull(boolean condition, String property) {
if(condition) {
specifications.add((cb, query) -> cb.isNull(property));
}
return this;
}
public PredicateBuilder isNotNull(String property) {
specifications.add((cb, query) -> cb.isNotNull(property));
return this;
}
public PredicateBuilder isNotNull(boolean condition, String property) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value1, Object value2) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value1, Object value2) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object ...values) {
if(condition) {
specifications.add((cb, query) -> cb.in(property, values));
}
return this;
}
public PredicateBuilder in(String property, List> values) {
specifications.add((cb, query) -> cb.in(property, values));
return this;
}
public PredicateBuilder in(boolean condition, String property, List> values) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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(boolean condition, String property, Object value) {
if(condition) {
specifications.add((cb, query) -> cb.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) {
for (int i = 0; i < specifications.size(); i++) {
PredicateExpression predicateExpression = specifications.get(i).toPredicate(cb, query);
if(predicateExpression != null) {
this.predicates.add(predicateExpression);
}
}
if (!CollectionUtils.isEmpty(predicates)) {
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()])));
}
}
}
}