Skip to content

Commit 9d404d3

Browse files
committed
Polishing.
Reformat code, limit scope to nullHandling in SortDefault. See #3152 Original pull request: #3153
1 parent 53cfbfb commit 9d404d3

3 files changed

Lines changed: 61 additions & 164 deletions

File tree

src/main/java/org/springframework/data/web/SortDefault.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,31 @@
4444

4545
/**
4646
* Alias for {@link #sort()} to make a declaration configuring fields only more concise.
47-
*
48-
* @return
4947
*/
5048
@AliasFor("sort")
5149
String[] value() default {};
5250

5351
/**
54-
* The properties to sort by default. If unset, no sorting will be applied at all.
55-
*
56-
* @return
52+
* The properties to sort by default. If not set, no sorting will be applied at all.
5753
*/
5854
@AliasFor("value")
5955
String[] sort() default {};
6056

6157
/**
6258
* The direction to sort by. Defaults to {@link Direction#ASC}.
63-
*
64-
* @return
6559
*/
6660
Direction direction() default Direction.ASC;
6761

6862
/**
6963
* Specifies whether to apply case-sensitive sorting. Defaults to {@literal true}.
70-
*
71-
* @return
7264
* @since 2.3
7365
*/
7466
boolean caseSensitive() default true;
7567

7668
/**
7769
* Specifies which null handling to apply. Defaults to {@link NullHandling#NATIVE}.
7870
*
79-
* @return
80-
* @since 3.4
71+
* @since 4.0.5
8172
*/
8273
NullHandling nullHandling() default NullHandling.NATIVE;
8374

@@ -94,9 +85,9 @@
9485

9586
/**
9687
* The individual {@link SortDefault} declarations to be sorted by.
97-
*
98-
* @return
9988
*/
10089
SortDefault[] value();
90+
10191
}
92+
10293
}

src/main/java/org/springframework/data/web/SortHandlerMethodArgumentResolverSupport.java

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ public abstract class SortHandlerMethodArgumentResolverSupport {
5454
private static final String DEFAULT_QUALIFIER_DELIMITER = "_";
5555
private static final Sort DEFAULT_SORT = Sort.unsorted();
5656

57-
private static final String SORT_DEFAULTS_NAME = SortDefaults.class.getSimpleName();
58-
private static final String SORT_DEFAULT_NAME = SortDefault.class.getSimpleName();
59-
6057
private Sort fallbackSort = DEFAULT_SORT;
6158
private String sortParameter = DEFAULT_PARAMETER;
6259
private String propertyDelimiter = DEFAULT_PROPERTY_DELIMITER;
@@ -165,17 +162,21 @@ private Sort appendOrCreateSortTo(MergedAnnotation<SortDefault> sortDefault, Sor
165162
}
166163

167164
List<Order> orders = new ArrayList<>(fields.length);
165+
Direction direction = sortDefault.getEnum("direction", Direction.class);
166+
NullHandling nullHandling = sortDefault.getEnum("nullHandling", NullHandling.class);
167+
boolean caseSensitive = sortDefault.getBoolean("caseSensitive");
168+
168169
for (String field : fields) {
169170

170-
Order order = new Order(sortDefault.getEnum("direction", Direction.class), field);
171+
Order order = new Order(direction, field);
171172

172-
order = switch (sortDefault.getEnum("nullHandling", NullHandling.class)) {
173+
order = switch (nullHandling) {
173174
case NATIVE -> order.nullsNative();
174175
case NULLS_FIRST -> order.nullsFirst();
175176
case NULLS_LAST -> order.nullsLast();
176177
};
177178

178-
orders.add(sortDefault.getBoolean("caseSensitive") ? order : order.ignoreCase());
179+
orders.add(caseSensitive ? order : order.ignoreCase());
179180
}
180181

181182
return sortOrNull.and(Sort.by(orders));
@@ -223,7 +224,6 @@ Sort parseParameterIntoSort(List<String> source, String delimiter) {
223224
}
224225

225226
SortOrderParser.parse(part, delimiter) //
226-
.parseNullHandling() //
227227
.parseIgnoreCase() //
228228
.parseDirection() //
229229
.forEachOrder(allOrders::add);
@@ -370,28 +370,23 @@ List<String> dumpExpressionIfPresentInto(List<String> expressions) {
370370
static class SortOrderParser {
371371

372372
private static final String IGNORECASE = "ignorecase";
373-
private static final String NULLSNATIVE = "nullsnative";
374-
private static final String NULLSFIRST = "nullsfirst";
375-
private static final String NULLSLAST = "nullslast";
376373

377374
private final String[] elements;
378375
private final int lastIndex;
379376
private final Optional<Direction> direction;
380377
private final Optional<Boolean> ignoreCase;
381-
private final Optional<NullHandling> nullHandling;
382378

383379
private SortOrderParser(String[] elements) {
384-
this(elements, elements.length, Optional.empty(), Optional.empty(), Optional.empty());
380+
this(elements, elements.length, Optional.empty(), Optional.empty());
385381
}
386382

387383
private SortOrderParser(String[] elements, int lastIndex, Optional<Direction> direction,
388-
Optional<Boolean> ignoreCase, Optional<NullHandling> nullHandling) {
384+
Optional<Boolean> ignoreCase) {
389385

390386
this.elements = elements;
391387
this.lastIndex = Math.max(0, lastIndex);
392388
this.direction = direction;
393389
this.ignoreCase = ignoreCase;
394-
this.nullHandling = nullHandling;
395390
}
396391

397392
/**
@@ -410,21 +405,6 @@ public static SortOrderParser parse(String part, String delimiter) {
410405
return new SortOrderParser(elements);
411406
}
412407

413-
/**
414-
* Parse the {@link NullHandling} portion of the sort specification.
415-
*
416-
* @return a new parsing state object.
417-
*/
418-
public SortOrderParser parseNullHandling() {
419-
420-
Optional<NullHandling> nullHandling = lastIndex > 0 ?
421-
fromOptionalNullHandlingString(elements[lastIndex - 1]) :
422-
Optional.empty();
423-
424-
return new SortOrderParser(elements, lastIndex - (nullHandling.isPresent() ? 1 : 0), direction, ignoreCase,
425-
nullHandling);
426-
}
427-
428408
/**
429409
* Parse the {@code ignoreCase} portion of the sort specification.
430410
*
@@ -436,8 +416,7 @@ public SortOrderParser parseIgnoreCase() {
436416
fromOptionalIgnoreCaseString(elements[lastIndex - 1]) :
437417
Optional.empty();
438418

439-
return new SortOrderParser(elements, lastIndex - (ignoreCase.isPresent() ? 1 : 0), direction, ignoreCase,
440-
nullHandling);
419+
return new SortOrderParser(elements, lastIndex - (ignoreCase.isPresent() ? 1 : 0), direction, ignoreCase);
441420
}
442421

443422
/**
@@ -450,8 +429,7 @@ public SortOrderParser parseDirection() {
450429
Optional<Direction> direction = lastIndex > 0 ? Direction.fromOptionalString(elements[lastIndex - 1])
451430
: Optional.empty();
452431

453-
return new SortOrderParser(elements, lastIndex - (direction.isPresent() ? 1 : 0), direction, ignoreCase,
454-
nullHandling);
432+
return new SortOrderParser(elements, lastIndex - (direction.isPresent() ? 1 : 0), direction, ignoreCase);
455433
}
456434

457435
/**
@@ -466,23 +444,6 @@ public void forEachOrder(Consumer<? super Order> callback) {
466444
}
467445
}
468446

469-
private Optional<NullHandling> fromOptionalNullHandlingString(String value) {
470-
471-
if (NULLSNATIVE.equalsIgnoreCase(value)) {
472-
return Optional.of(NullHandling.NATIVE);
473-
}
474-
475-
if (NULLSFIRST.equalsIgnoreCase(value)) {
476-
return Optional.of(NullHandling.NULLS_FIRST);
477-
}
478-
479-
if (NULLSLAST.equalsIgnoreCase(value)) {
480-
return Optional.of(NullHandling.NULLS_LAST);
481-
}
482-
483-
return Optional.empty();
484-
}
485-
486447
private Optional<Boolean> fromOptionalIgnoreCaseString(String value) {
487448
return IGNORECASE.equalsIgnoreCase(value) ? Optional.of(true) : Optional.empty();
488449
}
@@ -495,14 +456,6 @@ private Optional<Order> toOrder(String property) {
495456

496457
Order order = direction.map(it -> new Order(it, property)).orElseGet(() -> Order.by(property));
497458

498-
if (nullHandling.isPresent()) {
499-
order = switch (nullHandling.get()) {
500-
case NATIVE -> order.nullsNative();
501-
case NULLS_FIRST -> order.nullsFirst();
502-
case NULLS_LAST -> order.nullsLast();
503-
};
504-
}
505-
506459
if (ignoreCase.isPresent()) {
507460
return Optional.of(order.ignoreCase());
508461
}

0 commit comments

Comments
 (0)