44 */
55package org .hibernate .query ;
66
7+ import jakarta .persistence .criteria .Nulls ;
78import jakarta .persistence .metamodel .SingularAttribute ;
89import 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
0 commit comments