From 3ecc370cb08ba0bbe12655dd5339a21cbba698f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 22 Oct 2014 19:52:19 +0200 Subject: [PATCH] Use a simple command handler for simple commands. --- .../java/net/pterodactylus/irc/Connection.java | 8 ++-- .../irc/connection/SimpleCommandHandler.java | 49 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/pterodactylus/irc/connection/SimpleCommandHandler.java diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index 45795f3..d12cc77 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -45,6 +45,7 @@ import net.pterodactylus.irc.connection.Handler; import net.pterodactylus.irc.connection.MessageHandler; import net.pterodactylus.irc.connection.MotdHandler; import net.pterodactylus.irc.connection.PrefixHandler; +import net.pterodactylus.irc.connection.SimpleCommandHandler; import net.pterodactylus.irc.event.ChannelJoined; import net.pterodactylus.irc.event.ChannelLeft; import net.pterodactylus.irc.event.ChannelTopic; @@ -368,6 +369,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic new MessageHandler(eventBus, this, prefixHandler), new CtcpHandler(eventBus, this), new ChannelNickHandler(eventBus, this, prefixHandler), + new SimpleCommandHandler() + .addCommand("431", (parameters) -> eventBus.post( + new NoNicknameGivenReceived(this))), new MotdHandler(eventBus, this), new ChannelNotJoinedHandler(eventBus, this), new ConnectionEstablishHandler(eventBus, this), @@ -389,9 +393,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic } /* 43x replies are for nick change errors. */ - if (command.equals("431")) { - eventBus.post(new NoNicknameGivenReceived(this)); - } else if (command.equals("433")) { + if (command.equals("433")) { if (!established.get()) { nickname = nicknameChooser.getNickname(); connectionHandler.sendCommand("NICK", nickname); diff --git a/src/main/java/net/pterodactylus/irc/connection/SimpleCommandHandler.java b/src/main/java/net/pterodactylus/irc/connection/SimpleCommandHandler.java new file mode 100644 index 0000000..24df16f --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/connection/SimpleCommandHandler.java @@ -0,0 +1,49 @@ +package net.pterodactylus.irc.connection; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.pterodactylus.irc.Reply; + +/** + * Handler that can process any number of events. + * + * @author David ‘Bombe’ Roden + */ +public class SimpleCommandHandler implements Handler { + + private final Map commandEventSenders = + new HashMap<>(); + + public SimpleCommandHandler addCommand(String command, + EventProcessor eventProcessor) { + commandEventSenders.put(command, eventProcessor); + return this; + } + + @Override + public boolean willHandle(Reply reply) { + return commandEventSenders.containsKey(reply.command()); + } + + @Override + public void handleReply(Reply reply) { + EventProcessor eventProcessor = + commandEventSenders.get(reply.command()); + eventProcessor.processEvent(reply.parameters()); + } + + /** + * Interface for event processors. + * + * @author David ‘Bombe’ Roden + */ + @FunctionalInterface + public static interface EventProcessor { + + void processEvent(List parameters); + + } + +} -- 2.7.4