2 * XdccDownloader - SearchCommand.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.xdcc.ui.stdin;
20 import static com.google.common.collect.Lists.newArrayList;
21 import static java.util.Arrays.asList;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.Bot;
31 import net.pterodactylus.xdcc.data.Pack;
34 * Command that searches all {@link Pack}s of all {@link Bot}s for files
35 * matching the search parameters.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class SearchCommand implements Command {
41 /** The core to operate on. */
42 private final Core core;
45 * Creates a new search command.
48 * The core to operate on
50 public SearchCommand(Core core) {
59 public String getName() {
64 public Collection<String> getAliases() {
65 return asList("find", "locate");
69 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
70 List<Result> lastResult = newArrayList();
71 for (Bot bot : newArrayList(core.bots())) {
72 for (Pack pack : newArrayList(bot)) {
74 for (String parameter : parameters) {
75 if (parameter.startsWith("-") && pack.name().toLowerCase().contains(parameter.toLowerCase().substring(1))) {
79 if (!parameter.startsWith("-") && !pack.name().toLowerCase().contains(parameter.toLowerCase())) {
85 lastResult.add(new Result(core, bot, pack));
89 Collections.sort(lastResult);
91 for (Result result : lastResult) {
92 outputWriter.write(String.format("[%d] %s (%s) from %s (#%s) on %s\n", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name()));
94 outputWriter.write("End of Search.\n");
96 return state.setLastResults(lastResult);