11/*
2- * Copyright 2022 Björn Kautler
2+ * Copyright 2022-2025 Björn Kautler
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -74,18 +74,40 @@ public class CommandContext<M> {
7474 /**
7575 * Constructs a new command context from the given builder.
7676 *
77- * @param builder the message that triggered the command processing
77+ * @param builder the builder holding the configuration for the new command context
7878 */
7979 private CommandContext (Builder <M > builder ) {
80- message = requireNonNull (builder .message );
81- messageContent = requireNonNull (builder .messageContent );
80+ this (true , requireNonEmptyMessageAndContent (builder ));
81+ }
82+
83+ /**
84+ * Constructs a new command context from the given builder.
85+ *
86+ * @param parametersValidated a dummy parameter for finalizer attack prevention
87+ * @param builder the builder holding the configuration for the new command context
88+ */
89+ private CommandContext (boolean parametersValidated , Builder <M > builder ) {
90+ message = builder .message ;
91+ messageContent = builder .messageContent ;
8292 prefix = builder .prefix ;
8393 alias = builder .alias ;
8494 parameterString = builder .parameterString ;
8595 command = builder .command ;
8696 additionalData .putAll (builder .additionalData );
8797 }
8898
99+ /**
100+ * Ensures that the given builder has a non-null message and message content.
101+ *
102+ * @param builder the builder to be validated
103+ * @return the validated builder
104+ */
105+ private static <M > Builder <M > requireNonEmptyMessageAndContent (Builder <M > builder ) {
106+ requireNonNull (builder .message );
107+ requireNonNull (builder .messageContent );
108+ return builder ;
109+ }
110+
89111 /**
90112 * Returns the message that triggered the command processing.
91113 *
@@ -436,8 +458,19 @@ public static class Builder<M> {
436458 * @param messageContent the content of the message that triggered the command processing
437459 */
438460 public Builder (M message , String messageContent ) {
439- this .message = requireNonNull (message );
440- this .messageContent = requireNonNull (messageContent );
461+ this (true , requireNonNull (message ), requireNonNull (messageContent ));
462+ }
463+
464+ /**
465+ * Constructs a new command context builder with the given message and message content.
466+ *
467+ * @param parametersValidated a dummy parameter for finalizer attack prevention
468+ * @param message the message that triggered the command processing
469+ * @param messageContent the content of the message that triggered the command processing
470+ */
471+ private Builder (boolean parametersValidated , M message , String messageContent ) {
472+ this .message = message ;
473+ this .messageContent = messageContent ;
441474 }
442475
443476 /**
@@ -446,15 +479,38 @@ public Builder(M message, String messageContent) {
446479 * @param commandContext the command context used to initialize the builder
447480 */
448481 private Builder (CommandContext <M > commandContext ) {
449- message = requireNonNull (commandContext .message );
450- messageContent = requireNonNull (commandContext .messageContent );
482+ this (true , requireNonEmptyMessageAndContentAndAdditionalData (commandContext ));
483+ }
484+
485+ /**
486+ * Constructs a new command context builder with the same values as the given command context.
487+ *
488+ * @param parametersValidated a dummy parameter for finalizer attack prevention
489+ * @param commandContext the command context used to initialize the builder
490+ */
491+ private Builder (boolean parametersValidated , CommandContext <M > commandContext ) {
492+ message = commandContext .message ;
493+ messageContent = commandContext .messageContent ;
451494 prefix = commandContext .prefix ;
452495 alias = commandContext .alias ;
453496 parameterString = commandContext .parameterString ;
454497 command = commandContext .command ;
498+ additionalData .putAll (commandContext .additionalData );
499+ }
500+
501+ /**
502+ * Ensures that the given command context has a non-null message and message content, and that all
503+ * additional data keys and values are non-null.
504+ *
505+ * @param commandContext the command context to be validated
506+ * @return the validated command context
507+ */
508+ private static <M > CommandContext <M > requireNonEmptyMessageAndContentAndAdditionalData (CommandContext <M > commandContext ) {
509+ requireNonNull (commandContext .message );
510+ requireNonNull (commandContext .messageContent );
455511 commandContext .additionalData .keySet ().forEach (Objects ::requireNonNull );
456512 commandContext .additionalData .values ().forEach (Objects ::requireNonNull );
457- additionalData . putAll ( commandContext . additionalData ) ;
513+ return commandContext ;
458514 }
459515
460516 /**
0 commit comments