From e9188b8c1a36b1f271a12a01ec7a358528a5ade7 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 5 Jul 2019 22:18:09 +0200 Subject: [PATCH] refactoring TabProposal --- .../java/util/commands/AbstractCommand.java | 18 ------- .../java/util/commands/TabProposal.java | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/main/java/fr/pandacube/java/util/commands/AbstractCommand.java b/src/main/java/fr/pandacube/java/util/commands/AbstractCommand.java index d2402a1..2329370 100644 --- a/src/main/java/fr/pandacube/java/util/commands/AbstractCommand.java +++ b/src/main/java/fr/pandacube/java/util/commands/AbstractCommand.java @@ -1,9 +1,6 @@ package fr.pandacube.java.util.commands; import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; public class AbstractCommand { @@ -38,21 +35,6 @@ public class AbstractCommand { if (index < 0 || index >= args.length) return null; return String.join(" ", Arrays.copyOfRange(args, index, args.length)); } - - /** - * Prends en charge les tokens avec des espaces, mais retourne les - * propositions complètes - * - * @param token - * @param allProposal - * @return - */ - public static List getTabProposalFromToken(String token, Collection allProposal) { - return allProposal.stream() - .filter(s -> s != null && s.toLowerCase().startsWith(token.toLowerCase())) - .sorted() - .collect(Collectors.toList()); - } diff --git a/src/main/java/fr/pandacube/java/util/commands/TabProposal.java b/src/main/java/fr/pandacube/java/util/commands/TabProposal.java index a728c2d..3e8e213 100644 --- a/src/main/java/fr/pandacube/java/util/commands/TabProposal.java +++ b/src/main/java/fr/pandacube/java/util/commands/TabProposal.java @@ -5,23 +5,31 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; @FunctionalInterface public interface TabProposal { - public abstract Collection getProposal(); + public abstract List getProposal(String token); + public static Predicate filter(String token) { + return (proposal) -> proposal.toLowerCase().startsWith(token.toLowerCase()); + } + public static List filterStream(Stream stream, String token) { + return stream.filter(filter(token)).sorted().collect(Collectors.toList()); + } - public static TabProposal empty() { return Collections::emptyList; } + public static TabProposal empty() { return t -> Collections.emptyList(); } public static > TabProposal fromEnum(Class enumClass) { @@ -30,17 +38,19 @@ public interface TabProposal { @SafeVarargs public static > TabProposal fromEnumValues(E... enumValues) { - return () -> Arrays.stream(enumValues).map(Enum::name).collect(Collectors.toList()); + return fromStream(Arrays.stream(enumValues).map(Enum::name)); } public static TabProposal fromCollection(Collection proposals) { - return () -> proposals; + return fromStream(proposals.stream()); } public static TabProposal fromIntRange(int startIncluded, int endIncluded) { - return () -> IntStream.rangeClosed(startIncluded, endIncluded) - .mapToObj(v -> Integer.toString(v)) - .collect(Collectors.toList()); + return fromStream(IntStream.rangeClosed(startIncluded, endIncluded).mapToObj(Integer::toString)); + } + + public static TabProposal fromStream(Stream proposals) { + return t -> filterStream(proposals, t); } /** @@ -57,19 +67,21 @@ public interface TabProposal { int currentTokenPosition = splittedToken.length - 1; String[] previousTokens = Arrays.copyOf(splittedToken, currentTokenPosition); - List currentTokenProposal = new ArrayList<>(); - for (String p : proposals) { - String[] splittedProposal = p.split(" ", -1); - if (splittedProposal.length <= currentTokenPosition) - continue; - if (!Arrays.equals(Arrays.copyOf(splittedToken, currentTokenPosition), previousTokens)) - continue; - if (splittedProposal[currentTokenPosition].isEmpty()) - continue; - - currentTokenProposal.add(splittedProposal[currentTokenPosition]); - } - - return () -> currentTokenProposal; + return token -> { + List currentTokenProposal = new ArrayList<>(); + for (String p : proposals) { + String[] splittedProposal = p.split(" ", -1); + if (splittedProposal.length <= currentTokenPosition) + continue; + if (!Arrays.equals(Arrays.copyOf(splittedToken, currentTokenPosition), previousTokens)) + continue; + if (splittedProposal[currentTokenPosition].isEmpty()) + continue; + + if (filter(token).test(splittedProposal[currentTokenPosition])) + currentTokenProposal.add(splittedProposal[currentTokenPosition]); + } + return currentTokenProposal; + }; } } \ No newline at end of file