From a5c43bb823cc2191a067540064a1d8ea28165714 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sat, 10 Apr 2021 09:34:53 +0200 Subject: [PATCH] Suggest flag name instead of value --- .../plotsquared/core/command/FlagCommand.java | 3 ++- .../core/util/StringComparison.java | 26 +++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java index dcefbed55..8562cda29 100644 --- a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java @@ -202,7 +202,8 @@ public final class FlagCommand extends Command { final StringComparison> stringComparison = new StringComparison<>( arg, - GlobalFlagContainer.getInstance().getFlagMap().values() + GlobalFlagContainer.getInstance().getFlagMap().values(), + PlotFlag::getName ); final String best = stringComparison.getBestMatch(); if (best != null) { diff --git a/Core/src/main/java/com/plotsquared/core/util/StringComparison.java b/Core/src/main/java/com/plotsquared/core/util/StringComparison.java index 277121c95..d75d66cbe 100644 --- a/Core/src/main/java/com/plotsquared/core/util/StringComparison.java +++ b/Core/src/main/java/com/plotsquared/core/util/StringComparison.java @@ -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 { private T bestMatch; private double match = Integer.MAX_VALUE; private T bestMatchObject; + private final Function toString; /** * Constructor @@ -45,17 +48,27 @@ public class StringComparison { * @param objects Objects to compare */ public StringComparison(String input, T[] objects) { + this(input, objects, Object::toString); + } + + public StringComparison(String input, T[] objects, Function toString) { + this.toString = toString; init(input, objects); } public StringComparison(String input, Collection objects) { - init(input, (T[]) objects.toArray()); + this(input, objects, Object::toString); + } + + public StringComparison(String input, Collection objects, Function 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 { } 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 { return new ComparisonResult(this.match, this.bestMatch); } - public interface StringComparable { - - String getComparableString(); - - } - /** * The comparison result