Skip to content

Commit 417e728

Browse files
committed
DATACMNS-867 - Moved to new factory methods for Sort.
Codebase now uses Sort.by(…) where possible instead of the deprecated new Sort(…).
1 parent fee153f commit 417e728

12 files changed

+53
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public PageRequest(int page, int size) {
5555
*/
5656
@Deprecated
5757
public PageRequest(int page, int size, Direction direction, String... properties) {
58-
this(page, size, new Sort(direction, properties));
58+
this(page, size, Sort.by(direction, properties));
5959
}
6060

6161
/**
@@ -83,7 +83,7 @@ public static PageRequest of(int page, int site, Sort sort) {
8383
}
8484

8585
public static PageRequest of(int page, int size, Direction direction, String... properties) {
86-
return of(page, size, new Sort(direction, properties));
86+
return of(page, size, Sort.by(direction, properties));
8787
}
8888

8989
/*

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
3939

4040
private static final long serialVersionUID = 5737186511678863905L;
4141

42-
private static final Sort UNSORTED = new Sort(new Order[0]);
42+
private static final Sort UNSORTED = Sort.by(new Order[0]);
4343

4444
public static final Direction DEFAULT_DIRECTION = Direction.ASC;
4545

@@ -93,7 +93,7 @@ public Sort(Direction direction, String... properties) {
9393
/**
9494
* Creates a new {@link Sort} instance.
9595
*
96-
* @param direction defaults to {@linke Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
96+
* @param direction defaults to {@link Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
9797
* @param properties must not be {@literal null} or contain {@literal null} or empty strings.
9898
*/
9999
public Sort(Direction direction, List<String> properties) {
@@ -148,6 +148,24 @@ public static Sort by(Order... orders) {
148148
return new Sort(orders);
149149
}
150150

151+
/**
152+
* Creates a new {@link Sort} for the given {@link Order}s.
153+
*
154+
* @param direction must not be {@literal null}.
155+
* @param properties must not be {@literal null}.
156+
* @return
157+
*/
158+
public static Sort by(Direction direction, String... properties) {
159+
160+
Assert.notNull(direction, "Direction must not be null!");
161+
Assert.notNull(properties, "Properties must not be null!");
162+
Assert.isTrue(properties.length > 0, "At least one property must be given!");
163+
164+
return Sort.by(Arrays.stream(properties)//
165+
.map(it -> new Order(direction, it))//
166+
.collect(Collectors.toList()));
167+
}
168+
151169
/**
152170
* Returns a {@link Sort} instances representing no sorting setup at all.
153171
*
@@ -419,6 +437,10 @@ public Order(String property) {
419437
this(DEFAULT_DIRECTION, property);
420438
}
421439

440+
public static Order by(String property) {
441+
return new Order(property);
442+
}
443+
422444
/**
423445
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
424446
* {@link Sort#DEFAULT_DIRECTION}
@@ -515,7 +537,7 @@ public Order withProperty(String property) {
515537
* @return
516538
*/
517539
public Sort withProperties(String... properties) {
518-
return new Sort(this.direction, properties);
540+
return Sort.by(this.direction, properties);
519541
}
520542

521543
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private Sort appendOrCreateSortTo(SortDefault sortDefault, Sort sortOrNull) {
178178
return Sort.unsorted();
179179
}
180180

181-
return sortOrNull.and(new Sort(sortDefault.direction(), fields));
181+
return sortOrNull.and(Sort.by(sortDefault.direction(), fields));
182182
}
183183

184184
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public AbstractPageRequest newPageRequest(int page, int size, Sort sort) {
4343
@Test
4444
public void equalsRegardsSortCorrectly() {
4545

46-
Sort sort = new Sort(Direction.DESC, "foo");
46+
Sort sort = Sort.by(Direction.DESC, "foo");
4747
AbstractPageRequest request = PageRequest.of(0, 10, sort);
4848

4949
// Equals itself

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public class SortUnitTests {
3838
*/
3939
@Test
4040
public void appliesDefaultForOrder() throws Exception {
41-
4241
assertThat(Sort.by("foo").iterator().next().getDirection()).isEqualTo(Sort.DEFAULT_DIRECTION);
43-
assertThat(new Sort((Direction) null, "foo").iterator().next().getDirection()).isEqualTo(Sort.DEFAULT_DIRECTION);
4442
}
4543

4644
/**
@@ -51,7 +49,7 @@ public void appliesDefaultForOrder() throws Exception {
5149
@Test(expected = IllegalArgumentException.class)
5250
public void preventsNullProperties() throws Exception {
5351

54-
new Sort(Direction.ASC, (String[]) null);
52+
Sort.by(Direction.ASC, (String[]) null);
5553
}
5654

5755
/**
@@ -62,7 +60,7 @@ public void preventsNullProperties() throws Exception {
6260
@Test(expected = IllegalArgumentException.class)
6361
public void preventsNullProperty() throws Exception {
6462

65-
new Sort(Direction.ASC, (String) null);
63+
Sort.by(Direction.ASC, (String) null);
6664
}
6765

6866
/**
@@ -73,7 +71,7 @@ public void preventsNullProperty() throws Exception {
7371
@Test(expected = IllegalArgumentException.class)
7472
public void preventsEmptyProperty() throws Exception {
7573

76-
new Sort(Direction.ASC, "");
74+
Sort.by(Direction.ASC, "");
7775
}
7876

7977
/**
@@ -84,7 +82,7 @@ public void preventsEmptyProperty() throws Exception {
8482
@Test(expected = IllegalArgumentException.class)
8583
public void preventsNoProperties() throws Exception {
8684

87-
new Sort(Direction.ASC);
85+
Sort.by(Direction.ASC);
8886
}
8987

9088
@Test
@@ -152,4 +150,12 @@ public void createsNewOrderForDifferentProperty() {
152150
assertThat(result.getNullHandling()).isEqualTo(source.getNullHandling());
153151
assertThat(result.isIgnoreCase()).isEqualTo(source.isIgnoreCase());
154152
}
153+
154+
@Test
155+
public void preventsNullDirection() {
156+
157+
assertThatExceptionOfType(IllegalArgumentException.class)//
158+
.isThrownBy(() -> Sort.by((Direction) null, "foo"))//
159+
.withMessageContaining("Direction");
160+
}
155161
}

src/test/java/org/springframework/data/domain/jaxb/SpringDataJaxbUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class SpringDataJaxbUnitTests {
5757
Marshaller marshaller;
5858
Unmarshaller unmarshaller;
5959

60-
Sort sort = new Sort(Direction.ASC, "firstname", "lastname");
60+
Sort sort = Sort.by(Direction.ASC, "firstname", "lastname");
6161
Pageable pageable = PageRequest.of(2, 15, sort);
6262
Resource resource = new ClassPathResource("pageable.xml", this.getClass());
6363
Resource schemaFile = new ClassPathResource("spring-data-jaxb.xsd", this.getClass());

src/test/java/org/springframework/data/querydsl/QSortUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void concatenatesPlainSortCorrectly() {
114114
QUser user = QUser.user;
115115
QSort sort = new QSort(user.firstname.asc());
116116

117-
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
117+
Sort result = sort.and(Sort.by(Direction.ASC, "lastname"));
118118
assertThat(result).hasSize(2);
119119
assertThat(result).contains(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname"));
120120
}
@@ -125,7 +125,7 @@ public void shouldSupportSortByOperatorExpressions() {
125125
QUser user = QUser.user;
126126
QSort sort = new QSort(user.dateOfBirth.yearMonth().asc());
127127

128-
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
128+
Sort result = sort.and(Sort.by(Direction.ASC, "lastname"));
129129
assertThat(result).hasSize(2);
130130
assertThat(result).contains(new Order(Direction.ASC, "lastname"),
131131
new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString()));

src/test/java/org/springframework/data/repository/query/ExtensionAwareEvaluationContextProviderUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void exposesPropertiesDefinedByExtension() {
121121
public void exposesPageableParameter() throws Exception {
122122

123123
this.method = SampleRepo.class.getMethod("findByFirstname", String.class, Pageable.class);
124-
PageRequest pageable = PageRequest.of(2, 3, new Sort(Direction.DESC, "lastname"));
124+
PageRequest pageable = PageRequest.of(2, 3, Sort.by(Direction.DESC, "lastname"));
125125

126126
assertThat(evaluateExpression("#pageable.offset", new Object[] { "test", pageable })).isEqualTo(6L);
127127
assertThat(evaluateExpression("#pageable.pageSize", new Object[] { "test", pageable })).isEqualTo(3);
@@ -133,7 +133,7 @@ public void exposesPageableParameter() throws Exception {
133133
public void exposesSortParameter() throws Exception {
134134

135135
this.method = SampleRepo.class.getMethod("findByFirstname", String.class, Sort.class);
136-
Sort sort = new Sort(Direction.DESC, "lastname");
136+
Sort sort = Sort.by(Direction.DESC, "lastname");
137137

138138
assertThat(evaluateExpression("#sort.toString()", new Object[] { "test", sort })).isEqualTo("lastname: DESC");
139139
}

src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public void handlesMultipleDirectionsCorrectly() throws Exception {
4444

4545
OrderBySource orderBySource = new OrderBySource("LastnameAscUsernameDesc");
4646
assertThat(orderBySource.toSort()).isEqualTo(Sort.by("lastname").ascending().and(Sort.by("username").descending()));
47-
// assertThat(orderBySource.toSort()).hasValue(new Sort(new Order(ASC, "lastname"), new Order(DESC, "username")));
4847
}
4948

5049
@Test(expected = IllegalArgumentException.class)

src/test/java/org/springframework/data/web/HateoasSortHandlerMethodArgumentResolverUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public class HateoasSortHandlerMethodArgumentResolverUnitTests extends SortHandl
3737
public void buildsUpRequestParameters() throws Exception {
3838

3939
assertUriStringFor(SORT, "sort=firstname,lastname,desc");
40-
assertUriStringFor(new Sort(ASC, "foo").and(new Sort(DESC, "bar").and(new Sort(ASC, "foobar"))),
40+
assertUriStringFor(Sort.by(ASC, "foo").and(Sort.by(DESC, "bar").and(Sort.by(ASC, "foobar"))),
4141
"sort=foo,asc&sort=bar,desc&sort=foobar,asc");
42-
assertUriStringFor(new Sort(ASC, "foo").and(new Sort(ASC, "bar").and(new Sort(DESC, "foobar"))),
42+
assertUriStringFor(Sort.by(ASC, "foo").and(Sort.by(ASC, "bar").and(Sort.by(DESC, "foobar"))),
4343
"sort=foo,bar,asc&sort=foobar,desc");
4444
}
4545

0 commit comments

Comments
 (0)