3636 * <a href="https://docs.mongodb.com/master/reference/configuration-options/#security.javascriptEnabled">enabled</a>.
3737 *
3838 * @author Christoph Strobl
39+ * @author Mark Paluch
3940 * @since 3.1
4041 */
4142public class ScriptOperators {
@@ -83,7 +84,6 @@ public static AccumulatorInitBuilder accumulatorBuilder() {
8384 *
8485 * @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/function/">MongoDB Documentation:
8586 * $function</a>
86- * @since 3.1
8787 */
8888 public static class Function extends AbstractAggregationExpression {
8989
@@ -99,6 +99,8 @@ private Function(Map<String, Object> values) {
9999 */
100100 public static Function function (String body ) {
101101
102+ Assert .notNull (body , "Function body must not be null!" );
103+
102104 Map <String , Object > function = new LinkedHashMap <>(2 );
103105 function .put (Fields .BODY .toString (), body );
104106 function .put (Fields .ARGS .toString (), Collections .emptyList ());
@@ -126,6 +128,7 @@ public Function args(Object... args) {
126128 public Function args (List <Object > args ) {
127129
128130 Assert .notNull (args , "Args must not be null! Use an empty list instead." );
131+
129132 return new Function (appendAt (1 , Fields .ARGS .toString (), args ));
130133 }
131134
@@ -137,7 +140,8 @@ public Function args(List<Object> args) {
137140 */
138141 public Function lang (String lang ) {
139142
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+
141145 return new Function (appendAt (2 , Fields .LANG .toString (), lang ));
142146 }
143147
@@ -198,7 +202,6 @@ public String toString() {
198202 *
199203 * @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/accumulator/">MongoDB Documentation:
200204 * $accumulator</a>
201- * @since 3.1
202205 */
203206 public static class Accumulator extends AbstractAggregationExpression {
204207
@@ -293,10 +296,10 @@ default AccumulatorAccumulateBuilder initArgs(Object... args) {
293296 /**
294297 * Define the optional {@code initArgs} for the {@link AccumulatorInitBuilder#init(String)} function.
295298 *
296- * @param args can be {@literal null}.
299+ * @param args must not be {@literal null}.
297300 * @return this.
298301 */
299- AccumulatorAccumulateBuilder initArgs (@ Nullable List <Object > args );
302+ AccumulatorAccumulateBuilder initArgs (List <Object > args );
300303 }
301304
302305 public interface AccumulatorAccumulateBuilder {
@@ -355,10 +358,10 @@ default AccumulatorMergeBuilder accumulateArgs(Object... args) {
355358 * Define additional {@code accumulateArgs} for the {@link AccumulatorAccumulateBuilder#accumulate(String)}
356359 * function.
357360 *
358- * @param args can be {@literal null}.
361+ * @param args must not be {@literal null}.
359362 * @return this.
360363 */
361- AccumulatorMergeBuilder accumulateArgs (@ Nullable List <Object > args );
364+ AccumulatorMergeBuilder accumulateArgs (List <Object > args );
362365 }
363366
364367 public interface AccumulatorMergeBuilder {
@@ -398,9 +401,16 @@ public interface AccumulatorFinalizeBuilder {
398401 * @return new instance of {@link Accumulator}.
399402 */
400403 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 ();
401411 }
402412
403- public static class AccumulatorBuilder
413+ static class AccumulatorBuilder
404414 implements AccumulatorInitBuilder , AccumulatorInitArgsBuilder , AccumulatorAccumulateBuilder ,
405415 AccumulatorAccumulateArgsBuilder , AccumulatorMergeBuilder , AccumulatorFinalizeBuilder {
406416
@@ -426,6 +436,7 @@ public static class AccumulatorBuilder
426436 * @param function must not be {@literal null}.
427437 * @return this.
428438 */
439+ @ Override
429440 public AccumulatorBuilder init (String function ) {
430441
431442 this .initFunction = function ;
@@ -435,12 +446,15 @@ public AccumulatorBuilder init(String function) {
435446 /**
436447 * Define the optional {@code initArgs} for the {@link #init(String)} function.
437448 *
438- * @param args can be {@literal null}.
449+ * @param function must not be {@literal null}.
439450 * @return this.
440451 */
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" );
442456
443- this .initArgs = args != null ? new ArrayList <>(args ) : Collections . emptyList ( );
457+ this .initArgs = new ArrayList <>(args );
444458 return this ;
445459 }
446460
@@ -458,21 +472,27 @@ public AccumulatorBuilder initArgs(@Nullable List<Object> args) {
458472 * @param function must not be {@literal null}.
459473 * @return this.
460474 */
475+ @ Override
461476 public AccumulatorBuilder accumulate (String function ) {
462477
478+ Assert .notNull (function , "Accumulate function must not be null" );
479+
463480 this .accumulateFunction = function ;
464481 return this ;
465482 }
466483
467484 /**
468485 * Define additional {@code accumulateArgs} for the {@link #accumulate(String)} function.
469486 *
470- * @param args can be {@literal null}.
487+ * @param args must not be {@literal null}.
471488 * @return this.
472489 */
473- public AccumulatorBuilder accumulateArgs (@ Nullable List <Object > args ) {
490+ @ Override
491+ public AccumulatorBuilder accumulateArgs (List <Object > args ) {
474492
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 );
476496 return this ;
477497 }
478498
@@ -491,8 +511,11 @@ public AccumulatorBuilder accumulateArgs(@Nullable List<Object> args) {
491511 * @param function must not be {@literal null}.
492512 * @return this.
493513 */
514+ @ Override
494515 public AccumulatorBuilder merge (String function ) {
495516
517+ Assert .notNull (function , "Merge function must not be null" );
518+
496519 this .mergeFunction = function ;
497520 return this ;
498521 }
@@ -505,6 +528,8 @@ public AccumulatorBuilder merge(String function) {
505528 */
506529 public AccumulatorBuilder lang (String lang ) {
507530
531+ Assert .hasText (lang , "Lang must not be null nor empty! The default would be 'js'." );
532+
508533 this .lang = lang ;
509534 return this ;
510535 }
@@ -523,10 +548,26 @@ public AccumulatorBuilder lang(String lang) {
523548 * @param function must not be {@literal null}.
524549 * @return new instance of {@link Accumulator}.
525550 */
551+ @ Override
526552 public Accumulator finalize (String function ) {
527553
554+ Assert .notNull (function , "Finalize function must not be null" );
555+
528556 this .finalizeFunction = function ;
529557
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+
530571 Map <String , Object > args = new LinkedHashMap <>();
531572 args .put (Fields .INIT .toString (), initFunction );
532573 if (!CollectionUtils .isEmpty (initArgs )) {
@@ -537,12 +578,10 @@ public Accumulator finalize(String function) {
537578 args .put (Fields .ACCUMULATE_ARGS .toString (), accumulateArgs );
538579 }
539580 args .put (Fields .MERGE .toString (), mergeFunction );
540- args .put (Fields .FINALIZE .toString (), finalizeFunction );
541581 args .put (Fields .LANG .toString (), lang );
542582
543- return new Accumulator ( args ) ;
583+ return args ;
544584 }
545-
546585 }
547586 }
548587}
0 commit comments