Add toString() method to default connection
[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         // OBJECT METHODS
92         //
93
94         @Override
95         public String toString() {
96                 StringBuilder string = new StringBuilder();
97                 if (source.isPresent()) {
98                         string.append(':').append(source.get()).append(' ');
99                 }
100                 string.append(command);
101                 for (int parameterIndex = 0; parameterIndex < parameters.size(); ++parameterIndex) {
102                         string.append(' ');
103                         String parameter = parameters.get(parameterIndex);
104                         if (parameterIndex == (parameters.size() - 1) && parameter.contains(" ")) {
105                                 string.append(':');
106                         }
107                         string.append(parameter);
108                 }
109                 return string.toString();
110         }
111
112         //
113         // STATIC METHODS
114         //
115
116         /**
117          * Parses the given line into a reply.
118          *
119          * @param line
120          *              The line to parse
121          * @return The parsed reply
122          */
123         public static Reply parseLine(String line) {
124                 String remainingLine = line;
125
126                 /* parse source. */
127                 Optional<Source> source = Optional.absent();
128                 if (remainingLine.startsWith(":")) {
129                         source = Optional.of(Source.parseSource(getFirstWord(remainingLine).substring(1)));
130                         remainingLine = removeFirstWord(remainingLine);
131                 }
132
133                 /* parse command. */
134                 String command = getFirstWord(remainingLine);
135                 remainingLine = removeFirstWord(remainingLine);
136
137                 /* parse parameters. */
138                 List<String> parameters = new ArrayList<String>();
139                 while (remainingLine.length() > 0) {
140                         if (remainingLine.startsWith(":")) {
141                                 parameters.add(remainingLine.substring(1));
142                                 break;
143                         }
144                         String parameter = getFirstWord(remainingLine);
145                         parameters.add(parameter);
146                         remainingLine = removeFirstWord(remainingLine);
147                 }
148
149                 return new Reply(source, command, parameters);
150         }
151
152         /**
153          * Returns the first word of the given line.
154          *
155          * @param line
156          *              The line to get the first word of
157          * @return The first word of the line
158          */
159         private static String getFirstWord(String line) {
160                 if (line.indexOf(' ') > -1) {
161                         return line.substring(0, line.indexOf(' '));
162                 }
163                 return line;
164         }
165
166         /**
167          * Returns the given line with the first word removed.
168          *
169          * @param line
170          *              The line from which to remove the first word
171          * @return The line with the first word removed
172          */
173         private static String removeFirstWord(String line) {
174                 if (line.indexOf(' ') > -1) {
175                         return line.substring(line.indexOf(' ') + 1);
176                 }
177                 return "";
178         }
179
180 }