Browse Source

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 #2103
Original pull request: #2377.
pull/2456/head
Mark Paluch 5 years ago
parent
commit
a45873e98a
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 10
      src/main/java/org/springframework/data/domain/Sort.java
  2. 13
      src/test/java/org/springframework/data/domain/SortUnitTests.java

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

@ -170,7 +170,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord @@ -170,7 +170,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
}
public boolean isSorted() {
return !orders.isEmpty();
return !isEmpty();
}
public boolean isUnsorted() {
@ -188,7 +188,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord @@ -188,7 +188,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
Assert.notNull(sort, "Sort must not be null!");
ArrayList<Order> these = new ArrayList<>(this.orders);
ArrayList<Order> these = new ArrayList<>(this.toList());
for (Order order : sort) {
these.add(order);
@ -240,7 +240,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord @@ -240,7 +240,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
Sort that = (Sort) obj;
return this.orders.equals(that.orders);
return toList().equals(that.toList());
}
/*
@ -261,7 +261,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord @@ -261,7 +261,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
*/
@Override
public String toString() {
return orders.isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
return isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
}
/**
@ -272,7 +272,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord @@ -272,7 +272,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
*/
private Sort withDirection(Direction direction) {
return Sort.by(orders.stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
return Sort.by(stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
}
/**

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

@ -25,6 +25,7 @@ import java.util.Collection; @@ -25,6 +25,7 @@ import java.util.Collection;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.geo.Circle;
/**
* Unit test for {@link Sort}.
@ -33,6 +34,7 @@ import org.springframework.data.domain.Sort.Order; @@ -33,6 +34,7 @@ import org.springframework.data.domain.Sort.Order;
* @author Kevin Raymond
* @author Thomas Darimont
* @author Mark Paluch
* @author Hiufung Kwok
*/
class SortUnitTests {
@ -186,6 +188,17 @@ class SortUnitTests { @@ -186,6 +188,17 @@ class SortUnitTests {
.containsExactly(Order.by("nesteds.firstname"));
}
@Test // #2103
void allowsCombiningTypedSorts() {
assertThat(Sort.sort(Circle.class).by(Circle::getCenter) //
.and(Sort.sort(Circle.class).by(Circle::getRadius))) //
.containsExactly(Order.by("center"), Order.by("radius"));
assertThat(Sort.by("center").and(Sort.sort(Circle.class).by(Circle::getRadius))) //
.containsExactly(Order.by("center"), Order.by("radius"));
}
@Getter
static class Sample {
Nested nested;

Loading…
Cancel
Save