Skip to content

Commit 06018fa

Browse files
committed
DATAMONGO-2287 - Polishing.
Add since tags. Remove final modifier from method args. Switch to lambdas. Original pull request: #760.
1 parent 8b406b2 commit 06018fa

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

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

+14-21
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,9 @@ public Zip zip(Object... arrays) {
16261626
*
16271627
* @author Christoph Strobl
16281628
* @author Shashank Sharma
1629+
* @see <a href=
1630+
* "https://docs.mongodb.com/manual/reference/operator/aggregation/in/">https://docs.mongodb.com/manual/reference/operator/aggregation/in/</a>
1631+
* @since 2.2
16291632
*/
16301633
public static class In extends AbstractAggregationExpression {
16311634

@@ -1644,18 +1647,14 @@ protected String getMongoMethod() {
16441647
* @param fieldReference must not be {@literal null}.
16451648
* @return
16461649
*/
1647-
public static InBuilder arrayOf(final String fieldReference) {
1650+
public static InBuilder arrayOf(String fieldReference) {
16481651

16491652
Assert.notNull(fieldReference, "FieldReference must not be null!");
16501653

1651-
return new InBuilder() {
1652-
1653-
@Override
1654-
public In containsValue(Object value) {
1654+
return value -> {
16551655

1656-
Assert.notNull(value, "Value must not be null!");
1657-
return new In(Arrays.asList(value, Fields.field(fieldReference)));
1658-
}
1656+
Assert.notNull(value, "Value must not be null!");
1657+
return new In(Arrays.asList(value, Fields.field(fieldReference)));
16591658
};
16601659
}
16611660

@@ -1669,20 +1668,17 @@ public static InBuilder arrayOf(AggregationExpression expression) {
16691668

16701669
Assert.notNull(expression, "Expression must not be null!");
16711670

1672-
return new InBuilder() {
1671+
return value -> {
16731672

1674-
@Override
1675-
public In containsValue(Object value) {
1673+
Assert.notNull(value, "Value must not be null!");
16761674

1677-
Assert.notNull(value, "Value must not be null!");
1678-
return new In(Arrays.asList(value, expression));
1679-
}
1675+
return new In(Arrays.asList(value, expression));
16801676
};
16811677
}
16821678

16831679
/**
16841680
* Support for Aggregation In Search an Element in List of Objects to Filter Start creating {@link In}.
1685-
*
1681+
*
16861682
* @param values must not be {@literal null}.
16871683
* @return new instance of {@link InBuilder}.
16881684
* @since 2.2
@@ -1691,14 +1687,11 @@ public static InBuilder arrayOf(Collection<?> values) {
16911687

16921688
Assert.notNull(values, "Values must not be null!");
16931689

1694-
return new InBuilder() {
1690+
return value -> {
16951691

1696-
@Override
1697-
public In containsValue(Object value) {
1692+
Assert.notNull(value, "Value must not be null!");
16981693

1699-
Assert.notNull(value, "Value must not be null!");
1700-
return new In(Arrays.asList(value, values));
1701-
}
1694+
return new In(Arrays.asList(value, values));
17021695
};
17031696
}
17041697

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

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

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

2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.List;
2323

2424
import org.bson.Document;
2525
import org.junit.Test;
26+
2627
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
2728

2829
/**
2930
* Unit tests for {@link ArrayOperators}
30-
*
31+
*
3132
* @author Christoph Strobl
3233
* @author Shashank Sharma
3334
* @currentRead Royal Assassin - Robin Hobb
@@ -44,14 +45,14 @@ public class ArrayOperatorsUnitTests {
4445
public void toArrayWithFieldReference() {
4546

4647
assertThat(ArrayOperators.arrayOf("regal").toObject().toDocument(Aggregation.DEFAULT_CONTEXT))
47-
.isEqualTo(Document.parse("{ $arrayToObject: \"$regal\" } "));
48+
.isEqualTo("{ $arrayToObject: \"$regal\" } ");
4849
}
4950

5051
@Test // DATAMONGO-2052
5152
public void toArrayWithExpression() {
5253

5354
assertThat(ArrayOperators.arrayOf(EXPRESSION).toObject().toDocument(Aggregation.DEFAULT_CONTEXT))
54-
.isEqualTo(Document.parse("{ $arrayToObject: " + EXPRESSION_STRING + "} "));
55+
.isEqualTo("{ $arrayToObject: " + EXPRESSION_STRING + "} ");
5556
}
5657

5758
@Test // DATAMONGO-2052
@@ -62,71 +63,70 @@ public void toArrayWithArgumentList() {
6263
source.add(Arrays.asList("prince", "verity"));
6364

6465
assertThat(ArrayToObject.arrayToObject(source).toDocument(Aggregation.DEFAULT_CONTEXT))
65-
.isEqualTo(Document.parse("{ $arrayToObject: [ [ \"king\", \"shrewd\"], [ \"prince\", \"verity\" ] ] } "));
66+
.isEqualTo("{ $arrayToObject: [ [ \"king\", \"shrewd\"], [ \"prince\", \"verity\" ] ] } ");
6667
}
6768

6869
@Test // DATAMONGO-2287
6970
public void arrayElementAtWithValueList() {
7071

7172
assertThat(ArrayOperators.arrayOf(VALUE_LIST).elementAt(1).toDocument(Aggregation.DEFAULT_CONTEXT))
72-
.isEqualTo(Document.parse("{ $arrayElemAt: [ " + VALUE_LIST_STRING + ", 1] } "));
73+
.isEqualTo("{ $arrayElemAt: [ " + VALUE_LIST_STRING + ", 1] } ");
7374
}
7475

7576
@Test // DATAMONGO-2287
7677
public void concatWithValueList() {
7778

7879
assertThat(ArrayOperators.arrayOf(VALUE_LIST).concat("field").toDocument(Aggregation.DEFAULT_CONTEXT))
79-
.isEqualTo(Document.parse("{ $concatArrays: [ " + VALUE_LIST_STRING + ", \"$field\"] } "));
80+
.isEqualTo("{ $concatArrays: [ " + VALUE_LIST_STRING + ", \"$field\"] } ");
8081
}
8182

8283
@Test // DATAMONGO-2287
8384
public void filterWithValueList() {
8485

8586
assertThat(ArrayOperators.arrayOf(VALUE_LIST).filter().as("var").by(new Document())
8687
.toDocument(Aggregation.DEFAULT_CONTEXT))
87-
.isEqualTo(Document
88-
.parse("{ $filter: { \"input\" : " + VALUE_LIST_STRING + ", \"as\" : \"var\", \"cond\" : {} } } "));
88+
.isEqualTo("{ $filter: { \"input\" : " + VALUE_LIST_STRING + ", \"as\" : \"var\", \"cond\" : {} } } ");
8989
}
9090

9191
@Test // DATAMONGO-2287
9292
public void lengthWithValueList() {
9393

9494
assertThat(ArrayOperators.arrayOf(VALUE_LIST).length().toDocument(Aggregation.DEFAULT_CONTEXT))
95-
.isEqualTo(Document.parse("{ $size: [ " + VALUE_LIST_STRING + "] } "));
95+
.isEqualTo("{ $size: [ " + VALUE_LIST_STRING + "] } ");
9696
}
9797

9898
@Test // DATAMONGO-2287
9999
public void sliceWithValueList() {
100100

101101
assertThat(ArrayOperators.arrayOf(VALUE_LIST).slice().itemCount(3).toDocument(Aggregation.DEFAULT_CONTEXT))
102-
.isEqualTo(Document.parse("{ $slice: [ " + VALUE_LIST_STRING + ", 3] } "));
102+
.isEqualTo("{ $slice: [ " + VALUE_LIST_STRING + ", 3] } ");
103103
}
104104

105105
@Test // DATAMONGO-2287
106106
public void indexOfWithValueList() {
107107

108108
assertThat(ArrayOperators.arrayOf(VALUE_LIST).indexOf("s1p").toDocument(Aggregation.DEFAULT_CONTEXT))
109-
.isEqualTo(Document.parse("{ $indexOfArray: [ " + VALUE_LIST_STRING + ", \"s1p\"] } "));
109+
.isEqualTo("{ $indexOfArray: [ " + VALUE_LIST_STRING + ", \"s1p\"] } ");
110110
}
111111

112112
@Test // DATAMONGO-2287
113113
public void reverseWithValueList() {
114114

115115
assertThat(ArrayOperators.arrayOf(VALUE_LIST).reverse().toDocument(Aggregation.DEFAULT_CONTEXT))
116-
.isEqualTo(Document.parse("{ $reverseArray: [ " + VALUE_LIST_STRING + "] } "));
116+
.isEqualTo("{ $reverseArray: [ " + VALUE_LIST_STRING + "] } ");
117117
}
118118

119119
@Test // DATAMONGO-2287
120120
public void zipWithValueList() {
121121

122122
assertThat(ArrayOperators.arrayOf(VALUE_LIST).zipWith("field").toDocument(Aggregation.DEFAULT_CONTEXT))
123-
.isEqualTo(Document.parse("{ $zip: { \"inputs\": [" + VALUE_LIST_STRING + ", \"$field\"]} } "));
123+
.isEqualTo("{ $zip: { \"inputs\": [" + VALUE_LIST_STRING + ", \"$field\"]} } ");
124124
}
125125

126126
@Test // DATAMONGO-2287
127127
public void inWithValueList() {
128128

129129
assertThat(ArrayOperators.arrayOf(VALUE_LIST).containsValue("$userName").toDocument(Aggregation.DEFAULT_CONTEXT))
130-
.isEqualTo(Document.parse("{ \"$in\" : [\"$userName\", " + VALUE_LIST_STRING + "] }"));
130+
.isEqualTo("{ \"$in\" : [\"$userName\", " + VALUE_LIST_STRING + "] }");
131131
}
132132
}

0 commit comments

Comments
 (0)