Suggestions + BiMap improvement

This commit is contained in:
Marc Baloup 2020-05-07 18:37:06 +02:00
parent b870095bea
commit e65c402684
3 changed files with 125 additions and 5 deletions

View File

@ -1,8 +1,10 @@
package fr.pandacube.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
public class BiMap<K, V> implements Iterable<Entry<K, V>> {
@ -49,7 +51,15 @@ public class BiMap<K, V> implements Iterable<Entry<K, V>> {
@Override
public Iterator<Entry<K, V>> iterator() {
return map.entrySet().iterator();
return Collections.unmodifiableSet(map.entrySet()).iterator();
}
public Set<K> keySet() {
return Collections.unmodifiableSet(map.keySet());
}
public Set<V> valuesSet() {
return Collections.unmodifiableSet(inversedMap.keySet());
}
public synchronized void forEach(BiConsumer<K, V> c) {
@ -62,4 +72,9 @@ public class BiMap<K, V> implements Iterable<Entry<K, V>> {
return map.size();
}
public synchronized void clear() {
map.clear();
inversedMap.clear();
}
}

View File

@ -0,0 +1,13 @@
package fr.pandacube.util;
import java.util.List;
public class ListUtil {
public static void addLongRangeToList(List<Long> list, long min, long max) {
for (long i = min; i <= max; i++) {
list.add(i);
}
}
}

View File

@ -7,12 +7,18 @@ 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;
import fr.pandacube.util.ListUtil;
@FunctionalInterface
public interface Suggestions<S> {
/**
* Number of suggestion visible at once without having to scrolleeeeeeeeeeeeeeee
*/
public static int VISIBLE_SUGGESTION_COUNT = 10;
public abstract List<String> getSuggestions(S sender, int tokenIndex, String token, String[] args);
@ -74,14 +80,93 @@ public interface Suggestions<S> {
};
}
/**
* Create a {@link Suggestions} that suggest numbers according to the provided range.
*
* The current implementation only support range that include either -1 or 1.
* @param min
* @param max
* @return
*/
public static <S> Suggestions<S> fromIntRange(int min, int max) {
return (s, ti, token, a) -> {
return collectFilteredStream(IntStream.range(min, max + 1).mapToObj(Integer::toString), token);
};
return fromLongRange(min, max);
}
/**
* Create a {@link Suggestions} that suggest numbers according to the provided range.
*
* The current implementation only support range that include either -1 or 1.
* @param min
* @param max
* @return
*/
public static <S> Suggestions<S> fromLongRange(long min, long max) {
if (max < min) {
throw new IllegalArgumentException("min should be less or equals than max");
}
return (s, ti, token, a) -> {
try {
List<Long> proposedValues = new ArrayList<>();
if (token.length() == 0) {
long start = Math.max(Math.max(Math.min(-4, max - 9), min), -9);
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) == '-') {
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 {
long lToken = Long.parseLong(token);
if (lToken < min || lToken > max) {
return Collections.emptyList();
}
lToken *= 10;
if (lToken < min || lToken > max) {
return Collections.singletonList(token);
}
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) {
return Collections.emptyList();
}
};
}
/**
* Create a {@link Suggestions} that support greedy strings argument using the suggestion from this {@link Suggestions}.
* @param args all the arguments currently in the buffer
@ -121,4 +206,11 @@ public interface Suggestions<S> {
public default Suggestions<S> requires(Predicate<S> check) {
return (s, ti, to, a) -> {
return check.test(s) ? getSuggestions(s, ti, to, a) : Collections.emptyList();
};
}
}