🚧 Parse token from DCC SEND
[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.Objects;
21 import com.google.common.base.Optional;
22
23 /**
24  * Container for reply source information.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
27  */
28 public class Source {
29
30         /** The nickname. */
31         private final Optional<String> nick;
32
33         /** The username. */
34         private final Optional<String> username;
35
36         /** The server. */
37         private final String server;
38
39         /**
40          * Creates a new source.
41          *
42          * @param nick
43          *              The nickname
44          * @param username
45          *              The username
46          * @param server
47          *              The server
48          */
49         Source(Optional<String> nick, Optional<String> username, String server) {
50                 this.nick = nick;
51                 this.username = username;
52                 this.server = server;
53         }
54
55         //
56         // ACCESSORS
57         //
58
59         /**
60          * Returns the nickname of the source.
61          *
62          * @return The nickname of the source
63          */
64         public Optional<String> nick() {
65                 return nick;
66         }
67
68         /**
69          * Returns the username of the source.
70          *
71          * @return The username of the source
72          */
73         public Optional<String> username() {
74                 return username;
75         }
76
77         /**
78          * Returns the server of the source.
79          *
80          * @return The server of the source
81          */
82         public String server() {
83                 return server;
84         }
85
86         //
87         // OBJECT METHODS
88         //
89
90         @Override
91         public boolean equals(Object object) {
92                 if (!(object instanceof Source)) {
93                         return false;
94                 }
95                 Source source = (Source) object;
96                 if (!Objects.equal(nick(), source.nick())) {
97                         return false;
98                 }
99                 if (!Objects.equal(username(), source.username())) {
100                         return false;
101                 }
102                 if (!Objects.equal(server(), source.server())) {
103                         return false;
104                 }
105                 return true;
106         }
107
108         @Override
109         public String toString() {
110                 return (nick.isPresent() ? nick().get() + "!" : "") + (username().isPresent() ? username.get() + "@" : "") + server;
111         }
112
113         //
114         // STATIC METHODS
115         //
116
117         /**
118          * Parses a source from the given string.
119          *
120          * @param source
121          *              The string to which to parse the source from
122          * @return The parsed source
123          */
124         public static Source parseSource(String source) {
125                 int exclamationMark = source.indexOf('!');
126                 int atSign = source.indexOf('@', exclamationMark);
127
128                 if ((exclamationMark == -1) || (atSign == -1)) {
129                         return new Source(Optional.<String>absent(), Optional.<String>absent(), source);
130                 }
131                 return new Source(Optional.of(source.substring(0, exclamationMark)), Optional.of(source.substring(exclamationMark + 1, atSign)), source.substring(atSign + 1));
132         }
133
134 }