Class CommandContext<M>

  • Type Parameters:
    M - the class of the messages for which this command context is used

    public class CommandContext<M>
    extends Object
    The context of a command triggered by messages of the given type. At various places in the processing some fields might be set and others not.

    This class is thread-safe as it is immutable and the additional data is stored in a thread-safe container. Any method that modifies a value - except for additional data - produces a new instance.

    • Method Detail

      • getMessage

        public M getMessage()
        Returns the message that triggered the command processing.
        Returns:
        the message that triggered the command processing
      • withMessage

        public CommandContext.Builder<M> withMessage​(M message)
        Returns a builder that will create a new command context based on this instance with the given message.
        Parameters:
        message - the message that triggered the command processing
        Returns:
        a builder that will create a new command context based on this instance with the given message
      • getMessageContent

        public String getMessageContent()
        Returns the content of the message that triggered the command processing.
        Returns:
        the content of the message that triggered the command processing
      • withMessageContent

        public CommandContext.Builder<M> withMessageContent​(String messageContent)
        Returns a builder that will create a new command context based on this instance with the given message content.
        Parameters:
        messageContent - the content of the message that triggered the command processing
        Returns:
        a builder that will create a new command context based on this instance with the given message content
      • getPrefix

        public Optional<String> getPrefix()
        Returns the prefix that the message has to start with to trigger a command.
        Returns:
        the prefix that the message has to start with to trigger a command
      • withPrefix

        public CommandContext.Builder<M> withPrefix​(String prefix)
        Returns a builder that will create a new command context based on this instance with the given prefix.
        Parameters:
        prefix - the prefix that the message has to start with to trigger a command
        Returns:
        a builder that will create a new command context based on this instance with the given prefix
      • getAlias

        public Optional<String> getAlias()
        Returns the alias of the triggered command.
        Returns:
        the alias of the triggered command
      • withAlias

        public CommandContext.Builder<M> withAlias​(String alias)
        Returns a builder that will create a new command context based on this instance with the given alias.
        Parameters:
        alias - the alias of the triggered command
        Returns:
        a builder that will create a new command context based on this instance with the given alias
      • getParameterString

        public Optional<String> getParameterString()
        Returns the parameter string to be processed by the triggered command.
        Returns:
        the parameter string to be processed by the triggered command
      • withParameterString

        public CommandContext.Builder<M> withParameterString​(String parameterString)
        Returns a builder that will create a new command context based on this instance with the given parameter string.
        Parameters:
        parameterString - the parameter string to be processed by the triggered command
        Returns:
        a builder that will create a new command context based on this instance with the given parameter string
      • getCommand

        public Optional<Command<? super M>> getCommand()
        Returns the command that is triggered.
        Returns:
        the command that is triggered
      • withCommand

        public CommandContext.Builder<M> withCommand​(Command<? super M> command)
        Returns a builder that will create a new command context based on this instance with the given command.
        Parameters:
        command - the command that is triggered
        Returns:
        a builder that will create a new command context based on this instance with the given command
      • getAdditionalData

        public <R> Optional<R> getAdditionalData​(String key)
        Returns the additional data value to which the specified key is mapped with an optional implicit downcast, or an empty Optional if this command context contains no additional data for the key.

        The returned Optional can implicitly be downcasted by using CommandContext to define the type using an explicit type parameter like with

        
         commandContext.<User>getAdditionalData("user");
         
        or using implicit type inference like with
        
         Optional<User> user = commandContext.getAdditionalData("user");
         

        Warning: Be aware that choosing R must be done wisely as it is an unsafe operation. If you for example select String for R and then try to get a User object from the returned optional, you will get a ClassCastException at runtime.

        Type Parameters:
        R - the class to which the value is implicitly downcasted
        Parameters:
        key - the additional data key whose associated value is to be returned
        Returns:
        the value to which the specified additional data key is mapped
      • getAdditionalData

        public <R> R getAdditionalData​(String key,
                                       R defaultValue)
        Returns the additional data value to which the specified key is mapped with an optional implicit downcast, or the given default value if this command context contains no additional data for the key.

        The returned value can implicitly be downcasted by using CommandContext to define the type using an explicit type parameter like with

        
         commandContext.<User>getAdditionalData("user", new UserSubClass());
         
        or using implicit type inference like with
        
         User user = commandContext.getAdditionalData("user", (User) null);
         
        or
        
         User defaultUser = ...;
         commandContext.getAdditionalData("user", defaultUser);
         

        Warning: Be aware that choosing R must be done wisely as it is an unsafe operation. If you for example select String for R and then try to get a User object, you will get a ClassCastException at runtime.

        Type Parameters:
        R - the class to which the value is implicitly downcasted
        Parameters:
        key - the additional data key whose associated value is to be returned
        defaultValue - the default value to return if there is no mapping
        Returns:
        the value to which the specified additional data key is mapped or the default value
      • getAdditionalData

        public <R> R getAdditionalData​(String key,
                                       Supplier<R> defaultValueSupplier)
        Returns the additional data value to which the specified key is mapped with an optional implicit downcast, or a default value returned by the given supplier if this command context contains no additional data for the key.

        The returned value can implicitly be downcasted by using CommandContext to define the type using an explicit type parameter like with

        
         commandContext.<User>getAdditionalData("user", () -> null);
         
        or using implicit type inference like with
        
         User user = commandContext.getAdditionalData("user", () -> null);
         
        or
        
         User defaultUser = ...;
         commandContext.getAdditionalData("user", () -> defaultUser);
         

        Warning: Be aware that choosing R must be done wisely as it is an unsafe operation. If you for example select String for R and then try to get a User object, you will get a ClassCastException at runtime.

        Type Parameters:
        R - the class to which the value is implicitly downcasted
        Parameters:
        key - the additional data key whose associated value is to be returned
        defaultValueSupplier - the supplier for the default value to return if there is no mapping
        Returns:
        the value to which the specified additional data key is mapped or the computed default value
      • setAdditionalData

        public <R> Optional<R> setAdditionalData​(String key,
                                                 Object value)
        Sets the additional data for the given key to the given value. If the additional data key was mapped to a value previously, the old value is returned, otherwise an empty Optional is returned.

        The returned value can implicitly be downcasted by using CommandContext to define the type using an explicit type parameter like with

        
         commandContext.<User>setAdditionalData("user", user);
         
        or using implicit type inference like with
        
         Optional<User> user = commandContext.setAdditionalData("user", user);
         

        Warning: Be aware that choosing R must be done wisely as it is an unsafe operation. If you for example select String for R and then try to get a User object, you will get a ClassCastException at runtime.

        Type Parameters:
        R - the class to which the returned old value is implicitly downcasted
        Parameters:
        key - the additional data key whose value is to be set
        value - the value to set for the given key
        Returns:
        the value to which the specified additional data key was mapped previously or an empty Optional
      • removeAdditionalData

        public <R> Optional<R> removeAdditionalData​(String key)
        Removes the additional data for the given key. If the additional data key was mapped to a value previously, the removed value is returned, otherwise an empty Optional is returned.

        The returned value can implicitly be downcasted by using CommandContext to define the type using an explicit type parameter like with

        
         commandContext.<User>removeAdditionalData("user");
         
        or using implicit type inference like with
        
         Optional<User> user = commandContext.removeAdditionalData("user");
         

        Warning: Be aware that choosing R must be done wisely as it is an unsafe operation. If you for example select String for R and then try to get a User object, you will get a ClassCastException at runtime.

        Type Parameters:
        R - the class to which the returned value is implicitly downcasted
        Parameters:
        key - the additional data key whose value is to be removed
        Returns:
        the value to which the specified additional data key was mapped previously or an empty Optional
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object