3232/**
3333 * centrally managed command parsing.
3434 *
35+ * This class is responsible to breaking IRC messages into commands which the
36+ * bot can understand. It contains a list of modules and the methods which are
37+ * tagged with the command annotation. Classes must be registered with the
38+ * Command Processor in order for the processor to recognise them as commands.
39+ *
3540 */
3641public class CommandProcessor
3742{
3843 private final Pattern regex ;
3944 private final Map <String , Object > commands ;
4045 private final Map <String , Map <String , Method >> callbacks ;
4146
47+ /**
48+ * Create a new command processor.
49+ *
50+ * This will create a new command processor and will initialise the regex
51+ * pattern the bot will use to match commands. It will also create the maps
52+ * needed to store information about the commands.
53+ *
54+ * @param trigger the first character of any line directed at the bot
55+ */
4256 public CommandProcessor (char trigger )
4357 {
4458 this .regex = Pattern .compile (trigger + "([a-z0-9]+)(?: ([a-z0-9]+))?(?: (.*))?" );
4559 this .commands = new HashMap <String , Object >();
4660 this .callbacks = new HashMap <String , Map <String , Method >>();
4761 }
4862
63+ /**
64+ * Register a new module and extract it's commands.
65+ *
66+ * This method will look at target and process any method which has been
67+ * annotated with the command annotation. It will then remember the module
68+ * and command names for use when processing messages.
69+ *
70+ * The module's name will need to be put before any command. This is to
71+ * prevent two modules conflicting with the same named commands.
72+ *
73+ * @param name the name of the module
74+ * @param target the module object
75+ */
4976 public void register (String name , Object target )
5077 {
5178 Map <String , Method > methods = new HashMap <String , Method >();
@@ -69,6 +96,16 @@ public void register(String name, Object target)
6996 callbacks .put (name , methods );
7097 }
7198
99+ /**
100+ * Process an IRC message to see if the bot needs to respond.
101+ *
102+ * This method takes an IRC message and splits it into it's component parts.
103+ * if the action is valid it will then call the call method to process the
104+ * event.
105+ *
106+ * @param event the event to be processed
107+ * @throws Exception
108+ */
72109 public void invoke (MessageEvent <PircBotX > event ) throws Exception
73110 {
74111 Matcher matcher = regex .matcher (event .getMessage ());
@@ -108,6 +145,15 @@ public void invoke(MessageEvent<PircBotX> event) throws Exception
108145 }
109146 }
110147
148+ /**
149+ * Call a module's command with required arguments.
150+ *
151+ * @param type the name of the module
152+ * @param cmd the name of the command
153+ * @param args the arguments to pass to the method associated with command
154+ * @return true if method was called, false if not
155+ * @throws Exception if the method throws an exception.
156+ */
111157 private boolean call (String type , String cmd , Object ... args ) throws Exception
112158 {
113159 Object obj = commands .get (type );
@@ -126,13 +172,24 @@ private boolean call(String type, String cmd, Object... args) throws Exception
126172 return true ;
127173 }
128174
175+ /**
176+ * Get a list of modules registered with the command processor.
177+ *
178+ * @return the list of module names
179+ */
129180 public String [] getModules ()
130181 {
131182 Collection <String > modules = callbacks .keySet ();
132183 String [] moduleArray = new String [modules .size ()];
133184 return modules .toArray (moduleArray );
134185 }
135186
187+ /**
188+ * Gets a list of commands which are registered with the command processor
189+ *
190+ * @param moduleName the name of the module to get the commands from.
191+ * @return the list of command names, or null if command doesn't exist.
192+ */
136193 public String [] getCommands (String moduleName )
137194 {
138195 Map <String , Method > commands = callbacks .get (moduleName );
0 commit comments