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;
22 import static java.util.regex.Pattern.CASE_INSENSITIVE;
23 import static java.util.regex.Pattern.UNICODE_CASE;
24 import static java.util.regex.Pattern.compile;
25 import static java.util.stream.Collectors.toList;
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.regex.Pattern;
34 import net.pterodactylus.xdcc.core.Core;
35 import net.pterodactylus.xdcc.data.Bot;
36 import net.pterodactylus.xdcc.data.Pack;
39 * Command that searches all {@link Pack}s of all {@link Bot}s for files
40 * matching the search parameters.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class SearchCommand implements Command {
46 /** The core to operate on. */
47 private final Core core;
50 * Creates a new search command.
53 * The core to operate on
55 public SearchCommand(Core core) {
64 public String getName() {
69 public Collection<String> getAliases() {
70 return asList("find", "locate");
74 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
75 List<Result> lastResult = newArrayList();
76 List<SearchParameter> searchParameters = parameters.stream().map(SearchParameter::from).collect(toList());
77 for (Bot bot : newArrayList(core.bots())) {
78 for (Pack pack : newArrayList(bot)) {
79 if (searchParameters.stream().allMatch((parameter) -> parameter.matches(pack.name()))) {
80 lastResult.add(new Result(core, bot, pack));
84 Collections.sort(lastResult);
86 for (Result result : lastResult) {
87 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()));
89 outputWriter.write("End of Search.\n");
90 return state.setLastResults(lastResult);
93 private static class SearchParameter {
95 private final boolean exclude;
96 private final Pattern pattern;
98 private SearchParameter(boolean exclude, Pattern pattern) {
99 this.exclude = exclude;
100 this.pattern = pattern;
103 public boolean matches(String filename) {
104 return pattern.matcher(filename).find() && !exclude;
107 public static SearchParameter from(String parameter) {
108 boolean exclude = parameter.startsWith("-");
109 Pattern pattern = compile(exclude ? parameter.substring(1) : parameter, CASE_INSENSITIVE | UNICODE_CASE);
110 return new SearchParameter(exclude, pattern);