Split command reader into separate commands.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / Command.java
1 /*
2  * XdccDownloader - Command.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.xdcc.ui.stdin;
19
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.Collection;
23 import java.util.List;
24
25 import com.google.common.base.Function;
26
27 /**
28  * A command is executed by the {@link CommandReader}. It receives the current
29  * state of the command reader and can return a changed state, depending on the
30  * given parameters.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public interface Command {
35
36         /** Converts a command into its name. */
37         public static final Function<Command, String> TO_NAME = new Function<Command, String>() {
38                 @Override
39                 public String apply(Command command) {
40                         return command.getName();
41                 }
42         };
43
44         /** Converts a command into its aliases. */
45         public static final Function<Command, Collection<String>> TO_ALIASES = new Function<Command, Collection<String>>() {
46                 @Override
47                 public Collection<String> apply(Command command) {
48                         return command.getAliases();
49                 }
50         };
51
52         /**
53          * Returns the name of this command.
54          *
55          * @return The name of this command
56          */
57         public String getName();
58
59         /**
60          * Returns possible aliases for this command.
61          *
62          * @return Possible aliases for this command
63          */
64         public Collection<String> getAliases();
65
66         /**
67          * Executes this command.
68          *
69          * @param state
70          *              The current state of the command reader
71          * @param parameters
72          *              The parameters given on the command line
73          * @param outputWriter
74          *              The output writer to write the output of the command to
75          * @return The new state of the command reader (which may be the given state if
76          *         the command does not need to modify the state)
77          * @throws IOException
78          *              if an I/O error occurs
79          */
80         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException;
81
82 }