Skip to content

Commit 248bcfa

Browse files
committed
Polishing.
Simplify code. Original pull request: #4059. See #4038
1 parent ee076ec commit 248bcfa

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

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

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

18-
import java.util.Map;
19-
2018
import org.bson.Document;
2119
import org.bson.conversions.Bson;
20+
2221
import org.springframework.data.mongodb.util.BsonUtils;
2322
import org.springframework.util.ObjectUtils;
2423

2524
/**
26-
* {@link AggregationOperation} implementation that c
27-
*
25+
* {@link AggregationOperation} implementation that can return a {@link Document} from a {@link Bson} or {@link String}
26+
* document.
27+
*
2828
* @author Christoph Strobl
2929
* @since 4.0
3030
*/
31-
class BasicAggregationOperation implements AggregationOperation {
32-
33-
private final Object value;
34-
35-
BasicAggregationOperation(Object value) {
36-
this.value = value;
37-
}
31+
record BasicAggregationOperation(Object value) implements AggregationOperation {
3832

3933
@Override
4034
public Document toDocument(AggregationOperationContext context) {
4135

42-
if (value instanceof Document document) {
43-
return document;
44-
}
45-
4636
if (value instanceof Bson bson) {
4737
return BsonUtils.asDocument(bson, context.getCodecRegistry());
4838
}
4939

50-
if (value instanceof Map map) {
51-
return new Document(map);
52-
}
53-
5440
if (value instanceof String json && BsonUtils.isJsonDocument(json)) {
5541
return BsonUtils.parse(json, context);
5642
}
5743

5844
throw new IllegalStateException(
59-
String.format("%s cannot be converted to org.bson.Document.", ObjectUtils.nullSafeClassName(value)));
45+
String.format("%s cannot be converted to org.bson.Document", ObjectUtils.nullSafeClassName(value)));
6046
}
6147
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static Map<String, Object> asMap(Bson bson) {
8989
* Return the {@link Bson} object as {@link Map}. Depending on the input type, the return value can be either a casted
9090
* version of {@code bson} or a converted (detached from the original value) using the given {@link CodecRegistry} to
9191
* obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
92-
*
92+
*
9393
* @param bson can be {@literal null}.
9494
* @param codecRegistry must not be {@literal null}.
9595
* @return never {@literal null}. Returns an empty {@link Map} if input {@link Bson} is {@literal null}.
@@ -138,10 +138,6 @@ public static Document asDocument(Bson bson) {
138138
*/
139139
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
140140

141-
if (bson instanceof Document document) {
142-
return document;
143-
}
144-
145141
Map<String, Object> map = asMap(bson, codecRegistry);
146142

147143
if (map instanceof Document) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import com.mongodb.MongoClientSettings;
3535

3636
/**
37+
* Unit tests for {@link BasicAggregationOperation}.
38+
*
3739
* @author Christoph Strobl
3840
*/
3941
@ExtendWith(MockitoExtension.class)

0 commit comments

Comments
 (0)