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;
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));
}
/**
* <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.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<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 Collections::emptyList; }
public static TabProposal empty() { return t -> Collections.emptyList(); }
public static <E extends Enum<E>> TabProposal fromEnum(Class<E> enumClass) {
@ -30,17 +38,19 @@ public interface TabProposal {
@SafeVarargs
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) {
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<String> 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<String> 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<String> 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;
};
}
}