Skip to content

Commit 34d66a2

Browse files
committed
Polishing.
Add license headers. Update Javadoc, author, and since tags. Add tests. Add toCriteriaDefinition method. See #3790
1 parent e71ec87 commit 34d66a2

File tree

8 files changed

+156
-73
lines changed

8 files changed

+156
-73
lines changed

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,18 @@ public static MatchOperation match(Criteria criteria) {
498498
public static MatchOperation match(CriteriaDefinition criteria) {
499499
return new MatchOperation(criteria);
500500
}
501-
501+
502502
/**
503-
* Creates a new {@link MatchOperation}
503+
* Creates a new {@link MatchOperation} using the given {@link AggregationExpression}.
504504
*
505+
* @param expression must not be {@literal null}.
505506
* @return new instance of {@link MatchOperation}.
506-
* @since 1.10
507+
* @since 3.3
507508
*/
508-
public static MatchOperation match() {
509-
return new MatchOperation();
509+
public static MatchOperation match(AggregationExpression expression) {
510+
return new MatchOperation(expression);
510511
}
511-
512+
512513
/**
513514
* Creates a new {@link GeoNearOperation} instance from the given {@link NearQuery} and the {@code distanceField}. The
514515
* {@code distanceField} defines output field that contains the calculated distance.

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

+56-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.mongodb.core.aggregation;
217

18+
import org.bson.Document;
19+
20+
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
321
import org.springframework.util.Assert;
422

23+
/**
24+
* Gateway to {@literal evaluation operators} such as {@literal $expr}.
25+
*
26+
* @author Divya Srivastava
27+
* @since 3.3
28+
*/
529
public class EvaluationOperators {
6-
30+
731
/**
832
* Take the value resulting from the given fieldReference.
933
*
@@ -13,7 +37,7 @@ public class EvaluationOperators {
1337
public static EvaluationOperatorFactory valueOf(String fieldReference) {
1438
return new EvaluationOperatorFactory(fieldReference);
1539
}
16-
40+
1741
/**
1842
* Take the value resulting from the given {@link AggregationExpression}.
1943
*
@@ -23,12 +47,12 @@ public static EvaluationOperatorFactory valueOf(String fieldReference) {
2347
public static EvaluationOperatorFactory valueOf(AggregationExpression expression) {
2448
return new EvaluationOperatorFactory(expression);
2549
}
26-
50+
2751
public static class EvaluationOperatorFactory {
28-
52+
2953
private final String fieldReference;
3054
private final AggregationExpression expression;
31-
55+
3256
/**
3357
* Creates new {@link EvaluationOperatorFactory} for given {@literal fieldReference}.
3458
*
@@ -41,7 +65,6 @@ public EvaluationOperatorFactory(String fieldReference) {
4165
this.expression = null;
4266
}
4367

44-
4568
/**
4669
* Creates new {@link EvaluationOperatorFactory} for given {@link AggregationExpression}.
4770
*
@@ -53,7 +76,7 @@ public EvaluationOperatorFactory(AggregationExpression expression) {
5376
this.fieldReference = null;
5477
this.expression = expression;
5578
}
56-
79+
5780
/**
5881
* Creates new {@link AggregationExpression} that is a valid aggregation expression.
5982
*
@@ -62,8 +85,10 @@ public EvaluationOperatorFactory(AggregationExpression expression) {
6285
public Expr expr() {
6386
return usesFieldRef() ? Expr.valueOf(fieldReference) : Expr.valueOf(expression);
6487
}
65-
66-
88+
89+
/**
90+
* Allows the use of aggregation expressions within the query language.
91+
*/
6792
public static class Expr extends AbstractAggregationExpression {
6893

6994
private Expr(Object value) {
@@ -99,8 +124,29 @@ public static Expr valueOf(AggregationExpression expression) {
99124
return new Expr(expression);
100125
}
101126

127+
/**
128+
* Creates {@code $expr} as {@link CriteriaDefinition}.
129+
*
130+
* @return the {@link CriteriaDefinition} from this expression.
131+
*/
132+
public CriteriaDefinition toCriteriaDefinition(AggregationOperationContext context) {
133+
134+
Document criteriaObject = toDocument(context);
135+
136+
return new CriteriaDefinition() {
137+
@Override
138+
public Document getCriteriaObject() {
139+
return criteriaObject;
140+
}
141+
142+
@Override
143+
public String getKey() {
144+
return getMongoMethod();
145+
}
146+
};
147+
}
102148
}
103-
149+
104150
private boolean usesFieldRef() {
105151
return fieldReference != null;
106152
}

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

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

1818
import org.bson.Document;
19-
import org.springframework.data.mongodb.core.aggregation.EvaluationOperators.EvaluationOperatorFactory.Expr;
19+
2020
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
2121
import org.springframework.util.Assert;
2222

@@ -30,6 +30,7 @@
3030
* @author Sebastian Herold
3131
* @author Thomas Darimont
3232
* @author Oliver Gierke
33+
* @author Divya Srivastava
3334
* @since 1.3
3435
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/">MongoDB Aggregation Framework:
3536
* $match</a>
@@ -38,15 +39,7 @@ public class MatchOperation implements AggregationOperation {
3839

3940
private final CriteriaDefinition criteriaDefinition;
4041
private final AggregationExpression expression;
41-
42-
/**
43-
* Creates a new {@link MatchOperation}
44-
*/
45-
public MatchOperation() {
46-
this.criteriaDefinition = null;
47-
this.expression = null;
48-
}
49-
42+
5043
/**
5144
* Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}.
5245
*
@@ -55,41 +48,34 @@ public MatchOperation() {
5548
public MatchOperation(CriteriaDefinition criteriaDefinition) {
5649

5750
Assert.notNull(criteriaDefinition, "Criteria must not be null!");
51+
5852
this.criteriaDefinition = criteriaDefinition;
5953
this.expression = null;
6054
}
61-
62-
/**
63-
* Creates a new {@link MatchOperation} for the given {@link Expression}.
64-
*
65-
* @param criteriaDefinition must not be {@literal null}.
66-
*/
67-
private MatchOperation(Expr expression) {
68-
Assert.notNull(expression, "Expression must not be null!");
69-
this.criteriaDefinition = null;
70-
this.expression = expression;
71-
}
72-
55+
7356
/**
7457
* Creates a new {@link MatchOperation} for the given {@link AggregationExpression}.
7558
*
7659
* @param expression must not be {@literal null}.
60+
* @since 3.3
7761
*/
78-
public MatchOperation withValueOf(AggregationExpression expression) {
62+
public MatchOperation(AggregationExpression expression) {
63+
7964
Assert.notNull(expression, "Expression must not be null!");
80-
return new MatchOperation(EvaluationOperators.valueOf(expression).expr());
65+
66+
this.criteriaDefinition = null;
67+
this.expression = expression;
8168
}
82-
69+
8370
/*
8471
* (non-Javadoc)
8572
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
8673
*/
8774
@Override
8875
public Document toDocument(AggregationOperationContext context) {
89-
if(expression != null) {
90-
return new Document(getOperator(), expression.toDocument());
91-
}
92-
return new Document(getOperator(), context.getMappedObject(criteriaDefinition.getCriteriaObject()));
76+
77+
return new Document(getOperator(),
78+
context.getMappedObject(expression != null ? expression.toDocument() : criteriaDefinition.getCriteriaObject()));
9379
}
9480

9581
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.springframework.data.mongodb.test.util.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* Unit tests for {@link EvaluationOperators}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
class EvaluationOperatorsUnitTests {
28+
29+
@Test // GH-3790
30+
void shouldRenderExprCorrectly() {
31+
32+
assertThat(EvaluationOperators.valueOf("foo").expr().toDocument(Aggregation.DEFAULT_CONTEXT))
33+
.isEqualTo("{ $expr: \"$foo\" }");
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
package org.springframework.data.mongodb.core.aggregation;
22

3-
import static org.assertj.core.api.Assertions.*;
43

5-
import org.bson.Document;
4+
import static org.springframework.data.mongodb.test.util.Assertions.*;
5+
66
import org.junit.jupiter.api.Test;
77

8+
/**
9+
* Unit tests for {@link MatchOperation}.
10+
*
11+
* @author Divya Srivastava
12+
*/
813
class MatchOperationUnitTests {
9-
10-
@Test // DATAMONGO - 3729
11-
public void shouldRenderStdDevPopCorrectly() {
12-
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevPop());
13-
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)).
14-
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevPop\" : \"$quiz\" } } } "));
15-
16-
}
17-
18-
@Test // DATAMONGO - 3729
19-
public void shouldRenderStdDevSampCorrectly() {
20-
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevSamp());
14+
15+
@Test // GH-3790
16+
void matchShouldRenderCorrectly() {
17+
18+
MatchOperation operation = Aggregation.match(ArithmeticOperators.valueOf("quiz").stdDevPop());
2119
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)).
22-
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevSamp\" : \"$quiz\" } } } "));
23-
20+
isEqualTo("{ $match: { \"$stdDevPop\" : \"$quiz\" } } ");
2421
}
2522

2623
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
*
2828
* @author Mark Paluch
2929
*/
30-
public class ReplaceRootOperationUnitTests {
30+
class ReplaceRootOperationUnitTests {
3131

3232
@Test // DATAMONGO-1550
33-
public void rejectsNullField() {
33+
void rejectsNullField() {
3434
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((Field) null));
3535
}
3636

3737
@Test // DATAMONGO-1550
38-
public void rejectsNullExpression() {
38+
void rejectsNullExpression() {
3939
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((AggregationExpression) null));
4040
}
4141

4242
@Test // DATAMONGO-1550
43-
public void shouldRenderCorrectly() {
43+
void shouldRenderCorrectly() {
4444

4545
ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder()
4646
.withDocument(new Document("hello", "world"));
@@ -50,7 +50,7 @@ public void shouldRenderCorrectly() {
5050
}
5151

5252
@Test // DATAMONGO-1550
53-
public void shouldRenderExpressionCorrectly() {
53+
void shouldRenderExpressionCorrectly() {
5454

5555
ReplaceRootOperation operation = new ReplaceRootOperation(VariableOperators //
5656
.mapItemsOf("array") //
@@ -64,7 +64,7 @@ public void shouldRenderExpressionCorrectly() {
6464
}
6565

6666
@Test // DATAMONGO-1550
67-
public void shouldComposeDocument() {
67+
void shouldComposeDocument() {
6868

6969
ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder().withDocument() //
7070
.andValue("value").as("key") //
@@ -77,7 +77,7 @@ public void shouldComposeDocument() {
7777
}
7878

7979
@Test // DATAMONGO-1550
80-
public void shouldComposeSubDocument() {
80+
void shouldComposeSubDocument() {
8181

8282
Document partialReplacement = new Document("key", "override").append("key2", "value2");
8383

@@ -92,7 +92,7 @@ public void shouldComposeSubDocument() {
9292
}
9393

9494
@Test // DATAMONGO-1550
95-
public void shouldNotExposeFields() {
95+
void shouldNotExposeFields() {
9696

9797
ReplaceRootOperation operation = new ReplaceRootOperation(Fields.field("field"));
9898

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
*
2626
* @author Christoph Strobl
2727
*/
28-
public class ReplaceWithOperationUnitTests {
28+
class ReplaceWithOperationUnitTests {
2929

3030
@Test // DATAMONGO-2331
31-
public void rejectsNullField() {
31+
void rejectsNullField() {
3232
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceWithOperation(null));
3333
}
3434

3535
@Test // DATAMONGO-2331
36-
public void shouldRenderValueCorrectly() {
36+
void shouldRenderValueCorrectly() {
3737

3838
ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValue(new Document("hello", "world"));
3939
Document dbObject = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
@@ -42,7 +42,7 @@ public void shouldRenderValueCorrectly() {
4242
}
4343

4444
@Test // DATAMONGO-2331
45-
public void shouldRenderExpressionCorrectly() {
45+
void shouldRenderExpressionCorrectly() {
4646

4747
ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValueOf(VariableOperators //
4848
.mapItemsOf("array") //

0 commit comments

Comments
 (0)