Use a source in the reply.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Reply.java
1 /*
2  * XdccDownloader - Reply.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 java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23
24 import com.google.common.base.Optional;
25
26 /**
27  * A reply from the IRC server. A reply contains an optional source, a command
28  * name (which may be a numeric code), and a list of parameters.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class Reply {
33
34         /** The source of the reply. */
35         private final Optional<Source> source;
36
37         /** The command of the reply (may be numeric). */
38         private final String command;
39
40         /** The parameters of the reply. */
41         private final List<String> parameters;
42
43         /**
44          * Creates a new reply.
45          *
46          * @param source
47          *              The source of the reply
48          * @param command
49          *              The command of the reply (may be numeric)
50          * @param parameters
51          *              The parameters of the reply
52          */
53         private Reply(Optional<Source> source, String command, List<String> parameters) {
54                 this.source = source;
55                 this.command = command;
56                 this.parameters = parameters;
57         }
58
59         //
60         // ACCESSORS
61         //
62
63         /**
64          * Returns the source of the reply.
65          *
66          * @return The source of the reply, or {@link Optional#absent()}
67          */
68         public Optional<Source> source() {
69                 return source;
70         }
71
72         /**
73          * Returns the command of the reply.
74          *
75          * @return The command of the reply (may be numeric)
76          */
77         public String command() {
78                 return command;
79         }
80
81         /**
82          * Returns the parameters of the reply.
83          *
84          * @return The parameters of the reply
85          */
86         public List<String> parameters() {
87                 return Collections.unmodifiableList(parameters);
88         }
89
90         //
91         // STATIC METHODS
92         //
93
94         /**
95          * Parses the given line into a reply.
96          *
97          * @param line
98          *              The line to parse
99          * @return The parsed reply
100          */
101         public static Reply parseLine(String line) {
102                 String remainingLine = line;
103
104                 /* parse source. */
105                 Optional<Source> source = Optional.absent();
106                 if (remainingLine.startsWith(":")) {
107                         source = Optional.of(Source.parseSource(getFirstWord(remainingLine).substring(1)));
108                         remainingLine = removeFirstWord(remainingLine);
109                 }
110
111                 /* parse command. */
112                 String command = getFirstWord(remainingLine);
113                 remainingLine = removeFirstWord(remainingLine);
114
115                 /* parse parameters. */
116                 List<String> parameters = new ArrayList<String>();
117                 while (remainingLine.length() > 0) {
118                         if (remainingLine.startsWith(":")) {
119                                 parameters.add(remainingLine.substring(1));
120                                 break;
121                         }
122                         String parameter = getFirstWord(remainingLine);
123                         parameters.add(parameter);
124                         remainingLine = removeFirstWord(remainingLine);
125                 }
126
127                 return new Reply(source, command, parameters);
128         }
129
130         /**
131          * Returns the first word of the given line.
132          *
133          * @param line
134          *              The line to get the first word of
135          * @return The first word of the line
136          */
137         private static String getFirstWord(String line) {
138                 if (line.indexOf(' ') > -1) {
139                         return line.substring(0, line.indexOf(' '));
140                 }
141                 return line;
142         }
143
144         /**
145          * Returns the given line with the first word removed.
146          *
147          * @param line
148          *              The line from which to remove the first word
149          * @return The line with the first word removed
150          */
151         private static String removeFirstWord(String line) {
152                 if (line.indexOf(' ') > -1) {
153                         return line.substring(line.indexOf(' ') + 1);
154                 }
155                 return "";
156         }
157
158 }