2 * XdccDownloader - Source.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.irc;
20 import com.google.common.base.Optional;
23 * Container for reply source information.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 private final Optional<String> nick;
33 private final Optional<String> username;
36 private final String server;
39 * Creates a new source.
48 Source(Optional<String> nick, Optional<String> username, String server) {
50 this.username = username;
59 * Returns the nickname of the source.
61 * @return The nickname of the source
63 public Optional<String> nick() {
68 * Returns the username of the source.
70 * @return The username of the source
72 public Optional<String> username() {
77 * Returns the server of the source.
79 * @return The server of the source
81 public String server() {
90 * Parses a source from the given string.
93 * The string to which to parse the source from
94 * @return The parsed source
96 public static Source parseSource(String source) {
97 int exclamationMark = source.indexOf('!');
98 int atSign = source.indexOf('@', exclamationMark);
100 if ((exclamationMark == -1) || (atSign == -1)) {
101 return new Source(Optional.<String>absent(), Optional.<String>absent(), source);
103 return new Source(Optional.of(source.substring(0, exclamationMark)), Optional.of(source.substring(exclamationMark + 1, atSign)), source.substring(atSign + 1));