✅ Add more tests for current behaviour
[xudocci.git] / src / main / java / net / pterodactylus / irc / connection / SimpleCommandHandler.java
index 24df16f..373b75e 100644 (file)
@@ -5,9 +5,14 @@ import java.util.List;
 import java.util.Map;
 
 import net.pterodactylus.irc.Reply;
+import net.pterodactylus.irc.Source;
+
+import com.google.common.base.Optional;
+import com.google.common.eventbus.EventBus;
 
 /**
- * Handler that can process any number of events.
+ * Handler that can send events associated with commands to an {@link
+ * EventBus}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -15,34 +20,40 @@ public class SimpleCommandHandler implements Handler {
 
        private final Map<String, EventProcessor> commandEventSenders =
                        new HashMap<>();
+       private final EventBus eventBus;
+
+       public SimpleCommandHandler(EventBus eventBus) {
+               this.eventBus = eventBus;
+       }
 
        public SimpleCommandHandler addCommand(String command,
                        EventProcessor eventProcessor) {
-               commandEventSenders.put(command, eventProcessor);
+               commandEventSenders.put(command.toLowerCase(), eventProcessor);
                return this;
        }
 
        @Override
        public boolean willHandle(Reply reply) {
-               return commandEventSenders.containsKey(reply.command());
+               return commandEventSenders.containsKey(reply.command().toLowerCase());
        }
 
        @Override
        public void handleReply(Reply reply) {
                EventProcessor eventProcessor =
-                               commandEventSenders.get(reply.command());
-               eventProcessor.processEvent(reply.parameters());
+                               commandEventSenders.get(reply.command().toLowerCase());
+               eventBus.post(eventProcessor.createEvent(reply.source(),
+                               reply.parameters()));
        }
 
        /**
-        * Interface for event processors.
+        * Interface for event creators.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
        @FunctionalInterface
        public static interface EventProcessor {
 
-               void processEvent(List<String> parameters);
+               Object createEvent(Optional<Source> source, List<String> parameters);
 
        }