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

cn.cliveyuan.robin.base.condition.Criterion Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * Copyright (c) 2020  Clive Yuan ([email protected]).
 */

package cn.cliveyuan.robin.base.condition;

import cn.cliveyuan.robin.base.util.SqlUtils;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * 条件明细
 *
 * @author Clive Yuan
 * @date 2020/10/29
 */
public class Criterion {

    private final String property;
    private final String condition;
    private final SqlKeyword sqlKeyword;
    private final Object value;
    private Object secondValue;
    private boolean noValue;
    private boolean singleValue;
    private boolean likeValue;
    private boolean betweenValue;
    private boolean listValue;
    private SqlLike sqlLike;

    private Criterion(String property, Object value, SqlKeyword sqlKeyword) {
        Assert.notNull(property, "property is required");
        Assert.notNull(sqlKeyword, "sqlKeyword is required");
        this.property = property;
        this.value = value;
        this.sqlKeyword = sqlKeyword;
        this.condition = parseCondition();
    }

    private String parseCondition() {
        return SqlUtils.contactCondition(property, sqlKeyword);
    }

    protected static Criterion build(String property, Object value, SqlKeyword sqlKeyword) {
        Criterion criterion = new Criterion(property, value, sqlKeyword);
        if (value instanceof List) {
            criterion.listValue = true;
        } else {
            criterion.singleValue = true;
        }
        return criterion;
    }

    protected static Criterion buildNoValue(String property, SqlKeyword sqlKeyword) {
        Criterion criterion = build(property, null, sqlKeyword);
        criterion.noValue = true;
        return criterion;
    }

    protected static Criterion buildLike(String property, Object value, SqlLike sqlLike, SqlKeyword sqlKeyword) {
        Criterion criterion = build(property, value, sqlKeyword);
        criterion.sqlLike = sqlLike;
        criterion.likeValue = true;
        criterion.singleValue = false;
        return criterion;
    }

    protected static Criterion buildBetween(String property, Object value, Object secondValue, SqlKeyword sqlKeyword) {
        Criterion criterion = build(property, value, sqlKeyword);
        criterion.secondValue = secondValue;
        criterion.betweenValue = true;
        criterion.singleValue = false;
        return criterion;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Criterion criterion = (Criterion) o;
        return noValue == criterion.noValue && singleValue == criterion.singleValue && likeValue == criterion.likeValue && betweenValue == criterion.betweenValue && listValue == criterion.listValue && Objects.equals(property, criterion.property) && Objects.equals(condition, criterion.condition) && sqlKeyword == criterion.sqlKeyword && Objects.equals(value, criterion.value) && Objects.equals(secondValue, criterion.secondValue) && sqlLike == criterion.sqlLike;
    }

    @Override
    public int hashCode() {
        return Objects.hash(property, condition, sqlKeyword, value, secondValue, noValue, singleValue, likeValue, betweenValue, listValue, sqlLike);
    }

    public String getProperty() {
        return property;
    }

    public Object getValue() {
        return value;
    }

    public Object getSecondValue() {
        return secondValue;
    }

    public boolean isNoValue() {
        return noValue;
    }

    public boolean isSingleValue() {
        return singleValue;
    }

    public boolean isBetweenValue() {
        return betweenValue;
    }

    public boolean isListValue() {
        return listValue;
    }

    public boolean isLikeValue() {
        return likeValue;
    }

    public SqlLike getSqlLike() {
        return sqlLike;
    }

    public SqlKeyword getSqlKeyword() {
        return sqlKeyword;
    }

    public String getCondition() {
        return condition;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy