Skip to content

Commit 5561ae0

Browse files
Polishing.
Update test, since tags, rename method. Original Pull Requests: #4935 & #4943
1 parent 1ffa287 commit 5561ae0

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArrayOperators.java

+19-30
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ public SortArray sort(Sort sort) {
350350
}
351351

352352
/**
353-
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
354-
* order}.
353+
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given
354+
* {@link Direction order}.
355355
*
356356
* @return new instance of {@link SortArray}.
357-
* @since 4.0
357+
* @since 4.5
358358
*/
359359
public SortArray sort(Direction direction) {
360360

361361
if (usesFieldRef()) {
362-
return SortArray.sortArrayOf(fieldReference).by(direction);
362+
return SortArray.sortArrayOf(fieldReference).direction(direction);
363363
}
364364

365-
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
365+
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).direction(direction);
366366
}
367367

368368
/**
@@ -1998,10 +1998,6 @@ public static First firstOf(AggregationExpression expression) {
19981998
return new First(expression);
19991999
}
20002000

2001-
/*
2002-
* (non-Javadoc)
2003-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2004-
*/
20052001
@Override
20062002
protected String getMongoMethod() {
20072003
return "$first";
@@ -2052,10 +2048,6 @@ public static Last lastOf(AggregationExpression expression) {
20522048
return new Last(expression);
20532049
}
20542050

2055-
/*
2056-
* (non-Javadoc)
2057-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2058-
*/
20592051
@Override
20602052
protected String getMongoMethod() {
20612053
return "$last";
@@ -2116,41 +2108,38 @@ public SortArray by(Sort sort) {
21162108
}
21172109

21182110
/**
2119-
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
2120-
* strings).
2111+
* Order the values for the array in the given direction.
21212112
*
2113+
* @param direction must not be {@literal null}.
21222114
* @return new instance of {@link SortArray}.
2123-
* @since 4.x (TBD)
2115+
* @since 4.5
21242116
*/
2125-
public SortArray byValueAscending() {
2126-
return new SortArray(append("sortBy", 1));
2117+
public SortArray direction(Direction direction) {
2118+
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
21272119
}
21282120

21292121
/**
2130-
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
2122+
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
21312123
* strings).
21322124
*
21332125
* @return new instance of {@link SortArray}.
2134-
* @since 4.x (TBD)
2126+
* @since 4.5
21352127
*/
2136-
public SortArray byValueDescending() {
2137-
return new SortArray(append("sortBy", -1));
2128+
public SortArray byValueAscending() {
2129+
return direction(Direction.ASC);
21382130
}
21392131

21402132
/**
2141-
* Set the order to put elements in.
2133+
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
2134+
* strings).
21422135
*
2143-
* @param direction must not be {@literal null}.
21442136
* @return new instance of {@link SortArray}.
2137+
* @since 4.5
21452138
*/
2146-
public SortArray by(Direction direction) {
2147-
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
2139+
public SortArray byValueDescending() {
2140+
return direction(Direction.DESC);
21482141
}
21492142

2150-
/*
2151-
* (non-Javadoc)
2152-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2153-
*/
21542143
@Override
21552144
protected String getMongoMethod() {
21562145
return "$sortArray";

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArrayOperatorsUnitTests.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.aggregation;
1717

18-
import static org.springframework.data.mongodb.test.util.Assertions.*;
18+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
1919

2020
import java.util.ArrayList;
2121
import java.util.Arrays;
@@ -26,7 +26,6 @@
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.domain.Sort.Direction;
2828
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
29-
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
3029

3130
/**
3231
* Unit tests for {@link ArrayOperators}
@@ -184,25 +183,16 @@ void sortByWithFieldRef() {
184183

185184
@Test // GH-4929
186185
public void sortArrayByValueAscending() {
187-
Document result = SortArray.sortArrayOf("numbers").byValueAscending().toDocument(Aggregation.DEFAULT_CONTEXT);
188-
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", 1));
189-
assertThat(result).isEqualTo(expected);
186+
187+
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.ASC).toDocument(Aggregation.DEFAULT_CONTEXT);
188+
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: 1 } }");
190189
}
191190

192191
@Test // GH-4929
193192
public void sortArrayByValueDescending() {
194-
Document result = SortArray.sortArrayOf("numbers").byValueDescending().toDocument(Aggregation.DEFAULT_CONTEXT);
195-
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", -1));
196-
assertThat(result).isEqualTo(expected);
197-
}
198193

199-
@Test // GH-4929
200-
public void sortArrayByPropertyUnchanged() {
201-
Document result = SortArray.sortArrayOf("items").by(Sort.by(Sort.Direction.ASC, "price"))
202-
.toDocument(Aggregation.DEFAULT_CONTEXT);
203-
Document expected = new Document("$sortArray",
204-
new Document("input", "$items").append("sortBy", new Document("price", 1)));
205-
assertThat(result).isEqualTo(expected);
194+
Document result = ArrayOperators.arrayOf("numbers").sort(Direction.DESC).toDocument(Aggregation.DEFAULT_CONTEXT);
195+
assertThat(result).isEqualTo("{ $sortArray: { input: '$numbers', sortBy: -1 } }");
206196
}
207197

208198
@Test // GH-4929

0 commit comments

Comments
 (0)