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

io.github.holmofy.data.jpa.MoreCriteria Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package io.github.holmofy.data.jpa;

import jakarta.persistence.metamodel.SingularAttribute;
import lombok.experimental.UtilityClass;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;

import java.util.Collection;

@UtilityClass
public class MoreCriteria {

    public static  Specification alwaysTrue() {
        return (root, query, cb) -> cb.isTrue(cb.literal(Boolean.TRUE));
    }

    public static  Specification alwaysFalse() {
        return (root, query, cb) -> cb.isTrue(cb.literal(Boolean.FALSE));
    }

    public static  Specification isNull(SingularAttribute attr) {
        return (root, query, cb) -> cb.isNull(root.get(attr));
    }

    public static  Specification isNotNull(SingularAttribute attr) {
        return (root, query, cb) -> cb.isNotNull(root.get(attr));
    }

    @Nullable
    public static  Specification eq(SingularAttribute attr, Object value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.equal(root.get(attr), value);
    }

    @Nullable
    public static  Specification notEq(SingularAttribute attr, Object value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.notEqual(root.get(attr), value);
    }

    @Nullable
    public static > Specification lt(SingularAttribute attr, Y value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.lessThan(root.get(attr), value);
    }

    @Nullable
    public static > Specification lte(SingularAttribute attr, Y value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.lessThanOrEqualTo(root.get(attr), value);
    }

    @Nullable
    public static > Specification gt(SingularAttribute attr, Y value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.greaterThan(root.get(attr), value);
    }

    @Nullable
    public static > Specification gte(SingularAttribute attr, Y value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.greaterThanOrEqualTo(root.get(attr), value);
    }

    @Nullable
    public static > Specification between(SingularAttribute attr, Y begin, Y end) {
        if (begin == null && end == null) {
            return null;
        }
        if (begin != null && end != null) {
            return (root, query, cb) -> cb.between(root.get(attr), begin, end);
        }
        return begin == null ? lte(attr, end) : gte(attr, begin);
    }

    @Nullable
    public static  Specification like(SingularAttribute attr, String value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.like(root.get(attr), value);
    }

    @Nullable
    public static  Specification startWith(SingularAttribute attr, String value) {
        return isNullOrEmpty(value) ? null : (root, query, cb) -> cb.like(root.get(attr), escapeLikeClause(value) + "%");
    }

    @Nullable
    public static  Specification in(SingularAttribute attr, Collection values) {
        return isNullOrEmpty(values) ? null : (root, query, cb) -> cb.isTrue(root.get(attr).in(values));
    }

    @Nullable
    public static  Specification notIn(SingularAttribute attr, Collection values) {
        return isNullOrEmpty(values) ? null : (root, query, cb) -> cb.isTrue(root.get(attr).in(values).not());
    }

    public static boolean isNullOrEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNullOrEmpty(Object obj) {
        return obj == null || obj instanceof String && ((String) obj).isEmpty();
    }

    private static String escapeLikeClause(CharSequence expression) {
        return expression.toString().replace("%", "\\%").replace("_", "\\_");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy