io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-model Show documentation
Show all versions of micronaut-data-model Show documentation
Data Repository Support for Micronaut
The newest version!
/*
* Copyright 2017-2021 original 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
*
* https://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 io.micronaut.data.model.jpa.criteria.impl;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.data.model.jpa.criteria.IExpression;
import io.micronaut.data.model.jpa.criteria.PersistentEntityRoot;
import io.micronaut.data.model.jpa.criteria.PersistentEntitySubquery;
import io.micronaut.data.model.jpa.criteria.PersistentPropertyPath;
import io.micronaut.data.model.jpa.criteria.impl.expression.LiteralExpression;
import io.micronaut.data.model.jpa.criteria.impl.predicate.ConjunctionPredicate;
import io.micronaut.data.model.jpa.criteria.impl.predicate.DisjunctionPredicate;
import io.micronaut.data.model.jpa.criteria.impl.predicate.BinaryPredicate;
import io.micronaut.data.model.jpa.criteria.impl.predicate.InPredicate;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Subquery;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* Criteria util class.
*
* @author Denis Stepanov
* @since 3.2
*/
@Internal
public final class CriteriaUtils {
private CriteriaUtils() {
}
public static boolean isNumeric(@NonNull Class> clazz) {
if (clazz.isPrimitive()) {
return Number.class.isAssignableFrom(ReflectionUtils.getPrimitiveType(clazz));
}
return Number.class.isAssignableFrom(clazz);
}
public static boolean isBoolean(@NonNull Class> clazz) {
return Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz);
}
public static boolean isComparable(@NonNull Class> clazz) {
return Comparable.class.isAssignableFrom(clazz) || isNumeric(clazz) ;
}
public static boolean isTextual(@NonNull Class> clazz) {
return CharSequence.class.isAssignableFrom(clazz);
}
public static List> requireBoolExpressions(Iterable extends Expression>> restrictions) {
return CollectionUtils.iterableToList(restrictions).stream().map(CriteriaUtils::requireBoolExpression).toList();
}
public static IExpression requireIExpression(Expression exp) {
if (exp instanceof IExpression expression) {
return expression;
}
throw new IllegalStateException("Expected an IExpression! Got: " + exp);
}
public static PersistentEntitySubquery requirePersistentEntitySubquery(Subquery subquery) {
if (subquery instanceof PersistentEntitySubquery persistentEntitySubquery) {
return persistentEntitySubquery;
}
throw new IllegalStateException("Expected an PersistentEntitySubquery! Got: " + subquery);
}
public static IExpression requireNumericExpression(Expression> exp) {
IExpression expression = requireIExpression(exp);
if (expression.getExpressionType().isNumeric()) {
return expression;
}
throw new IllegalStateException("Expected a numeric expression! Got: " + expression.getExpressionType().getName());
}
public static IExpression requireStringExpression(Expression> exp) {
IExpression expression = requireIExpression(exp);
if (expression.getExpressionType().isTextual()) {
return expression;
}
throw new IllegalStateException("Expected a string expression! Got: " + expression.getExpressionType().getName());
}
public static Expression requireComparableExpression(Expression exp) {
IExpression expression = requireIExpression(exp);
if (expression.getExpressionType().isComparable()) {
return expression;
}
throw new IllegalStateException("Expected a comparable expression! Got: " + expression.getExpressionType().getName());
}
public static IExpression requireBoolExpression(Expression> exp) {
IExpression expression = requireIExpression(exp);
if (expression.getExpressionType().isBoolean()) {
return expression;
}
throw new IllegalStateException("Expected a boolean expression! Got: " + expression.getExpressionType().getName());
}
public static ParameterExpression requireParameter(Expression exp) {
if (exp instanceof ParameterExpression parameterExpression) {
return parameterExpression;
}
throw new IllegalStateException("Expression is expected to be a parameter! Got: " + exp);
}
public static PersistentPropertyPath requireProperty(Expression extends T> exp) {
if (exp instanceof PersistentPropertyPath persistentPropertyPath) {
return persistentPropertyPath;
}
throw new IllegalStateException("Expression is expected to be a property path! Got: " + exp);
}
public static IExpression requirePropertyOrRoot(Expression exp) {
if (exp instanceof PersistentPropertyPath || exp instanceof PersistentEntityRoot) {
return (IExpression) exp;
}
throw new IllegalStateException("Expression is expected to be a property path or a root! Got: " + exp);
}
public static IllegalStateException notSupportedOperation() {
return new IllegalStateException("Not supported operation!");
}
public static boolean hasVersionPredicate(Expression> predicate) {
if (predicate instanceof BinaryPredicate binaryPredicate) {
if (binaryPredicate.getLeftExpression() instanceof PersistentPropertyPath> pp &&
pp.getProperty() == pp.getProperty().getOwner().getVersion()) {
return true;
}
if (binaryPredicate.getRightExpression() instanceof PersistentPropertyPath> pp &&
pp.getProperty() == pp.getProperty().getOwner().getVersion()) {
return true;
}
}
if (predicate instanceof ConjunctionPredicate conjunctionPredicate) {
for (IExpression pred : conjunctionPredicate.getPredicates()) {
if (hasVersionPredicate(pred)) {
return true;
}
}
}
if (predicate instanceof DisjunctionPredicate disjunctionPredicate) {
for (IExpression pred : disjunctionPredicate.getPredicates()) {
if (hasVersionPredicate(pred)) {
return true;
}
}
}
return false;
}
public static Set> extractPredicateParameters(Expression> predicate) {
if (predicate == null) {
return Collections.emptySet();
}
Set> properties = new LinkedHashSet<>();
extractPredicateParameters(predicate, properties);
return properties;
}
private static void extractPredicateParameters(Expression> predicate, Set> parameters) {
if (predicate instanceof LiteralExpression>) {
return;
} else if (predicate instanceof BinaryPredicate binaryPredicate) {
if (binaryPredicate.getLeftExpression() instanceof ParameterExpression> parameterExpression) {
parameters.add(parameterExpression);
}
if (binaryPredicate.getRightExpression() instanceof ParameterExpression> parameterExpression) {
parameters.add(parameterExpression);
}
} else if (predicate instanceof InPredicate> pp) {
for (Expression> expression : pp.getValues()) {
if (expression instanceof ParameterExpression> parameterExpression) {
parameters.add(parameterExpression);
}
}
} else if (predicate instanceof ConjunctionPredicate conjunctionPredicate) {
for (IExpression pred : conjunctionPredicate.getPredicates()) {
extractPredicateParameters(pred, parameters);
}
} else if (predicate instanceof DisjunctionPredicate disjunctionPredicate) {
for (IExpression pred : disjunctionPredicate.getPredicates()) {
extractPredicateParameters(pred, parameters);
}
} else {
throw new IllegalStateException("Unsupported predicate type: " + predicate.getClass().getSimpleName());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy