Skip to content

Commit c9dfeea

Browse files
DATAMONGO-1542 - Polishing.
Added some static entry points for better readability. Original Pull Request: #421
1 parent 1a11877 commit c9dfeea

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6438,12 +6438,52 @@ private void assertNotBuilder(Object toCheck, String name) {
64386438
/**
64396439
* Get a builder that allows fluent creation of {@link Cond}.
64406440
*
6441-
* @return a new {@link ConditionalExpressionBuilder}.
6441+
* @return never {@literal null}.
64426442
*/
6443-
public static ConditionalExpressionBuilder newBuilder() {
6443+
public static WhenBuilder newBuilder() {
64446444
return ConditionalExpressionBuilder.newBuilder();
64456445
}
64466446

6447+
/**
6448+
* Start creating new {@link Cond} by providing the boolean expression used in {@code if}.
6449+
*
6450+
* @param booleanExpression must not be {@literal null}.
6451+
* @return never {@literal null}.
6452+
*/
6453+
public static ThenBuilder when(DBObject booleanExpression) {
6454+
return ConditionalExpressionBuilder.newBuilder().when(booleanExpression);
6455+
}
6456+
6457+
/**
6458+
* Start creating new {@link Cond} by providing the {@link AggregationExpression} used in {@code if}.
6459+
*
6460+
* @param expression expression that yields in a boolean result, must not be {@literal null}.
6461+
* @return never {@literal null}.
6462+
*/
6463+
public static ThenBuilder when(AggregationExpression expression) {
6464+
return ConditionalExpressionBuilder.newBuilder().when(expression);
6465+
}
6466+
6467+
/**
6468+
* Start creating new {@link Cond} by providing the field reference used in {@code if}.
6469+
*
6470+
* @param booleanField name of a field holding a boolean value, must not be {@literal null}.
6471+
* @return never {@literal null}.
6472+
*/
6473+
public static ThenBuilder when(String booleanField) {
6474+
return ConditionalExpressionBuilder.newBuilder().when(booleanField);
6475+
}
6476+
6477+
/**
6478+
* Start creating new {@link Cond} by providing the {@link CriteriaDefinition} used in {@code if}.
6479+
*
6480+
* @param criteria criteria to evaluate, must not be {@literal null}.
6481+
* @return the {@link ThenBuilder}
6482+
*/
6483+
public static ThenBuilder when(CriteriaDefinition criteria) {
6484+
return ConditionalExpressionBuilder.newBuilder().when(criteria);
6485+
}
6486+
64476487
/**
64486488
* @author Mark Paluch
64496489
*/

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
@@ -1706,10 +1706,6 @@ public void shouldRenderMapAggregationExpressionOnExpression() {
17061706
"{ $project:{ adjustedGrades:{ $map: { input: { $size : [\"foo\"]}, as: \"grade\",in: { $add: [ \"$$grade\", 2 ] }}}}}")));
17071707
}
17081708

1709-
private static DBObject exctractOperation(String field, DBObject fromProjectClause) {
1710-
return (DBObject) fromProjectClause.get(field);
1711-
}
1712-
17131709
/**
17141710
* @see DATAMONGO-861, DATAMONGO-1542
17151711
*/
@@ -1748,4 +1744,8 @@ public void fieldReplacementIfNullShouldRenderCorrectly() {
17481744

17491745
assertThat(agg, is(JSON.parse("{ $project: { result: { $ifNull: [ \"$optional\", \"$never-null\" ] } } }")));
17501746
}
1747+
1748+
private static DBObject exctractOperation(String field, DBObject fromProjectClause) {
1749+
return (DBObject) fromProjectClause.get(field);
1750+
}
17511751
}

0 commit comments

Comments
 (0)