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 * Parses the given line into a reply.
99 * @return The parsed reply
101 public static Reply parseLine(String line) {
102 String remainingLine = line;
105 Optional<Source> source = Optional.absent();
106 if (remainingLine.startsWith(":")) {
107 source = Optional.of(Source.parseSource(getFirstWord(remainingLine).substring(1)));
108 remainingLine = removeFirstWord(remainingLine);
112 String command = getFirstWord(remainingLine);
113 remainingLine = removeFirstWord(remainingLine);
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));
122 String parameter = getFirstWord(remainingLine);
123 parameters.add(parameter);
124 remainingLine = removeFirstWord(remainingLine);
127 return new Reply(source, command, parameters);
131 * Returns the first word of the given line.
134 * The line to get the first word of
135 * @return The first word of the line
137 private static String getFirstWord(String line) {
138 if (line.indexOf(' ') > -1) {
139 return line.substring(0, line.indexOf(' '));
145 * Returns the given line with the first word removed.
148 * The line from which to remove the first word
149 * @return The line with the first word removed
151 private static String removeFirstWord(String line) {
152 if (line.indexOf(' ') > -1) {
153 return line.substring(line.indexOf(' ') + 1);