Suggest flag name instead of value

This commit is contained in:
Hannes Greule 2021-04-10 09:34:53 +02:00
parent 050cf3edb3
commit a5c43bb823
2 changed files with 17 additions and 12 deletions

View File

@ -202,7 +202,8 @@ public final class FlagCommand extends Command {
final StringComparison<PlotFlag<?, ?>> stringComparison =
new StringComparison<>(
arg,
GlobalFlagContainer.getInstance().getFlagMap().values()
GlobalFlagContainer.getInstance().getFlagMap().values(),
PlotFlag::getName
);
final String best = stringComparison.getBestMatch();
if (best != null) {

View File

@ -28,6 +28,8 @@ package com.plotsquared.core.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Function;
/**
* String comparison library.
@ -37,6 +39,7 @@ public class StringComparison<T> {
private T bestMatch;
private double match = Integer.MAX_VALUE;
private T bestMatchObject;
private final Function<T, String> toString;
/**
* Constructor
@ -45,17 +48,27 @@ public class StringComparison<T> {
* @param objects Objects to compare
*/
public StringComparison(String input, T[] objects) {
this(input, objects, Object::toString);
}
public StringComparison(String input, T[] objects, Function<T, String> toString) {
this.toString = toString;
init(input, objects);
}
public StringComparison(String input, Collection<T> objects) {
init(input, (T[]) objects.toArray());
this(input, objects, Object::toString);
}
public StringComparison(String input, Collection<T> objects, Function<T, String> toString) {
this(input, (T[]) objects.toArray(), toString);
}
/**
* You should call init(...) when you are ready to get a String comparison value.
*/
public StringComparison() {
this.toString = Object::toString;
}
/**
@ -122,10 +135,7 @@ public class StringComparison<T> {
}
public String getString(T o) {
if (o instanceof StringComparable) {
return ((StringComparable) o).getComparableString();
}
return o.toString();
return this.toString.apply(o);
}
/**
@ -155,12 +165,6 @@ public class StringComparison<T> {
return new ComparisonResult(this.match, this.bestMatch);
}
public interface StringComparable {
String getComparableString();
}
/**
* The comparison result