Skip to content

Commit 853b2b2

Browse files
committed
DATAMONGO-1540 - Polishing.
Reduce Map aggregation expression builder entrypoint. Fix JavaDoc. Original pull request: spring-projects#420.
1 parent c3f9af0 commit 853b2b2

File tree

3 files changed

+50
-53
lines changed

3 files changed

+50
-53
lines changed

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

+42-49
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.mongodb.DBObject;
2626
import org.bson.Document;
2727
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
28-
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Map.ArrayOfBuilder;
2928
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
3029
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
3130
import org.springframework.util.Assert;
@@ -1782,20 +1781,33 @@ private boolean usesFieldRef() {
17821781
}
17831782

17841783
/**
1785-
* Gateway to {@literal Date} aggregation operations.
1784+
* Gateway to {@literal variable} aggregation operations.
17861785
*
17871786
* @author Christoph Strobl
1787+
* @author Mark Paluch
17881788
*/
17891789
class VariableOperators {
17901790

17911791
/**
17921792
* Starts building new {@link Map} that applies an {@link AggregationExpression} to each item of a referenced array
17931793
* and returns an array with the applied results.
17941794
*
1795+
* @param fieldReference must not be {@literal null}.
1796+
* @return
1797+
*/
1798+
public static Map.AsBuilder mapItemsOf(String fieldReference) {
1799+
return Map.itemsOf(fieldReference);
1800+
}
1801+
1802+
/**
1803+
* Starts building new {@link Map} that applies an {@link AggregationExpression} to each item of a referenced array
1804+
* and returns an array with the applied results.
1805+
*
1806+
* @param expression must not be {@literal null}.
17951807
* @return
17961808
*/
1797-
public static ArrayOfBuilder map() {
1798-
return Map.map();
1809+
public static Map.AsBuilder mapItemsOf(AggregationExpression expression) {
1810+
return Map.itemsOf(expression);
17991811
}
18001812
}
18011813

@@ -5757,51 +5769,52 @@ private Map(Object sourceArray, String itemVariableName, AggregationExpression f
57575769
* Starts building new {@link Map} that applies an {@link AggregationExpression} to each item of a referenced array
57585770
* and returns an array with the applied results.
57595771
*
5772+
* @param fieldReference must not be {@literal null}.
57605773
* @return
57615774
*/
5762-
static ArrayOfBuilder map() {
5775+
static AsBuilder itemsOf(final String fieldReference) {
57635776

5764-
return new ArrayOfBuilder() {
5777+
return new AsBuilder() {
57655778

57665779
@Override
5767-
public AsBuilder itemsOf(final String fieldReference) {
5780+
public FunctionBuilder as(final String variableName) {
57685781

5769-
return new AsBuilder() {
5782+
return new FunctionBuilder() {
57705783

57715784
@Override
5772-
public FunctionBuilder as(final String variableName) {
5773-
5774-
return new FunctionBuilder() {
5775-
5776-
@Override
5777-
public Map andApply(final AggregationExpression expression) {
5778-
return new Map(Fields.field(fieldReference), variableName, expression);
5779-
}
5780-
};
5785+
public Map andApply(final AggregationExpression expression) {
5786+
return new Map(Fields.field(fieldReference), variableName, expression);
57815787
}
57825788
};
57835789
}
57845790

5785-
@Override
5786-
public AsBuilder itemsOf(final AggregationExpression source) {
5791+
};
5792+
};
57875793

5788-
return new AsBuilder() {
5794+
/**
5795+
* Starts building new {@link Map} that applies an {@link AggregationExpression} to each item of a referenced array
5796+
* and returns an array with the applied results.
5797+
*
5798+
* @param source must not be {@literal null}.
5799+
* @return
5800+
*/
5801+
public static AsBuilder itemsOf(final AggregationExpression source) {
57895802

5790-
@Override
5791-
public FunctionBuilder as(final String variableName) {
5803+
return new AsBuilder() {
5804+
5805+
@Override
5806+
public FunctionBuilder as(final String variableName) {
57925807

5793-
return new FunctionBuilder() {
5808+
return new FunctionBuilder() {
57945809

5795-
@Override
5796-
public Map andApply(final AggregationExpression expression) {
5797-
return new Map(source, variableName, expression);
5798-
}
5799-
};
5810+
@Override
5811+
public Map andApply(final AggregationExpression expression) {
5812+
return new Map(source, variableName, expression);
58005813
}
58015814
};
58025815
}
58035816
};
5804-
};
5817+
}
58055818

58065819
@Override
58075820
public Document toDocument(final AggregationOperationContext context) {
@@ -5841,26 +5854,6 @@ private Document toMap(AggregationOperationContext context) {
58415854
return new Document("$map", map);
58425855
}
58435856

5844-
interface ArrayOfBuilder {
5845-
5846-
/**
5847-
* Set the field that resolves to an array on which to apply the {@link AggregationExpression}.
5848-
*
5849-
* @param fieldReference must not be {@literal null}.
5850-
* @return
5851-
*/
5852-
AsBuilder itemsOf(String fieldReference);
5853-
5854-
/**
5855-
* Set the {@link AggregationExpression} that results in an array on which to apply the
5856-
* {@link AggregationExpression}.
5857-
*
5858-
* @param expression must not be {@literal null}.
5859-
* @return
5860-
*/
5861-
AsBuilder itemsOf(AggregationExpression expression);
5862-
}
5863-
58645857
interface AsBuilder {
58655858

58665859
/**

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1674,13 +1674,13 @@ public void shouldRenderNotAggregationExpression() {
16741674
}
16751675

16761676
/**
1677-
* @see DATAMONGO-784
1677+
* @see DATAMONGO-1540
16781678
*/
16791679
@Test
16801680
public void shouldRenderMapAggregationExpression() {
16811681

16821682
Document agg = Aggregation.project()
1683-
.and(VariableOperators.map().itemsOf("quizzes").as("grade")
1683+
.and(VariableOperators.mapItemsOf("quizzes").as("grade")
16841684
.andApply(AggregationFunctionExpressions.ADD.of(field("grade"), 2)))
16851685
.as("adjustedGrades").toDocument(Aggregation.DEFAULT_CONTEXT);
16861686

@@ -1689,13 +1689,13 @@ public void shouldRenderMapAggregationExpression() {
16891689
}
16901690

16911691
/**
1692-
* @see DATAMONGO-784
1692+
* @see DATAMONGO-1540
16931693
*/
16941694
@Test
16951695
public void shouldRenderMapAggregationExpressionOnExpression() {
16961696

16971697
Document agg = Aggregation.project()
1698-
.and(VariableOperators.map().itemsOf(AggregationFunctionExpressions.SIZE.of("foo")).as("grade")
1698+
.and(VariableOperators.mapItemsOf(AggregationFunctionExpressions.SIZE.of("foo")).as("grade")
16991699
.andApply(AggregationFunctionExpressions.ADD.of(field("grade"), 2)))
17001700
.as("adjustedGrades").toDocument(Aggregation.DEFAULT_CONTEXT);
17011701

src/main/asciidoc/reference/mongodb.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,10 @@ At the time of this writing we provide support for the following Aggregation Ope
17021702
| Date Aggregation Operators
17031703
| dayOfYear, dayOfMonth, dayOfWeek, year, month, week, hour, minute, second, millisecond, dateToString
17041704

1705+
| Variable Operators
1706+
| map
1707+
1708+
17051709
| Conditional Aggregation Operators
17061710
| cond, ifNull
17071711

0 commit comments

Comments
 (0)