36
36
* <a href="https://docs.mongodb.com/master/reference/configuration-options/#security.javascriptEnabled">enabled</a>.
37
37
*
38
38
* @author Christoph Strobl
39
+ * @author Mark Paluch
39
40
* @since 3.1
40
41
*/
41
42
public class ScriptOperators {
@@ -83,7 +84,6 @@ public static AccumulatorInitBuilder accumulatorBuilder() {
83
84
*
84
85
* @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/function/">MongoDB Documentation:
85
86
* $function</a>
86
- * @since 3.1
87
87
*/
88
88
public static class Function extends AbstractAggregationExpression {
89
89
@@ -99,6 +99,8 @@ private Function(Map<String, Object> values) {
99
99
*/
100
100
public static Function function (String body ) {
101
101
102
+ Assert .notNull (body , "Function body must not be null!" );
103
+
102
104
Map <String , Object > function = new LinkedHashMap <>(2 );
103
105
function .put (Fields .BODY .toString (), body );
104
106
function .put (Fields .ARGS .toString (), Collections .emptyList ());
@@ -126,6 +128,7 @@ public Function args(Object... args) {
126
128
public Function args (List <Object > args ) {
127
129
128
130
Assert .notNull (args , "Args must not be null! Use an empty list instead." );
131
+
129
132
return new Function (appendAt (1 , Fields .ARGS .toString (), args ));
130
133
}
131
134
@@ -137,7 +140,8 @@ public Function args(List<Object> args) {
137
140
*/
138
141
public Function lang (String lang ) {
139
142
140
- Assert .hasText (lang , "Lang must not be null nor emtpy! The default would be 'js'." );
143
+ Assert .hasText (lang , "Lang must not be null nor empty! The default would be 'js'." );
144
+
141
145
return new Function (appendAt (2 , Fields .LANG .toString (), lang ));
142
146
}
143
147
@@ -198,7 +202,6 @@ public String toString() {
198
202
*
199
203
* @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/accumulator/">MongoDB Documentation:
200
204
* $accumulator</a>
201
- * @since 3.1
202
205
*/
203
206
public static class Accumulator extends AbstractAggregationExpression {
204
207
@@ -293,10 +296,10 @@ default AccumulatorAccumulateBuilder initArgs(Object... args) {
293
296
/**
294
297
* Define the optional {@code initArgs} for the {@link AccumulatorInitBuilder#init(String)} function.
295
298
*
296
- * @param args can be {@literal null}.
299
+ * @param args must not be {@literal null}.
297
300
* @return this.
298
301
*/
299
- AccumulatorAccumulateBuilder initArgs (@ Nullable List <Object > args );
302
+ AccumulatorAccumulateBuilder initArgs (List <Object > args );
300
303
}
301
304
302
305
public interface AccumulatorAccumulateBuilder {
@@ -355,10 +358,10 @@ default AccumulatorMergeBuilder accumulateArgs(Object... args) {
355
358
* Define additional {@code accumulateArgs} for the {@link AccumulatorAccumulateBuilder#accumulate(String)}
356
359
* function.
357
360
*
358
- * @param args can be {@literal null}.
361
+ * @param args must not be {@literal null}.
359
362
* @return this.
360
363
*/
361
- AccumulatorMergeBuilder accumulateArgs (@ Nullable List <Object > args );
364
+ AccumulatorMergeBuilder accumulateArgs (List <Object > args );
362
365
}
363
366
364
367
public interface AccumulatorMergeBuilder {
@@ -398,9 +401,16 @@ public interface AccumulatorFinalizeBuilder {
398
401
* @return new instance of {@link Accumulator}.
399
402
*/
400
403
Accumulator finalize (String function );
404
+
405
+ /**
406
+ * Build the {@link Accumulator} object without specifying a {@link #finalize(String) finalize function}.
407
+ *
408
+ * @return new instance of {@link Accumulator}.
409
+ */
410
+ Accumulator build ();
401
411
}
402
412
403
- public static class AccumulatorBuilder
413
+ static class AccumulatorBuilder
404
414
implements AccumulatorInitBuilder , AccumulatorInitArgsBuilder , AccumulatorAccumulateBuilder ,
405
415
AccumulatorAccumulateArgsBuilder , AccumulatorMergeBuilder , AccumulatorFinalizeBuilder {
406
416
@@ -426,6 +436,7 @@ public static class AccumulatorBuilder
426
436
* @param function must not be {@literal null}.
427
437
* @return this.
428
438
*/
439
+ @ Override
429
440
public AccumulatorBuilder init (String function ) {
430
441
431
442
this .initFunction = function ;
@@ -435,12 +446,15 @@ public AccumulatorBuilder init(String function) {
435
446
/**
436
447
* Define the optional {@code initArgs} for the {@link #init(String)} function.
437
448
*
438
- * @param args can be {@literal null}.
449
+ * @param function must not be {@literal null}.
439
450
* @return this.
440
451
*/
441
- public AccumulatorBuilder initArgs (@ Nullable List <Object > args ) {
452
+ @ Override
453
+ public AccumulatorBuilder initArgs (List <Object > args ) {
454
+
455
+ Assert .notNull (args , "Args must not be null" );
442
456
443
- this .initArgs = args != null ? new ArrayList <>(args ) : Collections . emptyList ( );
457
+ this .initArgs = new ArrayList <>(args );
444
458
return this ;
445
459
}
446
460
@@ -458,21 +472,27 @@ public AccumulatorBuilder initArgs(@Nullable List<Object> args) {
458
472
* @param function must not be {@literal null}.
459
473
* @return this.
460
474
*/
475
+ @ Override
461
476
public AccumulatorBuilder accumulate (String function ) {
462
477
478
+ Assert .notNull (function , "Accumulate function must not be null" );
479
+
463
480
this .accumulateFunction = function ;
464
481
return this ;
465
482
}
466
483
467
484
/**
468
485
* Define additional {@code accumulateArgs} for the {@link #accumulate(String)} function.
469
486
*
470
- * @param args can be {@literal null}.
487
+ * @param args must not be {@literal null}.
471
488
* @return this.
472
489
*/
473
- public AccumulatorBuilder accumulateArgs (@ Nullable List <Object > args ) {
490
+ @ Override
491
+ public AccumulatorBuilder accumulateArgs (List <Object > args ) {
474
492
475
- this .accumulateArgs = args != null ? new ArrayList <>(args ) : Collections .emptyList ();
493
+ Assert .notNull (args , "Args must not be null" );
494
+
495
+ this .accumulateArgs = new ArrayList <>(args );
476
496
return this ;
477
497
}
478
498
@@ -491,8 +511,11 @@ public AccumulatorBuilder accumulateArgs(@Nullable List<Object> args) {
491
511
* @param function must not be {@literal null}.
492
512
* @return this.
493
513
*/
514
+ @ Override
494
515
public AccumulatorBuilder merge (String function ) {
495
516
517
+ Assert .notNull (function , "Merge function must not be null" );
518
+
496
519
this .mergeFunction = function ;
497
520
return this ;
498
521
}
@@ -505,6 +528,8 @@ public AccumulatorBuilder merge(String function) {
505
528
*/
506
529
public AccumulatorBuilder lang (String lang ) {
507
530
531
+ Assert .hasText (lang , "Lang must not be null nor empty! The default would be 'js'." );
532
+
508
533
this .lang = lang ;
509
534
return this ;
510
535
}
@@ -523,10 +548,26 @@ public AccumulatorBuilder lang(String lang) {
523
548
* @param function must not be {@literal null}.
524
549
* @return new instance of {@link Accumulator}.
525
550
*/
551
+ @ Override
526
552
public Accumulator finalize (String function ) {
527
553
554
+ Assert .notNull (function , "Finalize function must not be null" );
555
+
528
556
this .finalizeFunction = function ;
529
557
558
+ Map <String , Object > args = createArgumentMap ();
559
+ args .put (Fields .FINALIZE .toString (), finalizeFunction );
560
+
561
+ return new Accumulator (args );
562
+ }
563
+
564
+ @ Override
565
+ public Accumulator build () {
566
+ return new Accumulator (createArgumentMap ());
567
+ }
568
+
569
+ private Map <String , Object > createArgumentMap () {
570
+
530
571
Map <String , Object > args = new LinkedHashMap <>();
531
572
args .put (Fields .INIT .toString (), initFunction );
532
573
if (!CollectionUtils .isEmpty (initArgs )) {
@@ -537,12 +578,10 @@ public Accumulator finalize(String function) {
537
578
args .put (Fields .ACCUMULATE_ARGS .toString (), accumulateArgs );
538
579
}
539
580
args .put (Fields .MERGE .toString (), mergeFunction );
540
- args .put (Fields .FINALIZE .toString (), finalizeFunction );
541
581
args .put (Fields .LANG .toString (), lang );
542
582
543
- return new Accumulator ( args ) ;
583
+ return args ;
544
584
}
545
-
546
585
}
547
586
}
548
587
}
0 commit comments