15
15
*/
16
16
package org .springframework .data .domain ;
17
17
18
+ import java .io .Serial ;
18
19
import java .io .Serializable ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Arrays ;
31
32
import org .springframework .data .util .Streamable ;
32
33
import org .springframework .lang .Nullable ;
33
34
import org .springframework .util .Assert ;
35
+ import org .springframework .util .ObjectUtils ;
34
36
import org .springframework .util .StringUtils ;
35
37
36
38
/**
45
47
*/
46
48
public class Sort implements Streamable <org .springframework .data .domain .Sort .Order >, Serializable {
47
49
48
- private static final long serialVersionUID = 5737186511678863905L ;
50
+ private static final @ Serial long serialVersionUID = 5737186511678863905L ;
49
51
50
52
private static final Sort UNSORTED = Sort .by (new Order [0 ]);
51
53
@@ -63,7 +65,7 @@ protected Sort(List<Order> orders) {
63
65
* @param direction defaults to {@link Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
64
66
* @param properties must not be {@literal null} or contain {@literal null} or empty strings.
65
67
*/
66
- private Sort (Direction direction , List <String > properties ) {
68
+ private Sort (Direction direction , @ Nullable List <String > properties ) {
67
69
68
70
if (properties == null || properties .isEmpty ()) {
69
71
throw new IllegalArgumentException ("You have to provide at least one property to sort by" );
@@ -78,7 +80,7 @@ private Sort(Direction direction, List<String> properties) {
78
80
* Creates a new {@link Sort} for the given properties.
79
81
*
80
82
* @param properties must not be {@literal null}.
81
- * @return
83
+ * @return {@link Sort} for the given properties.
82
84
*/
83
85
public static Sort by (String ... properties ) {
84
86
@@ -93,7 +95,7 @@ public static Sort by(String... properties) {
93
95
* Creates a new {@link Sort} for the given {@link Order}s.
94
96
*
95
97
* @param orders must not be {@literal null}.
96
- * @return
98
+ * @return {@link Sort} for the given {@link Order}s.
97
99
*/
98
100
public static Sort by (List <Order > orders ) {
99
101
@@ -106,7 +108,7 @@ public static Sort by(List<Order> orders) {
106
108
* Creates a new {@link Sort} for the given {@link Order}s.
107
109
*
108
110
* @param orders must not be {@literal null}.
109
- * @return
111
+ * @return {@link Sort} for the given {@link Order}s.
110
112
*/
111
113
public static Sort by (Order ... orders ) {
112
114
@@ -120,7 +122,7 @@ public static Sort by(Order... orders) {
120
122
*
121
123
* @param direction must not be {@literal null}.
122
124
* @param properties must not be {@literal null}.
123
- * @return
125
+ * @return {@link Sort} for the given {@link Direction} and properties.
124
126
*/
125
127
public static Sort by (Direction direction , String ... properties ) {
126
128
@@ -137,7 +139,7 @@ public static Sort by(Direction direction, String... properties) {
137
139
* Creates a new {@link TypedSort} for the given type.
138
140
*
139
141
* @param type must not be {@literal null}.
140
- * @return
142
+ * @return {@link TypedSort} for the given type.
141
143
* @since 2.2
142
144
*/
143
145
public static <T > TypedSort <T > sort (Class <T > type ) {
@@ -147,7 +149,7 @@ public static <T> TypedSort<T> sort(Class<T> type) {
147
149
/**
148
150
* Returns a {@link Sort} instances representing no sorting setup at all.
149
151
*
150
- * @return
152
+ * @return unsorted Sort instance.
151
153
*/
152
154
public static Sort unsorted () {
153
155
return UNSORTED ;
@@ -156,7 +158,7 @@ public static Sort unsorted() {
156
158
/**
157
159
* Returns a new {@link Sort} with the current setup but descending order direction.
158
160
*
159
- * @return
161
+ * @return a new {@link Sort} with the current setup but descending order direction.
160
162
*/
161
163
public Sort descending () {
162
164
return withDirection (Direction .DESC );
@@ -165,12 +167,15 @@ public Sort descending() {
165
167
/**
166
168
* Returns a new {@link Sort} with the current setup but ascending order direction.
167
169
*
168
- * @return
170
+ * @return a new {@link Sort} with the current setup but ascending order direction.
169
171
*/
170
172
public Sort ascending () {
171
173
return withDirection (Direction .ASC );
172
174
}
173
175
176
+ /**
177
+ * @return {@literal true} if this Sort instance is sorted, {@literal false} otherwise.
178
+ */
174
179
public boolean isSorted () {
175
180
return !isEmpty ();
176
181
}
@@ -180,6 +185,9 @@ public boolean isEmpty() {
180
185
return orders .isEmpty ();
181
186
}
182
187
188
+ /**
189
+ * @return {@literal true} if this Sort instance is unsorted, {@literal false} otherwise.
190
+ */
183
191
public boolean isUnsorted () {
184
192
return !isSorted ();
185
193
}
@@ -189,7 +197,8 @@ public boolean isUnsorted() {
189
197
* ones.
190
198
*
191
199
* @param sort must not be {@literal null}.
192
- * @return
200
+ * @return a new {@link Sort} consisting of the {@link Order}s of the current {@link Sort} combined with the given
201
+ * ones.
193
202
*/
194
203
public Sort and (Sort sort ) {
195
204
@@ -232,8 +241,8 @@ protected List<Order> doReverse() {
232
241
/**
233
242
* Returns the order registered for the given property.
234
243
*
235
- * @param property
236
- * @return
244
+ * @param property name of the property that should be sorted.
245
+ * @return the sort {@link Order} or {@literal null} if the property is not sorted by.
237
246
*/
238
247
@ Nullable
239
248
public Order getOrderFor (String property ) {
@@ -247,6 +256,7 @@ public Order getOrderFor(String property) {
247
256
return null ;
248
257
}
249
258
259
+ @ Override
250
260
public Iterator <Order > iterator () {
251
261
return this .orders .iterator ();
252
262
}
@@ -278,12 +288,6 @@ public String toString() {
278
288
return isEmpty () ? "UNSORTED" : StringUtils .collectionToCommaDelimitedString (orders );
279
289
}
280
290
281
- /**
282
- * Creates a new {@link Sort} with the current setup but the given order direction.
283
- *
284
- * @param direction
285
- * @return
286
- */
287
291
private Sort withDirection (Direction direction ) {
288
292
289
293
List <Order > result = new ArrayList <>(orders .size ());
@@ -307,7 +311,7 @@ public enum Direction {
307
311
/**
308
312
* Returns whether the direction is ascending.
309
313
*
310
- * @return
314
+ * @return {@literal true} if ascending, {@literal false} otherwise.
311
315
* @since 1.13
312
316
*/
313
317
public boolean isAscending () {
@@ -317,7 +321,7 @@ public boolean isAscending() {
317
321
/**
318
322
* Returns whether the direction is descending.
319
323
*
320
- * @return
324
+ * @return {@literal true} if descending, {@literal false} otherwise.
321
325
* @since 1.13
322
326
*/
323
327
public boolean isDescending () {
@@ -327,9 +331,9 @@ public boolean isDescending() {
327
331
/**
328
332
* Returns the {@link Direction} enum for the given {@link String} value.
329
333
*
330
- * @param value
334
+ * @param value the direction name.
335
+ * @return the {@link Direction} enum value for the given {@code value}.
331
336
* @throws IllegalArgumentException in case the given value cannot be parsed into an enum value.
332
- * @return
333
337
*/
334
338
public static Direction fromString (String value ) {
335
339
@@ -342,15 +346,16 @@ public static Direction fromString(String value) {
342
346
}
343
347
344
348
/**
345
- * Returns the {@link Direction} enum for the given {@link String} or empty if it cannot be parsed into an enum
346
- * value.
349
+ * Returns the {@link Direction} enum for the given {@link String} or {@code Optional. empty()} if it cannot be
350
+ * parsed into an enum value.
347
351
*
348
- * @param value
349
- * @return
352
+ * @param value the direction name.
353
+ * @return Optional holding the {@link Direction} enum value or empty, if {@code value} cannot be parsed into
354
+ * {@link Direction}.
350
355
*/
351
356
public static Optional <Direction > fromOptionalString (String value ) {
352
357
353
- if (value == null ) {
358
+ if (ObjectUtils . isEmpty ( value ) ) {
354
359
return Optional .empty ();
355
360
}
356
361
@@ -396,7 +401,8 @@ public enum NullHandling {
396
401
*/
397
402
public static class Order implements Serializable {
398
403
399
- private static final long serialVersionUID = 1522511010900108987L ;
404
+ private static final @ Serial long serialVersionUID = 1522511010900108987L ;
405
+
400
406
private static final boolean DEFAULT_IGNORE_CASE = false ;
401
407
private static final NullHandling DEFAULT_NULL_HANDLING = NullHandling .NATIVE ;
402
408
@@ -486,7 +492,7 @@ public static Order desc(String property) {
486
492
/**
487
493
* Returns the order the property shall be sorted for.
488
494
*
489
- * @return
495
+ * @return the order the property shall be sorted for.
490
496
*/
491
497
public Direction getDirection () {
492
498
return direction ;
@@ -495,7 +501,7 @@ public Direction getDirection() {
495
501
/**
496
502
* Returns the property to order for.
497
503
*
498
- * @return
504
+ * @return the property to order for.
499
505
*/
500
506
public String getProperty () {
501
507
return property ;
@@ -504,7 +510,7 @@ public String getProperty() {
504
510
/**
505
511
* Returns whether sorting for this property shall be ascending.
506
512
*
507
- * @return
513
+ * @return {@literal true} if ascending, {@literal false} otherwise.
508
514
*/
509
515
public boolean isAscending () {
510
516
return this .direction .isAscending ();
@@ -513,7 +519,7 @@ public boolean isAscending() {
513
519
/**
514
520
* Returns whether sorting for this property shall be descending.
515
521
*
516
- * @return
522
+ * @return {@literal true} if descending, {@literal false} otherwise.
517
523
* @since 1.13
518
524
*/
519
525
public boolean isDescending () {
@@ -523,48 +529,48 @@ public boolean isDescending() {
523
529
/**
524
530
* Returns whether the sort will be case-sensitive or case-insensitive.
525
531
*
526
- * @return
532
+ * @return {@literal true} if the sort will be case-sensitive or case-insensitive, {@literal false} otherwise.
527
533
*/
528
534
public boolean isIgnoreCase () {
529
535
return ignoreCase ;
530
536
}
531
537
532
538
/**
533
- * Returns a new {@link Order} with the given {@link Direction}.
539
+ * Returns a new {@link Order} with the given {@link Direction} applied .
534
540
*
535
- * @param direction
536
- * @return
541
+ * @param direction the new direction to use.
542
+ * @return a new {@link Order} with the given {@link Direction} applied.
537
543
*/
538
544
public Order with (Direction direction ) {
539
545
return new Order (direction , this .property , this .ignoreCase , this .nullHandling );
540
546
}
541
547
542
548
/**
543
- * Returns a new {@link Order} with the reversed {@link #getDirection()}.
549
+ * Returns a new {@link Order} with the reversed {@link #getDirection()} applied .
544
550
*
545
- * @return
551
+ * @return a reversed {@link Order} with the given {@link Direction} applied.
546
552
* @since 3.1
547
553
*/
548
554
public Order reverse () {
549
555
return with (this .direction == Direction .ASC ? Direction .DESC : Direction .ASC );
550
556
}
551
557
552
558
/**
553
- * Returns a new {@link Order}
559
+ * Returns a new {@link Order} with the {@code property} name applied.
554
560
*
555
561
* @param property must not be {@literal null} or empty.
556
- * @return
562
+ * @return a new {@link Order} with the {@code property} name applied.
557
563
* @since 1.13
558
564
*/
559
565
public Order withProperty (String property ) {
560
566
return new Order (this .direction , property , this .ignoreCase , this .nullHandling );
561
567
}
562
568
563
569
/**
564
- * Returns a new {@link Sort} instance for the given properties.
570
+ * Returns a new {@link Sort} instance for the given properties using {@link #getDirection()} .
565
571
*
566
- * @param properties
567
- * @return
572
+ * @param properties properties to sort by.
573
+ * @return a new {@link Sort} instance for the given properties using {@link #getDirection()}.
568
574
*/
569
575
public Sort withProperties (String ... properties ) {
570
576
return Sort .by (this .direction , properties );
@@ -573,47 +579,47 @@ public Sort withProperties(String... properties) {
573
579
/**
574
580
* Returns a new {@link Order} with case-insensitive sorting enabled.
575
581
*
576
- * @return
582
+ * @return a new {@link Order} with case-insensitive sorting enabled.
577
583
*/
578
584
public Order ignoreCase () {
579
585
return new Order (direction , property , true , nullHandling );
580
586
}
581
587
582
588
/**
583
- * Returns a {@link Order} with the given {@link NullHandling}.
589
+ * Returns a {@link Order} with the given {@link NullHandling} applied .
584
590
*
585
591
* @param nullHandling can be {@literal null}.
586
- * @return
592
+ * @return a new {@link Order} with the given {@link NullHandling} applied.
587
593
* @since 1.8
588
594
*/
589
595
public Order with (NullHandling nullHandling ) {
590
596
return new Order (direction , this .property , ignoreCase , nullHandling );
591
597
}
592
598
593
599
/**
594
- * Returns a {@link Order} with {@link NullHandling#NULLS_FIRST} as null handling hint.
600
+ * Returns a new {@link Order} with {@link NullHandling#NULLS_FIRST} as null handling hint applied .
595
601
*
596
- * @return
602
+ * @return a new {@link Order} with {@link NullHandling#NULLS_FIRST} as null handling hint applied.
597
603
* @since 1.8
598
604
*/
599
605
public Order nullsFirst () {
600
606
return with (NullHandling .NULLS_FIRST );
601
607
}
602
608
603
609
/**
604
- * Returns a {@link Order} with {@link NullHandling#NULLS_LAST} as null handling hint.
610
+ * Returns a new {@link Order} with {@link NullHandling#NULLS_LAST} as null handling hint applied .
605
611
*
606
- * @return
612
+ * @return a new {@link Order} with {@link NullHandling#NULLS_LAST} as null handling hint applied.
607
613
* @since 1.7
608
614
*/
609
615
public Order nullsLast () {
610
616
return with (NullHandling .NULLS_LAST );
611
617
}
612
618
613
619
/**
614
- * Returns a {@link Order} with {@link NullHandling#NATIVE} as null handling hint.
620
+ * Returns a new {@link Order} with {@link NullHandling#NATIVE} as null handling hint applied .
615
621
*
616
- * @return
622
+ * @return a new {@link Order} with {@link NullHandling#NATIVE} as null handling hint applied.
617
623
* @since 1.7
618
624
*/
619
625
public Order nullsNative () {
@@ -623,7 +629,7 @@ public Order nullsNative() {
623
629
/**
624
630
* Returns the used {@link NullHandling} hint, which can but may not be respected by the used datastore.
625
631
*
626
- * @return
632
+ * @return the used {@link NullHandling} hint.
627
633
* @since 1.7
628
634
*/
629
635
public NullHandling getNullHandling () {
@@ -684,7 +690,7 @@ public String toString() {
684
690
*/
685
691
public static class TypedSort <T > extends Sort {
686
692
687
- private static final long serialVersionUID = -3550403511206745880L ;
693
+ private static final @ Serial long serialVersionUID = -3550403511206745880L ;
688
694
689
695
private final Recorded <T > recorded ;
690
696
@@ -739,7 +745,7 @@ public Iterator<Order> iterator() {
739
745
740
746
@ Override
741
747
public boolean isEmpty () {
742
- return ! recorded .getPropertyPath ().isPresent ();
748
+ return recorded .getPropertyPath ().isEmpty ();
743
749
}
744
750
745
751
@ Override
0 commit comments