Skip to content

Commit 63e595b

Browse files
committed
DATACMNS-491 - Polishing of null handling API in Sort.Order.
Fixed @since-tags, added author tags where appropriate, fixed copyright header in SortUnitTests. Removed unnecessary formatting line comments. Renamed nullHandlingHint to nullHandling inside Order. Changed builder method signatures to be consistent with with(…) methods defining the direction. Fixed copy & paste test in SortUnitTests. Original pull request: #79.
1 parent f689b5d commit 63e595b

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/main/java/org/springframework/data/domain/Sort.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* {@literal null} or empty strings. The direction defaults to {@link Sort#DEFAULT_DIRECTION}.
3030
*
3131
* @author Oliver Gierke
32+
* @author Thomas Darimont
3233
*/
3334
public class Sort implements Iterable<org.springframework.data.domain.Sort.Order>, Serializable {
3435

@@ -233,24 +234,24 @@ public static Direction fromStringOrNull(String value) {
233234
* Enumeration for null handling hints that can be used in {@link Order} expressions.
234235
*
235236
* @author Thomas Darimont
236-
* @since 1.7
237+
* @since 1.8
237238
*/
238239
public static enum NullHandling {
239240

240241
/**
241242
* Lets the data store decide what to do with nulls.
242243
*/
243-
NATIVE, //
244+
NATIVE,
244245

245246
/**
246247
* A hint to the used data store to order entries with null values before non null entries.
247248
*/
248-
NULLS_FIRST, //
249+
NULLS_FIRST,
249250

250251
/**
251252
* A hint to the used data store to order entries with null values after non null entries.
252253
*/
253-
NULLS_LAST; //
254+
NULLS_LAST;
254255
}
255256

256257
/**
@@ -268,7 +269,7 @@ public static class Order implements Serializable {
268269
private final Direction direction;
269270
private final String property;
270271
private final boolean ignoreCase;
271-
private final NullHandling nullHandlingHint;
272+
private final NullHandling nullHandling;
272273

273274
/**
274275
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
@@ -278,7 +279,6 @@ public static class Order implements Serializable {
278279
* @param property must not be {@literal null} or empty.
279280
*/
280281
public Order(Direction direction, String property) {
281-
282282
this(direction, property, DEFAULT_IGNORE_CASE, null);
283283
}
284284

@@ -288,10 +288,9 @@ public Order(Direction direction, String property) {
288288
*
289289
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}
290290
* @param property must not be {@literal null} or empty.
291-
* @param nullHandlingHint can be {@literal null}, will default to {@link NullHandling#NATIVE}.
291+
* @param nullHandling can be {@literal null}, will default to {@link NullHandling#NATIVE}.
292292
*/
293293
public Order(Direction direction, String property, NullHandling nullHandlingHint) {
294-
295294
this(direction, property, DEFAULT_IGNORE_CASE, nullHandlingHint);
296295
}
297296

@@ -312,10 +311,10 @@ public Order(String property) {
312311
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}
313312
* @param property must not be {@literal null} or empty.
314313
* @param ignoreCase true if sorting should be case insensitive. false if sorting should be case sensitive.
315-
* @param nullHandlingHint can be {@literal null}, will default to {@link NullHandling#NATIVE}.
314+
* @param nullHandling can be {@literal null}, will default to {@link NullHandling#NATIVE}.
316315
* @since 1.7
317316
*/
318-
private Order(Direction direction, String property, boolean ignoreCase, NullHandling nullHandlingHint) {
317+
private Order(Direction direction, String property, boolean ignoreCase, NullHandling nullHandling) {
319318

320319
if (!StringUtils.hasText(property)) {
321320
throw new IllegalArgumentException("Property must not null or empty!");
@@ -324,7 +323,7 @@ private Order(Direction direction, String property, boolean ignoreCase, NullHand
324323
this.direction = direction == null ? DEFAULT_DIRECTION : direction;
325324
this.property = property;
326325
this.ignoreCase = ignoreCase;
327-
this.nullHandlingHint = nullHandlingHint == null ? NullHandling.NATIVE : nullHandlingHint;
326+
this.nullHandling = nullHandling == null ? NullHandling.NATIVE : nullHandling;
328327
}
329328

330329
/**
@@ -383,7 +382,7 @@ public boolean isIgnoreCase() {
383382
* @return
384383
*/
385384
public Order with(Direction order) {
386-
return new Order(order, this.property, nullHandlingHint);
385+
return new Order(order, this.property, nullHandling);
387386
}
388387

389388
/**
@@ -402,28 +401,28 @@ public Sort withProperties(String... properties) {
402401
* @return
403402
*/
404403
public Order ignoreCase() {
405-
return new Order(direction, property, true, nullHandlingHint);
404+
return new Order(direction, property, true, nullHandling);
406405
}
407406

408407
/**
409408
* Returns a {@link Order} with the given {@link NullHandling}.
410409
*
411-
* @param nullHandling
410+
* @param nullHandling can be {@literal null}.
412411
* @return
413-
* @since 1.7
412+
* @since 1.8
414413
*/
415-
public Order withNullHandling(NullHandling nullHandling) {
414+
public Order with(NullHandling nullHandling) {
416415
return new Order(direction, this.property, ignoreCase, nullHandling);
417416
}
418417

419418
/**
420419
* Returns a {@link Order} with {@link NullHandling#NULLS_FIRST} as null handling hint.
421420
*
422421
* @return
423-
* @since 1.7
422+
* @since 1.8
424423
*/
425424
public Order nullsFirst() {
426-
return withNullHandling(NullHandling.NULLS_FIRST);
425+
return with(NullHandling.NULLS_FIRST);
427426
}
428427

429428
/**
@@ -433,7 +432,7 @@ public Order nullsFirst() {
433432
* @since 1.7
434433
*/
435434
public Order nullsLast() {
436-
return withNullHandling(NullHandling.NULLS_LAST);
435+
return with(NullHandling.NULLS_LAST);
437436
}
438437

439438
/**
@@ -443,7 +442,7 @@ public Order nullsLast() {
443442
* @since 1.7
444443
*/
445444
public Order nullsNative() {
446-
return withNullHandling(NullHandling.NATIVE);
445+
return with(NullHandling.NATIVE);
447446
}
448447

449448
/**
@@ -452,8 +451,8 @@ public Order nullsNative() {
452451
* @return
453452
* @since 1.7
454453
*/
455-
public NullHandling getNullHandlingHint() {
456-
return nullHandlingHint;
454+
public NullHandling getNullHandling() {
455+
return nullHandling;
457456
}
458457

459458
/*
@@ -468,7 +467,7 @@ public int hashCode() {
468467
result = 31 * result + direction.hashCode();
469468
result = 31 * result + property.hashCode();
470469
result = 31 * result + (ignoreCase ? 1 : 0);
471-
result = 31 * result + (nullHandlingHint.hashCode());
470+
result = 31 * result + nullHandling.hashCode();
472471

473472
return result;
474473
}
@@ -491,7 +490,7 @@ public boolean equals(Object obj) {
491490
Order that = (Order) obj;
492491

493492
return this.direction.equals(that.direction) && this.property.equals(that.property)
494-
&& this.ignoreCase == that.ignoreCase && this.nullHandlingHint.equals(that.nullHandlingHint);
493+
&& this.ignoreCase == that.ignoreCase && this.nullHandling.equals(that.nullHandling);
495494
}
496495

497496
/*
@@ -503,8 +502,8 @@ public String toString() {
503502

504503
String result = String.format("%s: %s", property, direction);
505504

506-
if (!NullHandling.NATIVE.equals(nullHandlingHint)) {
507-
result += ", " + nullHandlingHint;
505+
if (!NullHandling.NATIVE.equals(nullHandling)) {
506+
result += ", " + nullHandling;
508507
}
509508

510509
if (ignoreCase) {

src/test/java/org/springframework/data/domain/SortUnitTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,18 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.springframework.data.domain.Sort.NullHandling.*;
2021

2122
import org.junit.Test;
2223
import org.springframework.data.domain.Sort.Direction;
23-
import org.springframework.data.domain.Sort.NullHandling;
2424
import org.springframework.data.domain.Sort.Order;
2525

2626
/**
2727
* Unit test for {@link Sort}.
2828
*
2929
* @author Oliver Gierke
3030
* @author Kevin Raymond
31+
* @author Thomas Darimont
3132
*/
3233
public class SortUnitTests {
3334

@@ -137,30 +138,30 @@ public void ordersWithDifferentIgnoreCaseDoNotEqual() {
137138
*/
138139
@Test
139140
public void orderWithNullHandlingHintNullsFirst() {
140-
assertThat(new Order("foo").nullsFirst().getNullHandlingHint(), is(NullHandling.NULLS_FIRST));
141+
assertThat(new Order("foo").nullsFirst().getNullHandling(), is(NULLS_FIRST));
141142
}
142143

143144
/**
144145
* @see DATACMNS-491
145146
*/
146147
@Test
147148
public void orderWithNullHandlingHintNullsLast() {
148-
assertThat(new Order("foo").nullsFirst().getNullHandlingHint(), is(NullHandling.NULLS_FIRST));
149+
assertThat(new Order("foo").nullsLast().getNullHandling(), is(NULLS_LAST));
149150
}
150151

151152
/**
152153
* @see DATACMNS-491
153154
*/
154155
@Test
155156
public void orderWithNullHandlingHintNullsNative() {
156-
assertThat(new Order("foo").nullsNative().getNullHandlingHint(), is(NullHandling.NATIVE));
157+
assertThat(new Order("foo").nullsNative().getNullHandling(), is(NATIVE));
157158
}
158159

159160
/**
160161
* @see DATACMNS-491
161162
*/
162163
@Test
163164
public void orderWithDefaultNullHandlingHint() {
164-
assertThat(new Order("foo").getNullHandlingHint(), is(NullHandling.NATIVE));
165+
assertThat(new Order("foo").getNullHandling(), is(NATIVE));
165166
}
166167
}

0 commit comments

Comments
 (0)