Skip to content

Commit 65b9b43

Browse files
committed
add Order.reversedIf() and ignoringCaseIf() to simplify generated code
1 parent 83f7af3 commit 65b9b43

File tree

2 files changed

+86
-52
lines changed

2 files changed

+86
-52
lines changed

hibernate-core/src/main/java/org/hibernate/query/Order.java

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.query;
66

7+
import jakarta.persistence.criteria.Nulls;
78
import jakarta.persistence.metamodel.SingularAttribute;
89
import org.hibernate.Incubating;
910

@@ -42,11 +43,11 @@ public class Order<X> {
4243
private final SingularAttribute<X,?> attribute;
4344
private final Class<X> entityClass;
4445
private final String attributeName;
45-
private final NullPrecedence nullPrecedence;
46+
private final Nulls nullPrecedence;
4647
private final int element;
4748
private final boolean ignoreCase;
4849

49-
private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttribute<X, ?> attribute) {
50+
private Order(SortDirection order, Nulls nullPrecedence, SingularAttribute<X, ?> attribute) {
5051
this.order = order;
5152
this.attribute = attribute;
5253
this.attributeName = attribute.getName();
@@ -56,7 +57,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttrib
5657
this.ignoreCase = false;
5758
}
5859

59-
private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttribute<X, ?> attribute, boolean ignoreCase) {
60+
private Order(SortDirection order, Nulls nullPrecedence, SingularAttribute<X, ?> attribute, boolean ignoreCase) {
6061
this.order = order;
6162
this.attribute = attribute;
6263
this.attributeName = attribute.getName();
@@ -66,7 +67,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttrib
6667
this.ignoreCase = ignoreCase;
6768
}
6869

69-
private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entityClass, String attributeName) {
70+
private Order(SortDirection order, Nulls nullPrecedence, Class<X> entityClass, String attributeName) {
7071
this.order = order;
7172
this.entityClass = entityClass;
7273
this.attributeName = attributeName;
@@ -76,7 +77,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entit
7677
this.ignoreCase = false;
7778
}
7879

79-
private Order(SortDirection order, NullPrecedence nullPrecedence, int element) {
80+
private Order(SortDirection order, Nulls nullPrecedence, int element) {
8081
this.order = order;
8182
this.entityClass = null;
8283
this.attributeName = null;
@@ -86,7 +87,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, int element) {
8687
this.ignoreCase = false;
8788
}
8889

89-
private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entityClass, String attributeName, boolean ignoreCase) {
90+
private Order(SortDirection order, Nulls nullPrecedence, Class<X> entityClass, String attributeName, boolean ignoreCase) {
9091
this.order = order;
9192
this.entityClass = entityClass;
9293
this.attributeName = attributeName;
@@ -96,7 +97,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entit
9697
this.ignoreCase = ignoreCase;
9798
}
9899

99-
private Order(SortDirection order, NullPrecedence nullPrecedence, int element, boolean ignoreCase) {
100+
private Order(SortDirection order, Nulls nullPrecedence, int element, boolean ignoreCase) {
100101
this.order = order;
101102
this.entityClass = null;
102103
this.attributeName = null;
@@ -126,7 +127,7 @@ private Order(Order<X> other, boolean ignoreCase) {
126127
this.ignoreCase = ignoreCase;
127128
}
128129

129-
private Order(Order<X> other, NullPrecedence nullPrecedence) {
130+
private Order(Order<X> other, Nulls nullPrecedence) {
130131
this.order = other.order;
131132
this.attribute = other.attribute;
132133
this.entityClass = other.entityClass;
@@ -142,7 +143,7 @@ private Order(Order<X> other, NullPrecedence nullPrecedence) {
142143
* type, the ordering is case-sensitive.
143144
*/
144145
public static <T> Order<T> asc(SingularAttribute<T,?> attribute) {
145-
return new Order<>(ASCENDING, NullPrecedence.NONE, attribute);
146+
return new Order<>(ASCENDING, Nulls.NONE, attribute);
146147
}
147148

148149
/**
@@ -151,7 +152,7 @@ public static <T> Order<T> asc(SingularAttribute<T,?> attribute) {
151152
* type, the ordering is case-sensitive.
152153
*/
153154
public static <T> Order<T> desc(SingularAttribute<T,?> attribute) {
154-
return new Order<>(DESCENDING, NullPrecedence.NONE, attribute);
155+
return new Order<>(DESCENDING, Nulls.NONE, attribute);
155156
}
156157

157158
/**
@@ -160,15 +161,15 @@ public static <T> Order<T> desc(SingularAttribute<T,?> attribute) {
160161
* type, the ordering is case-sensitive.
161162
*/
162163
public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection direction) {
163-
return new Order<>(direction, NullPrecedence.NONE, attribute);
164+
return new Order<>(direction, Nulls.NONE, attribute);
164165
}
165166

166167
/**
167168
* An order where an entity is sorted by the given attribute,
168169
* in the given direction, with the specified case-sensitivity.
169170
*/
170171
public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection direction, boolean ignoreCase) {
171-
return new Order<>(direction, NullPrecedence.NONE, attribute, ignoreCase);
172+
return new Order<>(direction, Nulls.NONE, attribute, ignoreCase);
172173
}
173174

174175
/**
@@ -177,7 +178,7 @@ public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection di
177178
* null values. If the give attribute is of textual type, the
178179
* ordering is case-sensitive.
179180
*/
180-
public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection direction, NullPrecedence nullPrecedence) {
181+
public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection direction, Nulls nullPrecedence) {
181182
return new Order<>(direction, nullPrecedence, attribute);
182183
}
183184

@@ -188,7 +189,7 @@ public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection di
188189
* case-sensitive.
189190
*/
190191
public static <T> Order<T> asc(Class<T> entityClass, String attributeName) {
191-
return new Order<>( ASCENDING, NullPrecedence.NONE, entityClass, attributeName );
192+
return new Order<>( ASCENDING, Nulls.NONE, entityClass, attributeName );
192193
}
193194

194195
/**
@@ -198,7 +199,7 @@ public static <T> Order<T> asc(Class<T> entityClass, String attributeName) {
198199
* case-sensitive.
199200
*/
200201
public static <T> Order<T> desc(Class<T> entityClass, String attributeName) {
201-
return new Order<>( DESCENDING, NullPrecedence.NONE, entityClass, attributeName );
202+
return new Order<>( DESCENDING, Nulls.NONE, entityClass, attributeName );
202203
}
203204

204205
/**
@@ -208,7 +209,7 @@ public static <T> Order<T> desc(Class<T> entityClass, String attributeName) {
208209
* case-sensitive.
209210
*/
210211
public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDirection direction) {
211-
return new Order<>( direction, NullPrecedence.NONE, entityClass, attributeName );
212+
return new Order<>( direction, Nulls.NONE, entityClass, attributeName );
212213
}
213214

214215
/**
@@ -217,7 +218,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
217218
* the specified case-sensitivity.
218219
*/
219220
public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDirection direction, boolean ignoreCase) {
220-
return new Order<>( direction, NullPrecedence.NONE, entityClass, attributeName, ignoreCase );
221+
return new Order<>( direction, Nulls.NONE, entityClass, attributeName, ignoreCase );
221222
}
222223

223224
/**
@@ -227,7 +228,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
227228
* precedence for null values. If the named attribute is of
228229
* textual type, the ordering is case-sensitive.
229230
*/
230-
public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDirection direction, NullPrecedence nullPrecedence) {
231+
public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDirection direction, Nulls nullPrecedence) {
231232
return new Order<>( direction, nullPrecedence, entityClass, attributeName );
232233
}
233234

@@ -237,7 +238,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
237238
* item is of textual type, the ordering is case-sensitive.
238239
*/
239240
public static Order<Object[]> asc(int element) {
240-
return new Order<>( ASCENDING, NullPrecedence.NONE, element );
241+
return new Order<>( ASCENDING, Nulls.NONE, element );
241242
}
242243

243244
/**
@@ -246,7 +247,7 @@ public static Order<Object[]> asc(int element) {
246247
* item is of textual type, the ordering is case-sensitive.
247248
*/
248249
public static Order<Object[]> desc(int element) {
249-
return new Order<>( DESCENDING, NullPrecedence.NONE, element );
250+
return new Order<>( DESCENDING, Nulls.NONE, element );
250251
}
251252

252253
/**
@@ -255,7 +256,7 @@ public static Order<Object[]> desc(int element) {
255256
* is of textual type, the ordering is case-sensitive.
256257
*/
257258
public static Order<Object[]> by(int element, SortDirection direction) {
258-
return new Order<>( direction, NullPrecedence.NONE, element );
259+
return new Order<>( direction, Nulls.NONE, element );
259260
}
260261

261262
/**
@@ -264,7 +265,7 @@ public static Order<Object[]> by(int element, SortDirection direction) {
264265
* case-sensitivity.
265266
*/
266267
public static Order<Object[]> by(int element, SortDirection direction, boolean ignoreCase) {
267-
return new Order<>( direction, NullPrecedence.NONE, element, ignoreCase );
268+
return new Order<>( direction, Nulls.NONE, element, ignoreCase );
268269
}
269270

270271
/**
@@ -273,15 +274,15 @@ public static Order<Object[]> by(int element, SortDirection direction, boolean i
273274
* precedence for null values. If the named attribute is of
274275
* textual type, the ordering is case-sensitive.
275276
*/
276-
public static Order<Object[]> by(int element, SortDirection direction, NullPrecedence nullPrecedence) {
277+
public static Order<Object[]> by(int element, SortDirection direction, Nulls nullPrecedence) {
277278
return new Order<>( direction, nullPrecedence, element );
278279
}
279280

280281
public SortDirection getDirection() {
281282
return order;
282283
}
283284

284-
public NullPrecedence getNullPrecedence() {
285+
public Nulls getNullPrecedence() {
285286
return nullPrecedence;
286287
}
287288

@@ -326,15 +327,45 @@ public Order<X> ignoringCase() {
326327
* @since 6.5
327328
*/
328329
public Order<X> withNullsFirst() {
329-
return new Order<>( this, NullPrecedence.FIRST );
330+
return new Order<>( this, Nulls.FIRST );
330331
}
331332

332333
/**
333334
* @return this order, but with nulls sorted last.
334335
* @since 6.5
335336
*/
336337
public Order<X> withNullsLast() {
337-
return new Order<>( this, NullPrecedence.LAST );
338+
return new Order<>( this, Nulls.LAST );
339+
}
340+
341+
/**
342+
* An order based on this order, possibly reversed.
343+
*
344+
* @param reverse {@code true} if the returned order should be
345+
* {@linkplain #reverse reversed}
346+
* @return this order, but reversed if the argument is {@code true}
347+
*
348+
* @apiNote This is a convenience for use with Jakarta Data
349+
*
350+
* @since 7.0
351+
*/
352+
public Order<X> reversedIf(boolean reverse) {
353+
return reverse ? reverse() : this;
354+
}
355+
356+
/**
357+
* An order based on this order, possibly without case-sensitivity.
358+
*
359+
* @param ignoreCase {@code true} if this order should be
360+
* {@linkplain #ignoringCase ignore case}
361+
* @return this order, but ignoring case if the argument is {@code true}
362+
*
363+
* @apiNote This is a convenience for use with Jakarta Data
364+
*
365+
* @since 7.0
366+
*/
367+
public Order<X> ignoringCaseIf(boolean ignoreCase) {
368+
return ignoreCase ? ignoringCase() : this;
338369
}
339370

340371
@Override
@@ -343,9 +374,8 @@ public String toString() {
343374
}
344375

345376
@Override
346-
public boolean equals(Object o) {
347-
if ( o instanceof Order) {
348-
Order<?> that = (Order<?>) o;
377+
public boolean equals(Object object) {
378+
if ( object instanceof Order<?> that) {
349379
return that.order == this.order
350380
&& that.nullPrecedence == this.nullPrecedence
351381
&& that.ignoreCase == this.ignoreCase

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,18 @@ void collectOrdering(StringBuilder declaration, List<String> paramTypes) {
500500
annotationMetaEntity.staticImport(HIB_SORT_DIRECTION, "*");
501501
declaration
502502
.append("\t_orders.add(")
503-
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
503+
.append(annotationMetaEntity.staticImport(HIB_ORDER, orderBy.descending ? "desc" : "asc"))
504504
.append('(')
505505
.append(annotationMetaEntity.importType(returnTypeName))
506506
.append(".class, \"")
507507
.append(orderBy.fieldName)
508-
.append("\", ")
509-
.append(orderBy.descending ? "DESCENDING" : "ASCENDING")
510-
.append(", ")
511-
.append(orderBy.ignoreCase)
512-
.append("));\n");
508+
.append("\")");
509+
if ( orderBy.ignoreCase ) {
510+
declaration
511+
.append("\n\t.ignoringCase()");
512+
}
513+
declaration
514+
.append(");\n");
513515

514516
}
515517
for (int i = 0; i < paramTypes.size(); i++) {
@@ -542,14 +544,14 @@ else if ( type.startsWith(JD_ORDER) ) {
542544
.append(name)
543545
.append(".sorts()) {\n")
544546
.append("\t\t_orders.add(")
545-
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
547+
.append(annotationMetaEntity.staticImport(HIB_ORDER, "asc"))
546548
.append('(')
547549
.append(annotationMetaEntity.importType(returnTypeName))
548-
.append(".class, _sort.property(),")
549-
.append("\n\t\t\t\t\t\t")
550-
.append("_sort.isAscending() ? ASCENDING : DESCENDING,")
551-
.append("\n\t\t\t\t\t\t")
552-
.append("_sort.ignoreCase()));\n")
550+
.append(".class, _sort.property())")
551+
.append("\n\t\t\t\t\t")
552+
.append(".reversedIf(_sort.isDescending())")
553+
.append("\n\t\t\t\t\t")
554+
.append(".ignoringCaseIf(_sort.ignoreCase()));\n")
553555
.append("\t}\n");
554556
}
555557
else if ( type.startsWith(JD_SORT) && type.endsWith("...") ) {
@@ -560,30 +562,32 @@ else if ( type.startsWith(JD_SORT) && type.endsWith("...") ) {
560562
.append(name)
561563
.append(") {\n")
562564
.append("\t\t_orders.add(")
563-
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
565+
.append(annotationMetaEntity.staticImport(HIB_ORDER, "asc"))
564566
.append('(')
565567
.append(annotationMetaEntity.importType(returnTypeName))
566-
.append(".class, _sort.property(),")
567-
.append("\n\t\t\t\t\t\t")
568-
.append("_sort.isAscending() ? ASCENDING : DESCENDING,")
569-
.append("\n\t\t\t\t\t\t")
570-
.append("_sort.ignoreCase()));\n")
568+
.append(".class, _sort.property())")
569+
.append("\n\t\t\t\t\t")
570+
.append(".reversedIf(_sort.isDescending())")
571+
.append("\n\t\t\t\t\t")
572+
.append(".ignoringCaseIf(_sort.ignoreCase()));\n")
571573
.append("\t}\n");
572574
}
573575
else if ( type.startsWith(JD_SORT) ) {
574576
annotationMetaEntity.staticImport(HIB_SORT_DIRECTION, "*");
575577
declaration
576578
.append("\t_orders.add(")
577-
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
579+
.append(annotationMetaEntity.staticImport(HIB_ORDER, "asc"))
578580
.append('(')
579581
.append(annotationMetaEntity.importType(returnTypeName))
580582
.append(".class, ")
581583
.append(name)
582-
.append(".property(),")
583-
.append("\n\t\t\t\t\t\t")
584+
.append(".property())")
585+
.append("\n\t\t\t\t\t")
586+
.append(".reversedIf(")
584587
.append(name)
585-
.append(".isAscending() ? ASCENDING : DESCENDING,")
586-
.append("\n\t\t\t\t\t\t")
588+
.append(".isDescending())")
589+
.append("\n\t\t\t\t\t")
590+
.append(".ignoringCaseIf(")
587591
.append(name)
588592
.append(".ignoreCase()));\n");
589593
}

0 commit comments

Comments
 (0)