import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;
+import static java.util.regex.Pattern.CASE_INSENSITIVE;
+import static java.util.regex.Pattern.UNICODE_CASE;
+import static java.util.regex.Pattern.compile;
+import static java.util.stream.Collectors.toList;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import java.util.regex.Pattern;
import net.pterodactylus.xdcc.core.Core;
import net.pterodactylus.xdcc.data.Bot;
@Override
public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
List<Result> lastResult = newArrayList();
+ List<SearchParameter> searchParameters = parameters.stream().map(SearchParameter::from).collect(toList());
for (Bot bot : newArrayList(core.bots())) {
for (Pack pack : newArrayList(bot)) {
- boolean found = true;
- for (String parameter : parameters) {
- if (parameter.startsWith("-") && pack.name().toLowerCase().contains(parameter.toLowerCase().substring(1))) {
- found = false;
- break;
- }
- if (!parameter.startsWith("-") && !pack.name().toLowerCase().contains(parameter.toLowerCase())) {
- found = false;
- break;
- }
- }
- if (found) {
+ if (searchParameters.stream().allMatch((parameter) -> parameter.matches(pack.name()))) {
lastResult.add(new Result(core, bot, pack));
}
}
return state.setLastResults(lastResult);
}
+ private static class SearchParameter {
+
+ private final boolean exclude;
+ private final Pattern pattern;
+
+ private SearchParameter(boolean exclude, Pattern pattern) {
+ this.exclude = exclude;
+ this.pattern = pattern;
+ }
+
+ public boolean matches(String filename) {
+ return pattern.matcher(filename).find() && !exclude;
+ }
+
+ public static SearchParameter from(String parameter) {
+ boolean exclude = parameter.startsWith("-");
+ Pattern pattern = compile(exclude ? parameter.substring(1) : parameter, CASE_INSENSITIVE | UNICODE_CASE);
+ return new SearchParameter(exclude, pattern);
+ }
+
+ }
+
}