refactoring TabProposal

This commit is contained in:
Marc Baloup 2019-07-05 22:18:09 +02:00
parent 7739473213
commit e9188b8c1a
2 changed files with 33 additions and 39 deletions

View File

@ -1,9 +1,6 @@
package fr.pandacube.java.util.commands; package fr.pandacube.java.util.commands;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class AbstractCommand { public class AbstractCommand {
@ -39,21 +36,6 @@ public class AbstractCommand {
return String.join(" ", Arrays.copyOfRange(args, index, args.length)); return String.join(" ", Arrays.copyOfRange(args, index, args.length));
} }
/**
* <i>Prends en charge les tokens avec des espaces, mais retourne les
* propositions complètes</i>
*
* @param token
* @param allProposal
* @return
*/
public static List<String> getTabProposalFromToken(String token, Collection<String> allProposal) {
return allProposal.stream()
.filter(s -> s != null && s.toLowerCase().startsWith(token.toLowerCase()))
.sorted()
.collect(Collectors.toList());
}
} }

View File

@ -5,23 +5,31 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream;
@FunctionalInterface @FunctionalInterface
public interface TabProposal { public interface TabProposal {
public abstract Collection<String> getProposal(); public abstract List<String> getProposal(String token);
public static Predicate<String> filter(String token) {
return (proposal) -> proposal.toLowerCase().startsWith(token.toLowerCase());
}
public static List<String> filterStream(Stream<String> stream, String token) {
return stream.filter(filter(token)).sorted().collect(Collectors.toList());
}
public static TabProposal empty() { return t -> Collections.emptyList(); }
public static TabProposal empty() { return Collections::emptyList; }
public static <E extends Enum<E>> TabProposal fromEnum(Class<E> enumClass) { public static <E extends Enum<E>> TabProposal fromEnum(Class<E> enumClass) {
@ -30,17 +38,19 @@ public interface TabProposal {
@SafeVarargs @SafeVarargs
public static <E extends Enum<E>> TabProposal fromEnumValues(E... enumValues) { public static <E extends Enum<E>> 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<String> proposals) { public static TabProposal fromCollection(Collection<String> proposals) {
return () -> proposals; return fromStream(proposals.stream());
} }
public static TabProposal fromIntRange(int startIncluded, int endIncluded) { public static TabProposal fromIntRange(int startIncluded, int endIncluded) {
return () -> IntStream.rangeClosed(startIncluded, endIncluded) return fromStream(IntStream.rangeClosed(startIncluded, endIncluded).mapToObj(Integer::toString));
.mapToObj(v -> Integer.toString(v)) }
.collect(Collectors.toList());
public static TabProposal fromStream(Stream<String> proposals) {
return t -> filterStream(proposals, t);
} }
/** /**
@ -57,19 +67,21 @@ public interface TabProposal {
int currentTokenPosition = splittedToken.length - 1; int currentTokenPosition = splittedToken.length - 1;
String[] previousTokens = Arrays.copyOf(splittedToken, currentTokenPosition); String[] previousTokens = Arrays.copyOf(splittedToken, currentTokenPosition);
List<String> currentTokenProposal = new ArrayList<>(); return token -> {
for (String p : proposals) { List<String> currentTokenProposal = new ArrayList<>();
String[] splittedProposal = p.split(" ", -1); for (String p : proposals) {
if (splittedProposal.length <= currentTokenPosition) String[] splittedProposal = p.split(" ", -1);
continue; if (splittedProposal.length <= currentTokenPosition)
if (!Arrays.equals(Arrays.copyOf(splittedToken, currentTokenPosition), previousTokens)) continue;
continue; if (!Arrays.equals(Arrays.copyOf(splittedToken, currentTokenPosition), previousTokens))
if (splittedProposal[currentTokenPosition].isEmpty()) continue;
continue; if (splittedProposal[currentTokenPosition].isEmpty())
continue;
currentTokenProposal.add(splittedProposal[currentTokenPosition]); if (filter(token).test(splittedProposal[currentTokenPosition]))
} currentTokenProposal.add(splittedProposal[currentTokenPosition]);
}
return () -> currentTokenProposal; return currentTokenProposal;
};
} }
} }