Class CommandContext<M>
- java.lang.Object
-
- net.kautler.command.api.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCommandContext.Builder<M>A builder to build command context instances.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)<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 emptyOptionalif this command context contains no additional data for the key.<R> RgetAdditionalData(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.<R> RgetAdditionalData(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.Optional<String>getAlias()Returns the alias of the triggered command.Optional<Command<? super M>>getCommand()Returns the command that is triggered.MgetMessage()Returns the message that triggered the command processing.StringgetMessageContent()Returns the content of the message that triggered the command processing.Optional<String>getParameterString()Returns the parameter string to be processed by the triggered command.Optional<String>getPrefix()Returns the prefix that the message has to start with to trigger a command.inthashCode()<R> Optional<R>removeAdditionalData(String key)Removes the additional data for the given key.<R> Optional<R>setAdditionalData(String key, Object value)Sets the additional data for the given key to the given value.StringtoString()CommandContext.Builder<M>withAlias(String alias)Returns a builder that will create a new command context based on this instance with the given alias.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.CommandContext.Builder<M>withMessage(M message)Returns a builder that will create a new command context based on this instance with the given message.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.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.CommandContext.Builder<M>withPrefix(String prefix)Returns a builder that will create a new command context based on this instance with the given prefix.
-
-
-
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 emptyOptionalif this command context contains no additional data for the key.The returned
Optionalcan implicitly be downcasted by usingCommandContextto define the type using an explicit type parameter like with
or using implicit type inference like withcommandContext.<User>getAdditionalData("user");Optional<User> user = commandContext.getAdditionalData("user");Warning: Be aware that choosing
Rmust be done wisely as it is an unsafe operation. If you for example selectStringforRand then try to get aUserobject from the returned optional, you will get aClassCastExceptionat 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
CommandContextto define the type using an explicit type parameter like with
or using implicit type inference like withcommandContext.<User>getAdditionalData("user", new UserSubClass());
orUser user = commandContext.getAdditionalData("user", (User) null);User defaultUser = ...; commandContext.getAdditionalData("user", defaultUser);Warning: Be aware that choosing
Rmust be done wisely as it is an unsafe operation. If you for example selectStringforRand then try to get aUserobject, you will get aClassCastExceptionat 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 returneddefaultValue- 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
CommandContextto define the type using an explicit type parameter like with
or using implicit type inference like withcommandContext.<User>getAdditionalData("user", () -> null);
orUser user = commandContext.getAdditionalData("user", () -> null);User defaultUser = ...; commandContext.getAdditionalData("user", () -> defaultUser);Warning: Be aware that choosing
Rmust be done wisely as it is an unsafe operation. If you for example selectStringforRand then try to get aUserobject, you will get aClassCastExceptionat 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 returneddefaultValueSupplier- 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 emptyOptionalis returned.The returned value can implicitly be downcasted by using
CommandContextto define the type using an explicit type parameter like with
or using implicit type inference like withcommandContext.<User>setAdditionalData("user", user);Optional<User> user = commandContext.setAdditionalData("user", user);Warning: Be aware that choosing
Rmust be done wisely as it is an unsafe operation. If you for example selectStringforRand then try to get aUserobject, you will get aClassCastExceptionat 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 setvalue- 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 emptyOptionalis returned.The returned value can implicitly be downcasted by using
CommandContextto define the type using an explicit type parameter like with
or using implicit type inference like withcommandContext.<User>removeAdditionalData("user");Optional<User> user = commandContext.removeAdditionalData("user");Warning: Be aware that choosing
Rmust be done wisely as it is an unsafe operation. If you for example selectStringforRand then try to get aUserobject, you will get aClassCastExceptionat 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
-
-