From: David ‘Bombe’ Roden Date: Mon, 20 Jan 2014 13:00:02 +0000 (+0100) Subject: Encapsulate search parameters. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=724167e41ebefc51ee04f7b8437259c0f5ed1f47;p=xudocci.git Encapsulate search parameters. --- diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java index fb76f1f..ad1d9d7 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java @@ -70,18 +70,7 @@ public class SearchCommand implements Command { List 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); + } + + } + }