26
26
import org .springframework .util .Assert ;
27
27
28
28
/**
29
- * Encapsulates the aggregation framework {@code $replaceRoot}-operation.
30
- * <p>
29
+ * Encapsulates the aggregation framework {@code $replaceRoot}-operation. <br />
31
30
* We recommend to use the static factory method {@link Aggregation#replaceRoot(String)} instead of creating instances
32
31
* of this class directly.
33
- *
34
- * @see https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/#pipe._S_replaceRoot
32
+ *
33
+ * @see <a href=
34
+ * "https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/">https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/</a>
35
35
* @author Mark Paluch
36
+ * @author Christoph Strobl
36
37
* @since 1.10
37
38
*/
38
39
public class ReplaceRootOperation implements FieldsExposingAggregationOperation {
39
40
40
41
private final Replacement replacement ;
41
42
42
43
/**
43
- * Creates a new {@link ReplaceRootOperation} given the {@link as } field name.
44
+ * Creates a new {@link ReplaceRootOperation} given the {@link Field } field name.
44
45
*
45
46
* @param field must not be {@literal null} or empty.
46
47
*/
47
48
public ReplaceRootOperation (Field field ) {
48
- this . replacement = new FieldReplacement (field );
49
+ this ( new FieldReplacement (field ) );
49
50
}
50
51
51
52
/**
52
- * Creates a new {@link ReplaceRootOperation} given the {@link as} field name .
53
+ * Creates a new {@link ReplaceRootOperation} given the {@link AggregationExpression} pointing to a document .
53
54
*
54
55
* @param aggregationExpression must not be {@literal null}.
55
56
*/
56
57
public ReplaceRootOperation (AggregationExpression aggregationExpression ) {
57
-
58
- Assert .notNull (aggregationExpression , "AggregationExpression must not be null!" );
59
- this .replacement = new AggregationExpressionReplacement (aggregationExpression );
58
+ this (new AggregationExpressionReplacement (aggregationExpression ));
60
59
}
61
60
62
- protected ReplaceRootOperation (Replacement replacement ) {
61
+ /**
62
+ * Creates a new {@link ReplaceRootOperation} given the {@link Replacement}.
63
+ *
64
+ * @param replacement must not be {@literal null}.
65
+ */
66
+ public ReplaceRootOperation (Replacement replacement ) {
67
+
68
+ Assert .notNull (replacement , "Replacement must not be null!" );
63
69
this .replacement = replacement ;
64
70
}
65
71
66
72
/**
67
73
* Creates a new {@link ReplaceRootDocumentOperationBuilder}.
68
- *
74
+ *
69
75
* @return a new {@link ReplaceRootDocumentOperationBuilder}.
70
76
*/
71
77
public static ReplaceRootOperationBuilder builder () {
@@ -77,7 +83,7 @@ public static ReplaceRootOperationBuilder builder() {
77
83
*/
78
84
@ Override
79
85
public Document toDocument (AggregationOperationContext context ) {
80
- return new Document ("$replaceRoot" , new Document ("newRoot" , replacement .toObject (context )));
86
+ return new Document ("$replaceRoot" , new Document ("newRoot" , replacement .toDocumentExpression (context )));
81
87
}
82
88
83
89
/* (non-Javadoc)
@@ -97,7 +103,7 @@ public static class ReplaceRootOperationBuilder {
97
103
98
104
/**
99
105
* Defines a root document replacement based on a {@literal fieldName} that resolves to a document.
100
- *
106
+ *
101
107
* @param fieldName must not be {@literal null} or empty.
102
108
* @return the final {@link ReplaceRootOperation}.
103
109
*/
@@ -116,8 +122,7 @@ public ReplaceRootOperation withValueOf(AggregationExpression aggregationExpress
116
122
}
117
123
118
124
/**
119
- * Defines a root document replacement based on a composable document that is empty initially.
120
- * <p>
125
+ * Defines a root document replacement based on a composable document that is empty initially. <br />
121
126
* {@link ReplaceRootOperation} can be populated with individual entries and derive its values from other, existing
122
127
* documents.
123
128
*
@@ -128,8 +133,7 @@ public ReplaceRootDocumentOperation withDocument() {
128
133
}
129
134
130
135
/**
131
- * Defines a root document replacement based on a composable document given {@literal document}
132
- * <p>
136
+ * Defines a root document replacement based on a composable document given {@literal document}. <br />
133
137
* {@link ReplaceRootOperation} can be populated with individual entries and derive its values from other, existing
134
138
* documents.
135
139
*
@@ -146,8 +150,7 @@ public ReplaceRootOperation withDocument(Document document) {
146
150
147
151
/**
148
152
* Encapsulates the aggregation framework {@code $replaceRoot}-operation to result in a composable replacement
149
- * document.
150
- * <p>
153
+ * document. <br />
151
154
* Instances of {@link ReplaceRootDocumentOperation} yield empty upon construction and can be populated with single
152
155
* values and documents.
153
156
*
@@ -173,7 +176,7 @@ private ReplaceRootDocumentOperation(ReplacementDocument replacementDocument) {
173
176
/**
174
177
* Creates an extended {@link ReplaceRootDocumentOperation} that combines {@link ReplacementDocument}s from the
175
178
* {@literal currentOperation} and {@literal extension} operation.
176
- *
179
+ *
177
180
* @param currentOperation must not be {@literal null}.
178
181
* @param extension must not be {@literal null}.
179
182
*/
@@ -183,9 +186,9 @@ protected ReplaceRootDocumentOperation(ReplaceRootDocumentOperation currentOpera
183
186
}
184
187
185
188
/**
186
- * Creates a new {@link ReplaceRootDocumentOperationBuilder} to define a field for the {@link AggregationExpression}
187
- * .
188
- *
189
+ * Creates a new {@link ReplaceRootDocumentOperationBuilder} to define a field for the
190
+ * {@link AggregationExpression} .
191
+ *
189
192
* @param aggregationExpression must not be {@literal null}.
190
193
* @return the {@link ReplaceRootDocumentOperationBuilder}.
191
194
*/
@@ -249,47 +252,50 @@ public ReplaceRootDocumentOperation as(String fieldName) {
249
252
* Replacement object that results in a replacement document or an expression that results in a document.
250
253
*
251
254
* @author Mark Paluch
255
+ * @author Christoph Strobl
252
256
*/
253
- private abstract static class Replacement {
257
+ public interface Replacement {
254
258
255
259
/**
256
- * Renders the current {@link Replacement} into a {@link Document} based on the given
260
+ * Renders the current {@link Replacement} into a its MongoDB representation based on the given
257
261
* {@link AggregationOperationContext}.
258
262
*
259
263
* @param context will never be {@literal null}.
260
264
* @return a replacement document or an expression that results in a document.
261
265
*/
262
- public abstract Object toObject (AggregationOperationContext context );
266
+ Object toDocumentExpression (AggregationOperationContext context );
263
267
}
264
268
265
269
/**
266
270
* {@link Replacement} that uses a {@link AggregationExpression} that results in a replacement document.
267
271
*
268
272
* @author Mark Paluch
269
273
*/
270
- private static class AggregationExpressionReplacement extends Replacement {
274
+ private static class AggregationExpressionReplacement implements Replacement {
271
275
272
276
private final AggregationExpression aggregationExpression ;
273
277
274
278
protected AggregationExpressionReplacement (AggregationExpression aggregationExpression ) {
279
+
280
+ Assert .notNull (aggregationExpression , "AggregationExpression must not be null!" );
275
281
this .aggregationExpression = aggregationExpression ;
276
282
}
277
283
278
284
/* (non-Javadoc)
279
285
* @see org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.Replacement#toObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
280
286
*/
281
287
@ Override
282
- public Document toObject (AggregationOperationContext context ) {
288
+ public Document toDocumentExpression (AggregationOperationContext context ) {
283
289
return aggregationExpression .toDocument (context );
284
290
}
285
291
}
286
292
287
293
/**
288
294
* {@link Replacement that references a {@link Field} inside the current aggregation pipeline.
289
- *
295
+ *
290
296
* @author Mark Paluch
291
297
*/
292
- private static class FieldReplacement extends Replacement {
298
+ private static class FieldReplacement implements Replacement {
293
299
294
300
private final Field field ;
295
301
@@ -306,7 +312,7 @@ protected FieldReplacement(Field field) {
306
312
* @see org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.Replacement#toObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
307
313
*/
308
314
@ Override
309
- public Object toObject (AggregationOperationContext context ) {
315
+ public Object toDocumentExpression (AggregationOperationContext context ) {
310
316
return context .getReference (field ).toString ();
311
317
}
312
318
}
@@ -316,7 +322,7 @@ public Object toObject(AggregationOperationContext context) {
316
322
*
317
323
* @author Mark Paluch
318
324
*/
319
- private static class ReplacementDocument extends Replacement {
325
+ private static class ReplacementDocument implements Replacement {
320
326
321
327
private final Collection <ReplacementContributor > replacements ;
322
328
@@ -376,7 +382,7 @@ public static ReplacementDocument forSingleValue(String field, Object value) {
376
382
* @see org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.Replacement#toObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
377
383
*/
378
384
@ Override
379
- public Document toObject (AggregationOperationContext context ) {
385
+ public Document toDocumentExpression (AggregationOperationContext context ) {
380
386
381
387
Document document = new Document ();
382
388
@@ -401,7 +407,7 @@ public ReplacementDocument extendWith(ReplacementDocument extension) {
401
407
ReplacementDocument replacementDocument = new ReplacementDocument ();
402
408
403
409
List <ReplacementContributor > replacements = new ArrayList <ReplacementContributor >(
404
- this .replacements .size () + replacementDocument .replacements .size ());
410
+ this .replacements .size () + extension .replacements .size ());
405
411
406
412
replacements .addAll (this .replacements );
407
413
replacements .addAll (extension .replacements );
@@ -412,10 +418,10 @@ public ReplacementDocument extendWith(ReplacementDocument extension) {
412
418
413
419
/**
414
420
* Partial {@link Document} contributor for document replacement.
415
- *
421
+ *
416
422
* @author Mark Paluch
417
423
*/
418
- private abstract static class ReplacementContributor {
424
+ private interface ReplacementContributor extends AggregationExpression {
419
425
420
426
/**
421
427
* Renders the current {@link ReplacementContributor} into a {@link Document} based on the given
@@ -424,17 +430,17 @@ private abstract static class ReplacementContributor {
424
430
* @param context will never be {@literal null}.
425
431
* @return
426
432
*/
427
- public abstract Document toDocument (AggregationOperationContext context );
433
+ Document toDocument (AggregationOperationContext context );
428
434
}
429
435
430
436
/**
431
- * {@link ReplacementContributor} to contribute multiple fields based on the input {@literal value}.
432
- * <p>
437
+ * {@link ReplacementContributor} to contribute multiple fields based on the input {@literal value}. <br />
433
438
* The value object is mapped into a MongoDB {@link Document}.
434
- *
439
+ *
435
440
* @author Mark Paluch
441
+ * @author Christoph Strobl
436
442
*/
437
- private static class DocumentContributor extends ReplacementContributor {
443
+ private static class DocumentContributor implements ReplacementContributor {
438
444
439
445
private final Object value ;
440
446
@@ -467,7 +473,7 @@ public Document toDocument(AggregationOperationContext context) {
467
473
*
468
474
* @author Mark Paluch
469
475
*/
470
- private abstract static class FieldContributorSupport extends ReplacementContributor {
476
+ private abstract static class FieldContributorSupport implements ReplacementContributor {
471
477
472
478
private final ExposedField field ;
473
479
0 commit comments