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.
This commit is contained in:
Mark Paluch
2021-06-10 14:59:27 +02:00
parent 366bd30290
commit a45873e98a
2 changed files with 18 additions and 5 deletions

View File

@@ -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
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
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
*/
@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
*/
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()));
}
/**

View File

@@ -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;
* @author Kevin Raymond
* @author Thomas Darimont
* @author Mark Paluch
* @author Hiufung Kwok
*/
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;