Skip to content

Commit eb66b4a

Browse files
committed
DATAMONGO-1540 - Polishing.
Reduce Map aggregation expression builder entrypoint. Fix JavaDoc.
1 parent 4dd27db commit eb66b4a

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

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323

2424
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
25-
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Map.ArrayOfBuilder;
2625
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
2726
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
2827
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 DBObject toDbObject(final AggregationOperationContext context) {
@@ -5841,26 +5854,6 @@ private DBObject toMap(AggregationOperationContext context) {
58415854
return new BasicDBObject("$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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,13 +1675,13 @@ public void shouldRenderNotAggregationExpression() {
16751675
}
16761676

16771677
/**
1678-
* @see DATAMONGO-784
1678+
* @see DATAMONGO-1540
16791679
*/
16801680
@Test
16811681
public void shouldRenderMapAggregationExpression() {
16821682

16831683
DBObject agg = Aggregation.project()
1684-
.and(VariableOperators.map().itemsOf("quizzes").as("grade")
1684+
.and(VariableOperators.mapItemsOf("quizzes").as("grade")
16851685
.andApply(AggregationFunctionExpressions.ADD.of(field("grade"), 2)))
16861686
.as("adjustedGrades").toDBObject(Aggregation.DEFAULT_CONTEXT);
16871687

@@ -1690,13 +1690,13 @@ public void shouldRenderMapAggregationExpression() {
16901690
}
16911691

16921692
/**
1693-
* @see DATAMONGO-784
1693+
* @see DATAMONGO-1540
16941694
*/
16951695
@Test
16961696
public void shouldRenderMapAggregationExpressionOnExpression() {
16971697

16981698
DBObject agg = Aggregation.project()
1699-
.and(VariableOperators.map().itemsOf(AggregationFunctionExpressions.SIZE.of("foo")).as("grade")
1699+
.and(VariableOperators.mapItemsOf(AggregationFunctionExpressions.SIZE.of("foo")).as("grade")
17001700
.andApply(AggregationFunctionExpressions.ADD.of(field("grade"), 2)))
17011701
.as("adjustedGrades").toDBObject(Aggregation.DEFAULT_CONTEXT);
17021702

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,10 @@ At the time of this writing we provide support for the following Aggregation Ope
17001700
| Date Aggregation Operators
17011701
| dayOfYear, dayOfMonth, dayOfWeek, year, month, week, hour, minute, second, millisecond, dateToString
17021702

1703+
| Variable Operators
1704+
| map
1705+
1706+
17031707
| Conditional Aggregation Operators
17041708
| cond, ifNull
17051709

0 commit comments

Comments
 (0)