Add option to have compact of exaustive int/long suggestions

This commit is contained in:
Marc Baloup 2021-05-01 18:47:54 +02:00
parent 89db9b7a64
commit 3e7b5c6820
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
1 changed files with 52 additions and 46 deletions

View File

@ -8,6 +8,7 @@ import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import fr.pandacube.lib.core.util.ListUtil; import fr.pandacube.lib.core.util.ListUtil;
@ -106,8 +107,8 @@ public interface SuggestionsSupplier<S> {
* @param max * @param max
* @return * @return
*/ */
public static <S> SuggestionsSupplier<S> fromIntRange(int min, int max) { public static <S> SuggestionsSupplier<S> fromIntRange(int min, int max, boolean compact) {
return fromLongRange(min, max); return fromLongRange(min, max, compact);
} }
@ -121,67 +122,72 @@ public interface SuggestionsSupplier<S> {
* @param max * @param max
* @return * @return
*/ */
public static <S> SuggestionsSupplier<S> fromLongRange(long min, long max) { public static <S> SuggestionsSupplier<S> fromLongRange(long min, long max, boolean compact) {
if (max < min) { if (max < min) {
throw new IllegalArgumentException("min should be less or equals than max"); throw new IllegalArgumentException("min should be less or equals than max");
} }
return (s, ti, token, a) -> { if (compact) {
try { return (s, ti, token, a) -> {
List<Long> proposedValues = new ArrayList<>(); try {
if (token.length() == 0) { List<Long> proposedValues = new ArrayList<>();
long start = Math.max(Math.max(Math.min(-4, max - 9), min), -9); if (token.length() == 0) {
long end = Math.min(Math.min(start + 9, max), 9); long start = Math.max(Math.max(Math.min(-4, max - 9), min), -9);
ListUtil.addLongRangeToList(proposedValues, start, end); long end = Math.min(Math.min(start + 9, max), 9);
} ListUtil.addLongRangeToList(proposedValues, start, end);
else if (token.length() == 1) {
if (token.charAt(0) == '0') {
if (min > 0 || max < 0) {
return Collections.emptyList();
}
else
return Collections.singletonList(token);
} }
else if (token.charAt(0) == '-') { else if (token.length() == 1) {
ListUtil.addLongRangeToList(proposedValues, Math.max(-9, min), -1); if (token.charAt(0) == '0') {
if (min > 0 || max < 0) {
return Collections.emptyList();
}
else
return Collections.singletonList(token);
}
else if (token.charAt(0) == '-') {
ListUtil.addLongRangeToList(proposedValues, Math.max(-9, min), -1);
}
else {
long lToken = Long.parseLong(token);
if (lToken > max) {
return Collections.emptyList();
}
lToken *= 10;
if (lToken > max) {
return Collections.singletonList(token);
}
ListUtil.addLongRangeToList(proposedValues, lToken, Math.min(lToken + 9, max));
}
} }
else { else {
long lToken = Long.parseLong(token); long lToken = Long.parseLong(token);
if (lToken > max) { if (lToken < min || lToken > max) {
return Collections.emptyList(); return Collections.emptyList();
} }
lToken *= 10; lToken *= 10;
if (lToken > max) { if (lToken < min || lToken > max) {
return Collections.singletonList(token); return Collections.singletonList(token);
} }
ListUtil.addLongRangeToList(proposedValues, lToken, Math.min(lToken + 9, max)); if (lToken < 0) {
} ListUtil.addLongRangeToList(proposedValues, Math.max(lToken - 9, min), lToken);
} }
else { else {
long lToken = Long.parseLong(token); ListUtil.addLongRangeToList(proposedValues, lToken, Math.min(lToken + 9, max));
if (lToken < min || lToken > max) { }
return Collections.emptyList();
} }
lToken *= 10; return collectFilteredStream(proposedValues.stream().map(i -> i.toString()), token);
if (lToken < min || lToken > max) { } catch (NumberFormatException e) {
return Collections.singletonList(token); return Collections.emptyList();
}
if (lToken < 0) {
ListUtil.addLongRangeToList(proposedValues, Math.max(lToken - 9, min), lToken);
}
else {
ListUtil.addLongRangeToList(proposedValues, lToken, Math.min(lToken + 9, max));
}
} }
};
return collectFilteredStream(proposedValues.stream().map(i -> i.toString()), token); }
} catch (NumberFormatException e) { else {
return Collections.emptyList(); return (s, ti, token, a) -> collectFilteredStream(LongStream.rangeClosed(min, max).mapToObj(Long::toString), token);
} }
};
} }