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.Objects;
21 import com.google.common.base.Optional;
24 * Container for reply source information.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 private final Optional<String> nick;
34 private final Optional<String> username;
37 private final String server;
40 * Creates a new source.
49 Source(Optional<String> nick, Optional<String> username, String server) {
51 this.username = username;
60 * Returns the nickname of the source.
62 * @return The nickname of the source
64 public Optional<String> nick() {
69 * Returns the username of the source.
71 * @return The username of the source
73 public Optional<String> username() {
78 * Returns the server of the source.
80 * @return The server of the source
82 public String server() {
91 public boolean equals(Object object) {
92 if (!(object instanceof Source)) {
95 Source source = (Source) object;
96 if (!Objects.equal(nick(), source.nick())) {
99 if (!Objects.equal(username(), source.username())) {
102 if (!Objects.equal(server(), source.server())) {
109 public String toString() {
110 return (nick.isPresent() ? nick().get() + "!" : "") + (username().isPresent() ? username.get() + "@" : "") + server;
118 * Parses a source from the given string.
121 * The string to which to parse the source from
122 * @return The parsed source
124 public static Source parseSource(String source) {
125 int exclamationMark = source.indexOf('!');
126 int atSign = source.indexOf('@', exclamationMark);
128 if ((exclamationMark == -1) || (atSign == -1)) {
129 return new Source(Optional.<String>absent(), Optional.<String>absent(), source);
131 return new Source(Optional.of(source.substring(0, exclamationMark)), Optional.of(source.substring(exclamationMark + 1, atSign)), source.substring(atSign + 1));