From 65b9b437565ba3f8494297962729276a16103951 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Tue, 12 Nov 2024 23:11:51 +0100 Subject: [PATCH] add Order.reversedIf() and ignoringCaseIf() to simplify generated code --- .../main/java/org/hibernate/query/Order.java | 88 +++++++++++++------ .../annotation/AbstractQueryMethod.java | 50 ++++++----- 2 files changed, 86 insertions(+), 52 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/Order.java b/hibernate-core/src/main/java/org/hibernate/query/Order.java index da5804fcee..be7c6a1b56 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/Order.java +++ b/hibernate-core/src/main/java/org/hibernate/query/Order.java @@ -4,6 +4,7 @@ */ package org.hibernate.query; +import jakarta.persistence.criteria.Nulls; import jakarta.persistence.metamodel.SingularAttribute; import org.hibernate.Incubating; @@ -42,11 +43,11 @@ public class Order { private final SingularAttribute attribute; private final Class entityClass; private final String attributeName; - private final NullPrecedence nullPrecedence; + private final Nulls nullPrecedence; private final int element; private final boolean ignoreCase; - private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttribute attribute) { + private Order(SortDirection order, Nulls nullPrecedence, SingularAttribute attribute) { this.order = order; this.attribute = attribute; this.attributeName = attribute.getName(); @@ -56,7 +57,7 @@ public class Order { this.ignoreCase = false; } - private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttribute attribute, boolean ignoreCase) { + private Order(SortDirection order, Nulls nullPrecedence, SingularAttribute attribute, boolean ignoreCase) { this.order = order; this.attribute = attribute; this.attributeName = attribute.getName(); @@ -66,7 +67,7 @@ public class Order { this.ignoreCase = ignoreCase; } - private Order(SortDirection order, NullPrecedence nullPrecedence, Class entityClass, String attributeName) { + private Order(SortDirection order, Nulls nullPrecedence, Class entityClass, String attributeName) { this.order = order; this.entityClass = entityClass; this.attributeName = attributeName; @@ -76,7 +77,7 @@ public class Order { this.ignoreCase = false; } - private Order(SortDirection order, NullPrecedence nullPrecedence, int element) { + private Order(SortDirection order, Nulls nullPrecedence, int element) { this.order = order; this.entityClass = null; this.attributeName = null; @@ -86,7 +87,7 @@ public class Order { this.ignoreCase = false; } - private Order(SortDirection order, NullPrecedence nullPrecedence, Class entityClass, String attributeName, boolean ignoreCase) { + private Order(SortDirection order, Nulls nullPrecedence, Class entityClass, String attributeName, boolean ignoreCase) { this.order = order; this.entityClass = entityClass; this.attributeName = attributeName; @@ -96,7 +97,7 @@ public class Order { this.ignoreCase = ignoreCase; } - private Order(SortDirection order, NullPrecedence nullPrecedence, int element, boolean ignoreCase) { + private Order(SortDirection order, Nulls nullPrecedence, int element, boolean ignoreCase) { this.order = order; this.entityClass = null; this.attributeName = null; @@ -126,7 +127,7 @@ public class Order { this.ignoreCase = ignoreCase; } - private Order(Order other, NullPrecedence nullPrecedence) { + private Order(Order other, Nulls nullPrecedence) { this.order = other.order; this.attribute = other.attribute; this.entityClass = other.entityClass; @@ -142,7 +143,7 @@ public class Order { * type, the ordering is case-sensitive. */ public static Order asc(SingularAttribute attribute) { - return new Order<>(ASCENDING, NullPrecedence.NONE, attribute); + return new Order<>(ASCENDING, Nulls.NONE, attribute); } /** @@ -151,7 +152,7 @@ public class Order { * type, the ordering is case-sensitive. */ public static Order desc(SingularAttribute attribute) { - return new Order<>(DESCENDING, NullPrecedence.NONE, attribute); + return new Order<>(DESCENDING, Nulls.NONE, attribute); } /** @@ -160,7 +161,7 @@ public class Order { * type, the ordering is case-sensitive. */ public static Order by(SingularAttribute attribute, SortDirection direction) { - return new Order<>(direction, NullPrecedence.NONE, attribute); + return new Order<>(direction, Nulls.NONE, attribute); } /** @@ -168,7 +169,7 @@ public class Order { * in the given direction, with the specified case-sensitivity. */ public static Order by(SingularAttribute attribute, SortDirection direction, boolean ignoreCase) { - return new Order<>(direction, NullPrecedence.NONE, attribute, ignoreCase); + return new Order<>(direction, Nulls.NONE, attribute, ignoreCase); } /** @@ -177,7 +178,7 @@ public class Order { * null values. If the give attribute is of textual type, the * ordering is case-sensitive. */ - public static Order by(SingularAttribute attribute, SortDirection direction, NullPrecedence nullPrecedence) { + public static Order by(SingularAttribute attribute, SortDirection direction, Nulls nullPrecedence) { return new Order<>(direction, nullPrecedence, attribute); } @@ -188,7 +189,7 @@ public class Order { * case-sensitive. */ public static Order asc(Class entityClass, String attributeName) { - return new Order<>( ASCENDING, NullPrecedence.NONE, entityClass, attributeName ); + return new Order<>( ASCENDING, Nulls.NONE, entityClass, attributeName ); } /** @@ -198,7 +199,7 @@ public class Order { * case-sensitive. */ public static Order desc(Class entityClass, String attributeName) { - return new Order<>( DESCENDING, NullPrecedence.NONE, entityClass, attributeName ); + return new Order<>( DESCENDING, Nulls.NONE, entityClass, attributeName ); } /** @@ -208,7 +209,7 @@ public class Order { * case-sensitive. */ public static Order by(Class entityClass, String attributeName, SortDirection direction) { - return new Order<>( direction, NullPrecedence.NONE, entityClass, attributeName ); + return new Order<>( direction, Nulls.NONE, entityClass, attributeName ); } /** @@ -217,7 +218,7 @@ public class Order { * the specified case-sensitivity. */ public static Order by(Class entityClass, String attributeName, SortDirection direction, boolean ignoreCase) { - return new Order<>( direction, NullPrecedence.NONE, entityClass, attributeName, ignoreCase ); + return new Order<>( direction, Nulls.NONE, entityClass, attributeName, ignoreCase ); } /** @@ -227,7 +228,7 @@ public class Order { * precedence for null values. If the named attribute is of * textual type, the ordering is case-sensitive. */ - public static Order by(Class entityClass, String attributeName, SortDirection direction, NullPrecedence nullPrecedence) { + public static Order by(Class entityClass, String attributeName, SortDirection direction, Nulls nullPrecedence) { return new Order<>( direction, nullPrecedence, entityClass, attributeName ); } @@ -237,7 +238,7 @@ public class Order { * item is of textual type, the ordering is case-sensitive. */ public static Order asc(int element) { - return new Order<>( ASCENDING, NullPrecedence.NONE, element ); + return new Order<>( ASCENDING, Nulls.NONE, element ); } /** @@ -246,7 +247,7 @@ public class Order { * item is of textual type, the ordering is case-sensitive. */ public static Order desc(int element) { - return new Order<>( DESCENDING, NullPrecedence.NONE, element ); + return new Order<>( DESCENDING, Nulls.NONE, element ); } /** @@ -255,7 +256,7 @@ public class Order { * is of textual type, the ordering is case-sensitive. */ public static Order by(int element, SortDirection direction) { - return new Order<>( direction, NullPrecedence.NONE, element ); + return new Order<>( direction, Nulls.NONE, element ); } /** @@ -264,7 +265,7 @@ public class Order { * case-sensitivity. */ public static Order by(int element, SortDirection direction, boolean ignoreCase) { - return new Order<>( direction, NullPrecedence.NONE, element, ignoreCase ); + return new Order<>( direction, Nulls.NONE, element, ignoreCase ); } /** @@ -273,7 +274,7 @@ public class Order { * precedence for null values. If the named attribute is of * textual type, the ordering is case-sensitive. */ - public static Order by(int element, SortDirection direction, NullPrecedence nullPrecedence) { + public static Order by(int element, SortDirection direction, Nulls nullPrecedence) { return new Order<>( direction, nullPrecedence, element ); } @@ -281,7 +282,7 @@ public class Order { return order; } - public NullPrecedence getNullPrecedence() { + public Nulls getNullPrecedence() { return nullPrecedence; } @@ -326,7 +327,7 @@ public class Order { * @since 6.5 */ public Order withNullsFirst() { - return new Order<>( this, NullPrecedence.FIRST ); + return new Order<>( this, Nulls.FIRST ); } /** @@ -334,7 +335,37 @@ public class Order { * @since 6.5 */ public Order withNullsLast() { - return new Order<>( this, NullPrecedence.LAST ); + return new Order<>( this, Nulls.LAST ); + } + + /** + * An order based on this order, possibly reversed. + * + * @param reverse {@code true} if the returned order should be + * {@linkplain #reverse reversed} + * @return this order, but reversed if the argument is {@code true} + * + * @apiNote This is a convenience for use with Jakarta Data + * + * @since 7.0 + */ + public Order reversedIf(boolean reverse) { + return reverse ? reverse() : this; + } + + /** + * An order based on this order, possibly without case-sensitivity. + * + * @param ignoreCase {@code true} if this order should be + * {@linkplain #ignoringCase ignore case} + * @return this order, but ignoring case if the argument is {@code true} + * + * @apiNote This is a convenience for use with Jakarta Data + * + * @since 7.0 + */ + public Order ignoringCaseIf(boolean ignoreCase) { + return ignoreCase ? ignoringCase() : this; } @Override @@ -343,9 +374,8 @@ public class Order { } @Override - public boolean equals(Object o) { - if ( o instanceof Order) { - Order that = (Order) o; + public boolean equals(Object object) { + if ( object instanceof Order that) { return that.order == this.order && that.nullPrecedence == this.nullPrecedence && that.ignoreCase == this.ignoreCase diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java index b45cd1caf2..2b15c136ac 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java @@ -500,16 +500,18 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod { annotationMetaEntity.staticImport(HIB_SORT_DIRECTION, "*"); declaration .append("\t_orders.add(") - .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) + .append(annotationMetaEntity.staticImport(HIB_ORDER, orderBy.descending ? "desc" : "asc")) .append('(') .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, \"") .append(orderBy.fieldName) - .append("\", ") - .append(orderBy.descending ? "DESCENDING" : "ASCENDING") - .append(", ") - .append(orderBy.ignoreCase) - .append("));\n"); + .append("\")"); + if ( orderBy.ignoreCase ) { + declaration + .append("\n\t.ignoringCase()"); + } + declaration + .append(");\n"); } for (int i = 0; i < paramTypes.size(); i++) { @@ -542,14 +544,14 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod { .append(name) .append(".sorts()) {\n") .append("\t\t_orders.add(") - .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) + .append(annotationMetaEntity.staticImport(HIB_ORDER, "asc")) .append('(') .append(annotationMetaEntity.importType(returnTypeName)) - .append(".class, _sort.property(),") - .append("\n\t\t\t\t\t\t") - .append("_sort.isAscending() ? ASCENDING : DESCENDING,") - .append("\n\t\t\t\t\t\t") - .append("_sort.ignoreCase()));\n") + .append(".class, _sort.property())") + .append("\n\t\t\t\t\t") + .append(".reversedIf(_sort.isDescending())") + .append("\n\t\t\t\t\t") + .append(".ignoringCaseIf(_sort.ignoreCase()));\n") .append("\t}\n"); } else if ( type.startsWith(JD_SORT) && type.endsWith("...") ) { @@ -560,30 +562,32 @@ public abstract class AbstractQueryMethod extends AbstractAnnotatedMethod { .append(name) .append(") {\n") .append("\t\t_orders.add(") - .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) + .append(annotationMetaEntity.staticImport(HIB_ORDER, "asc")) .append('(') .append(annotationMetaEntity.importType(returnTypeName)) - .append(".class, _sort.property(),") - .append("\n\t\t\t\t\t\t") - .append("_sort.isAscending() ? ASCENDING : DESCENDING,") - .append("\n\t\t\t\t\t\t") - .append("_sort.ignoreCase()));\n") + .append(".class, _sort.property())") + .append("\n\t\t\t\t\t") + .append(".reversedIf(_sort.isDescending())") + .append("\n\t\t\t\t\t") + .append(".ignoringCaseIf(_sort.ignoreCase()));\n") .append("\t}\n"); } else if ( type.startsWith(JD_SORT) ) { annotationMetaEntity.staticImport(HIB_SORT_DIRECTION, "*"); declaration .append("\t_orders.add(") - .append(annotationMetaEntity.staticImport(HIB_ORDER, "by")) + .append(annotationMetaEntity.staticImport(HIB_ORDER, "asc")) .append('(') .append(annotationMetaEntity.importType(returnTypeName)) .append(".class, ") .append(name) - .append(".property(),") - .append("\n\t\t\t\t\t\t") + .append(".property())") + .append("\n\t\t\t\t\t") + .append(".reversedIf(") .append(name) - .append(".isAscending() ? ASCENDING : DESCENDING,") - .append("\n\t\t\t\t\t\t") + .append(".isDescending())") + .append("\n\t\t\t\t\t") + .append(".ignoringCaseIf(") .append(name) .append(".ignoreCase()));\n"); }