Encapsulate search parameters.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / SearchCommand.java
1 /*
2  * XdccDownloader - SearchCommand.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 static com.google.common.collect.Lists.newArrayList;
21 import static java.util.Arrays.asList;
22
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;
28
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.Bot;
31 import net.pterodactylus.xdcc.data.Pack;
32
33 /**
34  * Command that searches all {@link Pack}s of all {@link Bot}s for files
35  * matching the search parameters.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class SearchCommand implements Command {
40
41         /** The core to operate on. */
42         private final Core core;
43
44         /**
45          * Creates a new search command.
46          *
47          * @param core
48          *              The core to operate on
49          */
50         public SearchCommand(Core core) {
51                 this.core = core;
52         }
53
54         //
55         // COMMAND METHODS
56         //
57
58         @Override
59         public String getName() {
60                 return "search";
61         }
62
63         @Override
64         public Collection<String> getAliases() {
65                 return asList("find", "locate");
66         }
67
68         @Override
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)) {
73                                 if (parameters.stream().map(SearchParameter::from).allMatch((parameter) -> parameter.matches(pack.name()))) {
74                                         lastResult.add(new Result(core, bot, pack));
75                                 }
76                         }
77                 }
78                 Collections.sort(lastResult);
79                 int counter = 0;
80                 for (Result result : lastResult) {
81                         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()));
82                 }
83                 outputWriter.write("End of Search.\n");
84                 outputWriter.flush();
85                 return state.setLastResults(lastResult);
86         }
87
88         private static class SearchParameter {
89
90                 private final boolean exclude;
91                 private final String pattern;
92
93                 private SearchParameter(boolean exclude, String pattern) {
94                         this.exclude = exclude;
95                         this.pattern = pattern;
96                 }
97
98                 public boolean matches(String filename) {
99                         boolean matches = filename.toLowerCase().contains(pattern.toLowerCase());
100                         return matches && !exclude;
101                 }
102
103                 public static SearchParameter from(String parameter) {
104                         boolean exclude = parameter.startsWith("-");
105                         String pattern = exclude ? parameter.substring(1) : parameter;
106                         return new SearchParameter(exclude, pattern);
107                 }
108
109         }
110
111 }