refactoring TabProposal
This commit is contained in:
parent
7739473213
commit
e9188b8c1a
@ -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 {
|
||||
|
||||
@ -39,21 +36,6 @@ public class AbstractCommand {
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -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,6 +67,7 @@ public interface TabProposal {
|
||||
int currentTokenPosition = splittedToken.length - 1;
|
||||
String[] previousTokens = Arrays.copyOf(splittedToken, currentTokenPosition);
|
||||
|
||||
return token -> {
|
||||
List<String> currentTokenProposal = new ArrayList<>();
|
||||
for (String p : proposals) {
|
||||
String[] splittedProposal = p.split(" ", -1);
|
||||
@ -67,9 +78,10 @@ public interface TabProposal {
|
||||
if (splittedProposal[currentTokenPosition].isEmpty())
|
||||
continue;
|
||||
|
||||
if (filter(token).test(splittedProposal[currentTokenPosition]))
|
||||
currentTokenProposal.add(splittedProposal[currentTokenPosition]);
|
||||
}
|
||||
|
||||
return () -> currentTokenProposal;
|
||||
return currentTokenProposal;
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user