From 724167e41ebefc51ee04f7b8437259c0f5ed1f47 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 20 Jan 2014 14:00:02 +0100 Subject: [PATCH] Encapsulate search parameters. --- .../pterodactylus/xdcc/ui/stdin/SearchCommand.java | 36 ++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) 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); + } + + } + } -- 2.7.4