Skip to content

Commit 9242b2c

Browse files
committed
Allow combining Sort and TypedSort using Sort.and(…).
We now retain properly the collection of orders by using accessor methods instead of relying on using the orders field. TypedSort orders are not using the orders field, instead they iterate over recorded persistent property paths. Closes spring-projects#2103 Original pull request: spring-projects#2377.
1 parent f97b4b9 commit 9242b2c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public Sort ascending() {
170170
}
171171

172172
public boolean isSorted() {
173-
return !orders.isEmpty();
173+
return !isEmpty();
174174
}
175175

176176
public boolean isUnsorted() {
@@ -188,7 +188,7 @@ public Sort and(Sort sort) {
188188

189189
Assert.notNull(sort, "Sort must not be null!");
190190

191-
ArrayList<Order> these = new ArrayList<>(this.orders);
191+
ArrayList<Order> these = new ArrayList<>(this.toList());
192192

193193
for (Order order : sort) {
194194
these.add(order);
@@ -240,7 +240,7 @@ public boolean equals(@Nullable Object obj) {
240240

241241
Sort that = (Sort) obj;
242242

243-
return this.orders.equals(that.orders);
243+
return toList().equals(that.toList());
244244
}
245245

246246
/*
@@ -261,7 +261,7 @@ public int hashCode() {
261261
*/
262262
@Override
263263
public String toString() {
264-
return orders.isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
264+
return isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
265265
}
266266

267267
/**
@@ -272,7 +272,7 @@ public String toString() {
272272
*/
273273
private Sort withDirection(Direction direction) {
274274

275-
return Sort.by(orders.stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
275+
return Sort.by(stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
276276
}
277277

278278
/**

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

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.data.domain.Sort.Direction;
2727
import org.springframework.data.domain.Sort.Order;
28+
import org.springframework.data.geo.Circle;
2829

2930
/**
3031
* Unit test for {@link Sort}.
@@ -33,6 +34,7 @@
3334
* @author Kevin Raymond
3435
* @author Thomas Darimont
3536
* @author Mark Paluch
37+
* @author Hiufung Kwok
3638
*/
3739
class SortUnitTests {
3840

@@ -186,6 +188,17 @@ void translatesTypedSortCorrectly() {
186188
.containsExactly(Order.by("nesteds.firstname"));
187189
}
188190

191+
@Test // #2103
192+
void allowsCombiningTypedSorts() {
193+
194+
assertThat(Sort.sort(Circle.class).by(Circle::getCenter) //
195+
.and(Sort.sort(Circle.class).by(Circle::getRadius))) //
196+
.containsExactly(Order.by("center"), Order.by("radius"));
197+
198+
assertThat(Sort.by("center").and(Sort.sort(Circle.class).by(Circle::getRadius))) //
199+
.containsExactly(Order.by("center"), Order.by("radius"));
200+
}
201+
189202
@Getter
190203
static class Sample {
191204
Nested nested;

0 commit comments

Comments
 (0)