List<Result> lastResult = newArrayList();
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 (parameters.stream().map(SearchParameter::from).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 String pattern;
+
+ private SearchParameter(boolean exclude, String pattern) {
+ this.exclude = exclude;
+ this.pattern = pattern;
+ }
+
+ public boolean matches(String filename) {
+ boolean matches = filename.toLowerCase().contains(pattern.toLowerCase());
+ return matches && !exclude;
+ }
+
+ public static SearchParameter from(String parameter) {
+ boolean exclude = parameter.startsWith("-");
+ String pattern = exclude ? parameter.substring(1) : parameter;
+ return new SearchParameter(exclude, pattern);
+ }
+
+ }
+
}