Add class for reply source information.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Source.java
1 /*
2  * XdccDownloader - Source.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.irc;
19
20 import com.google.common.base.Optional;
21
22 /**
23  * Container for reply source information.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class Source {
28
29         /** The nickname. */
30         private final Optional<String> nick;
31
32         /** The username. */
33         private final Optional<String> username;
34
35         /** The server. */
36         private final String server;
37
38         /**
39          * Creates a new source.
40          *
41          * @param nick
42          *              The nickname
43          * @param username
44          *              The username
45          * @param server
46          *              The server
47          */
48         Source(Optional<String> nick, Optional<String> username, String server) {
49                 this.nick = nick;
50                 this.username = username;
51                 this.server = server;
52         }
53
54         //
55         // ACCESSORS
56         //
57
58         /**
59          * Returns the nickname of the source.
60          *
61          * @return The nickname of the source
62          */
63         public Optional<String> nick() {
64                 return nick;
65         }
66
67         /**
68          * Returns the username of the source.
69          *
70          * @return The username of the source
71          */
72         public Optional<String> username() {
73                 return username;
74         }
75
76         /**
77          * Returns the server of the source.
78          *
79          * @return The server of the source
80          */
81         public String server() {
82                 return server;
83         }
84
85         //
86         // STATIC METHODS
87         //
88
89         /**
90          * Parses a source from the given string.
91          *
92          * @param source
93          *              The string to which to parse the source from
94          * @return The parsed source
95          */
96         public static Source parseSource(String source) {
97                 int exclamationMark = source.indexOf('!');
98                 int atSign = source.indexOf('@', exclamationMark);
99
100                 if ((exclamationMark == -1) || (atSign == -1)) {
101                         return new Source(Optional.<String>absent(), Optional.<String>absent(), source);
102                 }
103                 return new Source(Optional.of(source.substring(0, exclamationMark)), Optional.of(source.substring(exclamationMark + 1, atSign)), source.substring(atSign + 1));
104         }
105
106 }