4
4
*/
5
5
package org .hibernate .query ;
6
6
7
+ import jakarta .persistence .criteria .Nulls ;
7
8
import jakarta .persistence .metamodel .SingularAttribute ;
8
9
import org .hibernate .Incubating ;
9
10
@@ -42,11 +43,11 @@ public class Order<X> {
42
43
private final SingularAttribute <X ,?> attribute ;
43
44
private final Class <X > entityClass ;
44
45
private final String attributeName ;
45
- private final NullPrecedence nullPrecedence ;
46
+ private final Nulls nullPrecedence ;
46
47
private final int element ;
47
48
private final boolean ignoreCase ;
48
49
49
- private Order (SortDirection order , NullPrecedence nullPrecedence , SingularAttribute <X , ?> attribute ) {
50
+ private Order (SortDirection order , Nulls nullPrecedence , SingularAttribute <X , ?> attribute ) {
50
51
this .order = order ;
51
52
this .attribute = attribute ;
52
53
this .attributeName = attribute .getName ();
@@ -56,7 +57,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttrib
56
57
this .ignoreCase = false ;
57
58
}
58
59
59
- private Order (SortDirection order , NullPrecedence nullPrecedence , SingularAttribute <X , ?> attribute , boolean ignoreCase ) {
60
+ private Order (SortDirection order , Nulls nullPrecedence , SingularAttribute <X , ?> attribute , boolean ignoreCase ) {
60
61
this .order = order ;
61
62
this .attribute = attribute ;
62
63
this .attributeName = attribute .getName ();
@@ -66,7 +67,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttrib
66
67
this .ignoreCase = ignoreCase ;
67
68
}
68
69
69
- private Order (SortDirection order , NullPrecedence nullPrecedence , Class <X > entityClass , String attributeName ) {
70
+ private Order (SortDirection order , Nulls nullPrecedence , Class <X > entityClass , String attributeName ) {
70
71
this .order = order ;
71
72
this .entityClass = entityClass ;
72
73
this .attributeName = attributeName ;
@@ -76,7 +77,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entit
76
77
this .ignoreCase = false ;
77
78
}
78
79
79
- private Order (SortDirection order , NullPrecedence nullPrecedence , int element ) {
80
+ private Order (SortDirection order , Nulls nullPrecedence , int element ) {
80
81
this .order = order ;
81
82
this .entityClass = null ;
82
83
this .attributeName = null ;
@@ -86,7 +87,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, int element) {
86
87
this .ignoreCase = false ;
87
88
}
88
89
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 ) {
90
91
this .order = order ;
91
92
this .entityClass = entityClass ;
92
93
this .attributeName = attributeName ;
@@ -96,7 +97,7 @@ private Order(SortDirection order, NullPrecedence nullPrecedence, Class<X> entit
96
97
this .ignoreCase = ignoreCase ;
97
98
}
98
99
99
- private Order (SortDirection order , NullPrecedence nullPrecedence , int element , boolean ignoreCase ) {
100
+ private Order (SortDirection order , Nulls nullPrecedence , int element , boolean ignoreCase ) {
100
101
this .order = order ;
101
102
this .entityClass = null ;
102
103
this .attributeName = null ;
@@ -126,7 +127,7 @@ private Order(Order<X> other, boolean ignoreCase) {
126
127
this .ignoreCase = ignoreCase ;
127
128
}
128
129
129
- private Order (Order <X > other , NullPrecedence nullPrecedence ) {
130
+ private Order (Order <X > other , Nulls nullPrecedence ) {
130
131
this .order = other .order ;
131
132
this .attribute = other .attribute ;
132
133
this .entityClass = other .entityClass ;
@@ -142,7 +143,7 @@ private Order(Order<X> other, NullPrecedence nullPrecedence) {
142
143
* type, the ordering is case-sensitive.
143
144
*/
144
145
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 );
146
147
}
147
148
148
149
/**
@@ -151,7 +152,7 @@ public static <T> Order<T> asc(SingularAttribute<T,?> attribute) {
151
152
* type, the ordering is case-sensitive.
152
153
*/
153
154
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 );
155
156
}
156
157
157
158
/**
@@ -160,15 +161,15 @@ public static <T> Order<T> desc(SingularAttribute<T,?> attribute) {
160
161
* type, the ordering is case-sensitive.
161
162
*/
162
163
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 );
164
165
}
165
166
166
167
/**
167
168
* An order where an entity is sorted by the given attribute,
168
169
* in the given direction, with the specified case-sensitivity.
169
170
*/
170
171
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 );
172
173
}
173
174
174
175
/**
@@ -177,7 +178,7 @@ public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection di
177
178
* null values. If the give attribute is of textual type, the
178
179
* ordering is case-sensitive.
179
180
*/
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 ) {
181
182
return new Order <>(direction , nullPrecedence , attribute );
182
183
}
183
184
@@ -188,7 +189,7 @@ public static <T> Order<T> by(SingularAttribute<T,?> attribute, SortDirection di
188
189
* case-sensitive.
189
190
*/
190
191
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 );
192
193
}
193
194
194
195
/**
@@ -198,7 +199,7 @@ public static <T> Order<T> asc(Class<T> entityClass, String attributeName) {
198
199
* case-sensitive.
199
200
*/
200
201
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 );
202
203
}
203
204
204
205
/**
@@ -208,7 +209,7 @@ public static <T> Order<T> desc(Class<T> entityClass, String attributeName) {
208
209
* case-sensitive.
209
210
*/
210
211
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 );
212
213
}
213
214
214
215
/**
@@ -217,7 +218,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
217
218
* the specified case-sensitivity.
218
219
*/
219
220
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 );
221
222
}
222
223
223
224
/**
@@ -227,7 +228,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
227
228
* precedence for null values. If the named attribute is of
228
229
* textual type, the ordering is case-sensitive.
229
230
*/
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 ) {
231
232
return new Order <>( direction , nullPrecedence , entityClass , attributeName );
232
233
}
233
234
@@ -237,7 +238,7 @@ public static <T> Order<T> by(Class<T> entityClass, String attributeName, SortDi
237
238
* item is of textual type, the ordering is case-sensitive.
238
239
*/
239
240
public static Order <Object []> asc (int element ) {
240
- return new Order <>( ASCENDING , NullPrecedence .NONE , element );
241
+ return new Order <>( ASCENDING , Nulls .NONE , element );
241
242
}
242
243
243
244
/**
@@ -246,7 +247,7 @@ public static Order<Object[]> asc(int element) {
246
247
* item is of textual type, the ordering is case-sensitive.
247
248
*/
248
249
public static Order <Object []> desc (int element ) {
249
- return new Order <>( DESCENDING , NullPrecedence .NONE , element );
250
+ return new Order <>( DESCENDING , Nulls .NONE , element );
250
251
}
251
252
252
253
/**
@@ -255,7 +256,7 @@ public static Order<Object[]> desc(int element) {
255
256
* is of textual type, the ordering is case-sensitive.
256
257
*/
257
258
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 );
259
260
}
260
261
261
262
/**
@@ -264,7 +265,7 @@ public static Order<Object[]> by(int element, SortDirection direction) {
264
265
* case-sensitivity.
265
266
*/
266
267
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 );
268
269
}
269
270
270
271
/**
@@ -273,15 +274,15 @@ public static Order<Object[]> by(int element, SortDirection direction, boolean i
273
274
* precedence for null values. If the named attribute is of
274
275
* textual type, the ordering is case-sensitive.
275
276
*/
276
- public static Order <Object []> by (int element , SortDirection direction , NullPrecedence nullPrecedence ) {
277
+ public static Order <Object []> by (int element , SortDirection direction , Nulls nullPrecedence ) {
277
278
return new Order <>( direction , nullPrecedence , element );
278
279
}
279
280
280
281
public SortDirection getDirection () {
281
282
return order ;
282
283
}
283
284
284
- public NullPrecedence getNullPrecedence () {
285
+ public Nulls getNullPrecedence () {
285
286
return nullPrecedence ;
286
287
}
287
288
@@ -326,15 +327,45 @@ public Order<X> ignoringCase() {
326
327
* @since 6.5
327
328
*/
328
329
public Order <X > withNullsFirst () {
329
- return new Order <>( this , NullPrecedence .FIRST );
330
+ return new Order <>( this , Nulls .FIRST );
330
331
}
331
332
332
333
/**
333
334
* @return this order, but with nulls sorted last.
334
335
* @since 6.5
335
336
*/
336
337
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 ;
338
369
}
339
370
340
371
@ Override
@@ -343,9 +374,8 @@ public String toString() {
343
374
}
344
375
345
376
@ 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 ) {
349
379
return that .order == this .order
350
380
&& that .nullPrecedence == this .nullPrecedence
351
381
&& that .ignoreCase == this .ignoreCase
0 commit comments