net.lenni0451.commandlib.utils.comparator.CompletionsComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of CommandLib Show documentation
Show all versions of CommandLib Show documentation
A command lib with a simple and powerful API
The newest version!
package net.lenni0451.commandlib.utils.comparator;
import net.lenni0451.commandlib.Completion;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* A comparator to sort command completions.
* Numbers are sorted first, then the rest is sorted by {@link Comparator#naturalOrder()}.
*/
public class CompletionsComparator implements Comparator {
private static final Pattern INT_PATTERN = Pattern.compile("^[+-]?\\d+$");
private static final Pattern DECIMAL_PATTERN = Pattern.compile("^[+-]?(?:\\d+(\\.\\d*)?|\\d*\\.\\d+)$");
private static final Comparator BIG_INTEGER_COMPARATOR = BigInteger::compareTo;
private static final Comparator BIG_DECIMAL_COMPARATOR = BigDecimal::compareTo;
private final Map bigIntegerCache = new HashMap<>();
private final Map bigDecimalCache = new HashMap<>();
private final ArgumentComparator argumentComparator;
public CompletionsComparator(final ArgumentComparator argumentComparator) {
this.argumentComparator = argumentComparator;
}
@Override
public int compare(Completion c1, Completion c2) {
Integer result = this.compareNumber(c1.getCompletion(), c2.getCompletion());
if (result != null) return result;
return this.argumentComparator.compareTo(c1.getCompletion(), c2.getCompletion());
}
private Integer compareNumber(final String s1, final String s2) {
NumberType t1 = this.getNumberType(s1);
NumberType t2 = this.getNumberType(s2);
if (t1 == null || t2 == null) return null;
if (NumberType.INT.equals(t1) && NumberType.INT.equals(t2)) {
return BIG_INTEGER_COMPARATOR.compare(this.bigIntegerCache.computeIfAbsent(s1, BigInteger::new), this.bigIntegerCache.computeIfAbsent(s2, BigInteger::new));
} else {
return BIG_DECIMAL_COMPARATOR.compare(this.bigDecimalCache.computeIfAbsent(s1, BigDecimal::new), this.bigDecimalCache.computeIfAbsent(s2, BigDecimal::new));
}
}
private NumberType getNumberType(final String number) {
if (INT_PATTERN.matcher(number).matches()) return NumberType.INT;
if (DECIMAL_PATTERN.matcher(number).matches()) return NumberType.DECIMAL;
return null;
}
private enum NumberType {
INT,
DECIMAL
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy