Process handlers generically.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 17:44:46 +0000 (19:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 17:44:46 +0000 (19:44 +0200)
src/main/java/net/pterodactylus/irc/Connection.java

index 6354c90..86de535 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.irc;
 
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Arrays.asList;
 import static java.util.concurrent.TimeUnit.SECONDS;
 
 import java.io.BufferedReader;
@@ -42,6 +43,7 @@ import javax.net.SocketFactory;
 
 import net.pterodactylus.irc.connection.ChannelNotJoinedHandler;
 import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
+import net.pterodactylus.irc.connection.Handler;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
@@ -382,8 +384,10 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                        Map<String, String> nickPrefixes = Maps.newHashMap();
                        Set<Character> channelTypes = Sets.newHashSet();
 
-                       ConnectionEstablishHandler connectionEstablishHandler = new ConnectionEstablishHandler(eventBus, this);
-                       ChannelNotJoinedHandler channelNotJoinedHandler = new ChannelNotJoinedHandler(eventBus, this);
+                       List<Handler> handlers = asList(
+                                       new ConnectionEstablishHandler(eventBus, this),
+                                       new ChannelNotJoinedHandler(eventBus, this)
+                       );
 
                        while (connected) {
                                Reply reply = connectionHandler.readReply();
@@ -392,6 +396,13 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                String command = reply.command();
                                List<String> parameters = reply.parameters();
 
+                               for (Handler handler : handlers) {
+                                       if (handler.willHandle(reply)) {
+                                               handler.handleReply(reply);
+                                               break;
+                                       }
+                               }
+
                                /* most common events. */
                                if (command.equalsIgnoreCase("PRIVMSG")) {
                                        String recipient = parameters.get(0);
@@ -455,16 +466,10 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                } else if (command.equalsIgnoreCase("QUIT")) {
                                        eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0)));
 
-                               } else if (channelNotJoinedHandler.willHandle(reply)) {
-                                       channelNotJoinedHandler.handleReply(reply);
-
                                /* basic connection housekeeping. */
                                } else if (command.equalsIgnoreCase("PING")) {
                                        connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
 
-                               } else if (connectionEstablishHandler.willHandle(reply)) {
-                                       connectionEstablishHandler.handleReply(reply);
-
                                /* 005 originally was a bounce message, now used to transmit useful information about the server. */
                                } else if (command.equals("005")) {
                                        for (String parameter : parameters) {