From 5633e6fb70b4dc9e066e41bb10481bf255dd433d 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:37:42 +0200 Subject: [PATCH] Add class for reply source information. --- src/main/java/net/pterodactylus/irc/Source.java | 106 +++++++++++++++++++++ .../java/net/pterodactylus/irc/SourceTest.java | 51 ++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/main/java/net/pterodactylus/irc/Source.java create mode 100644 src/test/java/net/pterodactylus/irc/SourceTest.java diff --git a/src/main/java/net/pterodactylus/irc/Source.java b/src/main/java/net/pterodactylus/irc/Source.java new file mode 100644 index 0000000..7574396 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/Source.java @@ -0,0 +1,106 @@ +/* + * XdccDownloader - Source.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 com.google.common.base.Optional; + +/** + * Container for reply source information. + * + * @author David ‘Bombe’ Roden + */ +public class Source { + + /** The nickname. */ + private final Optional nick; + + /** The username. */ + private final Optional username; + + /** The server. */ + private final String server; + + /** + * Creates a new source. + * + * @param nick + * The nickname + * @param username + * The username + * @param server + * The server + */ + Source(Optional nick, Optional username, String server) { + this.nick = nick; + this.username = username; + this.server = server; + } + + // + // ACCESSORS + // + + /** + * Returns the nickname of the source. + * + * @return The nickname of the source + */ + public Optional nick() { + return nick; + } + + /** + * Returns the username of the source. + * + * @return The username of the source + */ + public Optional username() { + return username; + } + + /** + * Returns the server of the source. + * + * @return The server of the source + */ + public String server() { + return server; + } + + // + // STATIC METHODS + // + + /** + * Parses a source from the given string. + * + * @param source + * The string to which to parse the source from + * @return The parsed source + */ + public static Source parseSource(String source) { + int exclamationMark = source.indexOf('!'); + int atSign = source.indexOf('@', exclamationMark); + + if ((exclamationMark == -1) || (atSign == -1)) { + return new Source(Optional.absent(), Optional.absent(), source); + } + return new Source(Optional.of(source.substring(0, exclamationMark)), Optional.of(source.substring(exclamationMark + 1, atSign)), source.substring(atSign + 1)); + } + +} diff --git a/src/test/java/net/pterodactylus/irc/SourceTest.java b/src/test/java/net/pterodactylus/irc/SourceTest.java new file mode 100644 index 0000000..84f4a85 --- /dev/null +++ b/src/test/java/net/pterodactylus/irc/SourceTest.java @@ -0,0 +1,51 @@ +/* + * XdccDownloader - SourceTest.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.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.common.base.Optional; +import org.testng.annotations.Test; + +/** + * Tests for {@link Source}. + * + * @author David ‘Bombe’ Roden + */ +public class SourceTest { + + @Test + public void testParser() { + Source source; + + source = Source.parseSource("server.irc.net"); + assertThat(source, notNullValue()); + assertThat(source.nick(), is(Optional.absent())); + assertThat(source.username(), is(Optional.absent())); + assertThat(source.server(), is("server.irc.net")); + + source = Source.parseSource("foo!bar@server.irc.net"); + assertThat(source, notNullValue()); + assertThat(source.nick(), is(Optional.of("foo"))); + assertThat(source.username(), is(Optional.of("bar"))); + assertThat(source.server(), is("server.irc.net")); + } + +} -- 2.7.4