From 74e56181fe0578d1371401d718af93131b87b9bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 6 Apr 2013 14:31:53 +0200 Subject: [PATCH] Add class for replies. --- src/main/java/net/pterodactylus/irc/Reply.java | 158 +++++++++++++++++++++ src/test/java/net/pterodactylus/irc/ReplyTest.java | 56 ++++++++ 2 files changed, 214 insertions(+) create mode 100644 src/main/java/net/pterodactylus/irc/Reply.java create mode 100644 src/test/java/net/pterodactylus/irc/ReplyTest.java diff --git a/src/main/java/net/pterodactylus/irc/Reply.java b/src/main/java/net/pterodactylus/irc/Reply.java new file mode 100644 index 0000000..417a7a1 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/Reply.java @@ -0,0 +1,158 @@ +/* + * XdccDownloader - Reply.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.irc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.google.common.base.Optional; + +/** + * A reply from the IRC server. A reply contains an optional source, a command + * name (which may be a numeric code), and a list of parameters. + * + * @author David ‘Bombe’ Roden + */ +public class Reply { + + /** The source of the reply. */ + private final Optional source; + + /** The command of the reply (may be numeric). */ + private final String command; + + /** The parameters of the reply. */ + private final List parameters; + + /** + * Creates a new reply. + * + * @param source + * The source of the reply + * @param command + * The command of the reply (may be numeric) + * @param parameters + * The parameters of the reply + */ + private Reply(Optional source, String command, List parameters) { + this.source = source; + this.command = command; + this.parameters = parameters; + } + + // + // ACCESSORS + // + + /** + * Returns the source of the reply. + * + * @return The source of the reply, or {@link Optional#absent()} + */ + public Optional source() { + return source; + } + + /** + * Returns the command of the reply. + * + * @return The command of the reply (may be numeric) + */ + public String command() { + return command; + } + + /** + * Returns the parameters of the reply. + * + * @return The parameters of the reply + */ + public List parameters() { + return Collections.unmodifiableList(parameters); + } + + // + // STATIC METHODS + // + + /** + * Parses the given line into a reply. + * + * @param line + * The line to parse + * @return The parsed reply + */ + public static Reply parseLine(String line) { + String remainingLine = line; + + /* parse source. */ + Optional source = Optional.absent(); + if (remainingLine.startsWith(":")) { + source = Optional.of(getFirstWord(remainingLine).substring(1)); + remainingLine = removeFirstWord(remainingLine); + } + + /* parse command. */ + String command = getFirstWord(remainingLine); + remainingLine = removeFirstWord(remainingLine); + + /* parse parameters. */ + List parameters = new ArrayList(); + while (remainingLine.length() > 0) { + if (remainingLine.startsWith(":")) { + parameters.add(remainingLine.substring(1)); + break; + } + String parameter = getFirstWord(remainingLine); + parameters.add(parameter); + remainingLine = removeFirstWord(remainingLine); + } + + return new Reply(source, command, parameters); + } + + /** + * Returns the first word of the given line. + * + * @param line + * The line to get the first word of + * @return The first word of the line + */ + private static String getFirstWord(String line) { + if (line.indexOf(' ') > -1) { + return line.substring(0, line.indexOf(' ')); + } + return line; + } + + /** + * Returns the given line with the first word removed. + * + * @param line + * The line from which to remove the first word + * @return The line with the first word removed + */ + private static String removeFirstWord(String line) { + if (line.indexOf(' ') > -1) { + return line.substring(line.indexOf(' ') + 1); + } + return ""; + } + +} diff --git a/src/test/java/net/pterodactylus/irc/ReplyTest.java b/src/test/java/net/pterodactylus/irc/ReplyTest.java new file mode 100644 index 0000000..9d84c54 --- /dev/null +++ b/src/test/java/net/pterodactylus/irc/ReplyTest.java @@ -0,0 +1,56 @@ +/* + * XdccDownloader - ReplyTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.irc; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import com.google.common.base.Optional; +import org.testng.annotations.Test; + +/** + * Tests for {@link Reply}. + * + * @author David ‘Bombe’ Roden + */ +public class ReplyTest { + + @Test + public void testParser() { + Reply reply; + + reply = Reply.parseLine("NOTICE AUTH :*** Processing your connection"); + assertThat(reply, notNullValue()); + assertThat(reply.source(), is(Optional.absent())); + assertThat(reply.command(), is("NOTICE")); + assertThat(reply.parameters(), hasSize(2)); + assertThat(reply.parameters().get(0), is("AUTH")); + assertThat(reply.parameters().get(1), is("*** Processing your connection")); + + reply = Reply.parseLine(":ParaDMON!services@paraphysics.services PRIVMSG QshelTier :\u0001VERSION\u0001"); + assertThat(reply, notNullValue()); + assertThat(reply.source(), is(Optional.of("ParaDMON!services@paraphysics.services"))); + assertThat(reply.command(), is("PRIVMSG")); + assertThat(reply.parameters(), hasSize(2)); + assertThat(reply.parameters().get(0), is("QshelTier")); + assertThat(reply.parameters().get(1), is("\u0001VERSION\u0001")); + } + +} -- 2.7.4