Encapsulate search parameters.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jan 2014 13:00:02 +0000 (14:00 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jan 2014 13:00:02 +0000 (14:00 +0100)
src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java

index fb76f1f..ad1d9d7 100644 (file)
@@ -70,18 +70,7 @@ public class SearchCommand implements Command {
                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));
                                }
                        }
@@ -96,4 +85,27 @@ public class SearchCommand implements Command {
                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);
+               }
+
+       }
+
 }