All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alilitech.integration.jpa.criteria.CriteriaBuilder 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;

import com.alilitech.integration.jpa.criteria.expression.*;
import com.alilitech.integration.jpa.criteria.expression.operator.OperatorExpression;
import com.alilitech.integration.jpa.criteria.expression.operator.comparison.*;
import com.alilitech.integration.jpa.criteria.expression.operator.like.*;
import com.alilitech.integration.jpa.domain.Direction;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zhou Xiaoxiang
 * @since 1.1
 */
public class CriteriaBuilder {

    private Class domainClass;

    public CriteriaBuilder(Class domainClass) {
        this.domainClass = domainClass;
    }

    public PredicateExpression and(PredicateExpression ...predicates) {
        return new CompoundPredicateExpression<>(PredicateExpression.BooleanOperator.AND, predicates);
    }

    public PredicateExpression or(PredicateExpression ...predicates) {
        return new CompoundPredicateExpression<>(PredicateExpression.BooleanOperator.OR, predicates);
    }

    public PredicateExpression equal(String property, Object value) {
        return this.buildPredicate(property, new EqualExpression<>(), value);
    }

    public PredicateExpression notEqual(String property, Object value) {
        return this.buildPredicate(property, new NotEqualExpression<>(), value);
    }

    public PredicateExpression greaterThan(String property, Object value) {
        return this.buildPredicate(property, new GreaterThanExpression<>(), value);
    }

    public PredicateExpression greaterThanEqual(String property, Object value) {
        return this.buildPredicate(property, new GreaterThanEqualExpression<>(), value);
    }

    public PredicateExpression lessThan(String property, Object value) {
        return this.buildPredicate(property, new LessThanExpression<>(), value);
    }

    public PredicateExpression lessThanEqual(String property, Object value) {
        return this.buildPredicate(property, new LessThanEqualExpression<>(), value);
    }

    public PredicateExpression isNull(String property) {
        return this.buildPredicate(property, new IsNullExpression<>());
    }

    public PredicateExpression isNotNull(String property) {
        return this.buildPredicate(property, new IsNotNullExpression<>());
    }

    public PredicateExpression between(String property, Object value1, Object value2) {
        return this.buildPredicate(property, new BetweenExpression<>(), value1, value2);
    }

    public PredicateExpression notBetween(String property, Object value1, Object value2) {
        return this.buildPredicate(property, new NotBetweenExpression<>(), value1, value2);
    }

    public PredicateExpression in(String property, Object ...values) {
        return this.buildPredicate(property, new InExpression<>(), values);
    }

    public PredicateExpression in(String property, List values) {
        return this.buildPredicate(property, new InExpression<>(), values.toArray());
    }

    public PredicateExpression freeLike(String property, Object value) {
        return this.buildPredicate(property, new FreeLikeExpression<>(), value);
    }

    public PredicateExpression notFreeLike(String property, Object value) {
        return this.buildPredicate(property, new NotFreeLikeExpression<>(), value);
    }

    public PredicateExpression like(String property, Object value) {
        return this.buildPredicate(property, new LikeExpression<>(), value);
    }

    public PredicateExpression notLike(String property, Object value) {
        return this.buildPredicate(property, new NotLikeExpression<>(), value);
    }

    public PredicateExpression startsWith(String property, Object value) {
        return this.buildPredicate(property, new StartsWithExpression<>(), value);
    }

    public PredicateExpression endsWith(String property, Object value) {
        return this.buildPredicate(property, new EndsWithExpression<>(), value);
    }

    private PredicateExpression buildPredicate(String property, OperatorExpression operator, Object ...values) {
        VariableExpression variable = new VariableExpression(domainClass, property);
        if(values != null && values.length > 0) {
            List> parameters = Arrays.stream(values).map(value -> new ParameterExpression(value)).collect(Collectors.toList());
            return new SinglePredicateExpression<>(variable, operator, parameters);
        } else {
            return new SinglePredicateExpression<>(variable, operator);
        }
    }

    private PredicateExpression buildPredicate(VariableExpression variable, OperatorExpression operator, ParameterExpression ...parameters) {
        return new SinglePredicateExpression<>(variable, operator, parameters);
    }

    public OrderExpression desc(String property) {
        VariableExpression variable = new VariableExpression(domainClass, property);
        return new OrderExpression<>(variable, Direction.DESC);
    }

    public OrderExpression asc(String property) {
        VariableExpression variable = new VariableExpression(domainClass, property);
        return new OrderExpression<>(variable, Direction.ASC);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy