2 * XdccDownloader - Reply.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 java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
24 import com.google.common.base.Optional;
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.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 /** The source of the reply. */
35 private final Optional<Source> source;
37 /** The command of the reply (may be numeric). */
38 private final String command;
40 /** The parameters of the reply. */
41 private final List<String> parameters;
44 * Creates a new reply.
47 * The source of the reply
49 * The command of the reply (may be numeric)
51 * The parameters of the reply
53 private Reply(Optional<Source> source, String command, List<String> parameters) {
55 this.command = command;
56 this.parameters = parameters;
64 * Returns the source of the reply.
66 * @return The source of the reply, or {@link Optional#absent()}
68 public Optional<Source> source() {
73 * Returns the command of the reply.
75 * @return The command of the reply (may be numeric)
77 public String command() {
82 * Returns the parameters of the reply.
84 * @return The parameters of the reply
86 public List<String> parameters() {
87 return Collections.unmodifiableList(parameters);
95 public String toString() {
96 StringBuilder string = new StringBuilder();
97 if (source.isPresent()) {
98 string.append(':').append(source.get()).append(' ');
100 string.append(command);
101 for (int parameterIndex = 0; parameterIndex < parameters.size(); ++parameterIndex) {
103 String parameter = parameters.get(parameterIndex);
104 if (parameterIndex == (parameters.size() - 1) && parameter.contains(" ")) {
107 string.append(parameter);
109 return string.toString();
117 * Parses the given line into a reply.
121 * @return The parsed reply
123 public static Reply parseLine(String line) {
124 String remainingLine = line;
127 Optional<Source> source = Optional.absent();
128 if (remainingLine.startsWith(":")) {
129 source = Optional.of(Source.parseSource(getFirstWord(remainingLine).substring(1)));
130 remainingLine = removeFirstWord(remainingLine);
134 String command = getFirstWord(remainingLine);
135 remainingLine = removeFirstWord(remainingLine);
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));
144 String parameter = getFirstWord(remainingLine);
145 parameters.add(parameter);
146 remainingLine = removeFirstWord(remainingLine);
149 return new Reply(source, command, parameters);
153 * Returns the first word of the given line.
156 * The line to get the first word of
157 * @return The first word of the line
159 private static String getFirstWord(String line) {
160 if (line.indexOf(' ') > -1) {
161 return line.substring(0, line.indexOf(' '));
167 * Returns the given line with the first word removed.
170 * The line from which to remove the first word
171 * @return The line with the first word removed
173 private static String removeFirstWord(String line) {
174 if (line.indexOf(' ') > -1) {
175 return line.substring(line.indexOf(' ') + 1);