From 8dd2081556412ba93c3398ce48a12bae9fc8c416 Mon Sep 17 00:00:00 2001 From: Revxrsal Date: Tue, 10 Sep 2024 23:47:55 +0300 Subject: [PATCH] implement comparison transitivity --- .../commands/node/parser/Execution.java | 35 ++++++++++--------- .../node/parser/ParameterNodeImpl.java | 11 ++++-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/revxrsal/commands/node/parser/Execution.java b/common/src/main/java/revxrsal/commands/node/parser/Execution.java index 2dc5e6c1..766f502e 100644 --- a/common/src/main/java/revxrsal/commands/node/parser/Execution.java +++ b/common/src/main/java/revxrsal/commands/node/parser/Execution.java @@ -40,7 +40,6 @@ import java.util.*; -import static java.lang.Integer.compare; import static revxrsal.commands.exception.context.ErrorContext.executingFunction; import static revxrsal.commands.util.Collections.unmodifiableIterator; @@ -186,25 +185,27 @@ public String toString() { @Override public int compareTo(@NotNull ExecutableCommand o) { if (!(o instanceof Execution exec)) { - return 0; + return 0; // Handle the case where o is not an instance of Execution } - if (commandPriority().isPresent() && o.commandPriority().isPresent()) - return compare(commandPriority().getAsInt(), o.commandPriority().getAsInt()); - if (size - requiredInput == exec.size) { - if (isOptional(lastNode()) != isOptional(o.lastNode())) - return isOptional(lastNode()) ? 1 : -1; + + // Compare by priority if both have priorities + if (commandPriority().isPresent() && o.commandPriority().isPresent()) { + return Integer.compare(commandPriority().getAsInt(), o.commandPriority().getAsInt()); } - // notice that we do exec.size first, then our size. this - // reverses the order which is mostly what we want - // (higher size = higher priority) - int result = compare(exec.size, size); - if (result == 0) { - CommandNode ourLeaf = lastNode(); - CommandNode theirs = o.lastNode(); - - return ourLeaf.compareTo(theirs); + + // Compare by size if priorities are not present + int sizeComparison = Integer.compare(size, exec.size); + if (sizeComparison != 0) { + return sizeComparison; // Higher size = higher priority } - return result; + + // Compare optional status of the last node if sizes are equal + if (isOptional(lastNode()) != isOptional(o.lastNode())) { + return isOptional(lastNode()) ? 1 : -1; + } + + // Compare the last node if everything else is equal + return lastNode().compareTo(o.lastNode()); } @Override diff --git a/common/src/main/java/revxrsal/commands/node/parser/ParameterNodeImpl.java b/common/src/main/java/revxrsal/commands/node/parser/ParameterNodeImpl.java index 9e31de2b..b7e5d3af 100644 --- a/common/src/main/java/revxrsal/commands/node/parser/ParameterNodeImpl.java +++ b/common/src/main/java/revxrsal/commands/node/parser/ParameterNodeImpl.java @@ -47,7 +47,6 @@ import revxrsal.commands.stream.StringStream; import java.util.Collection; -import java.util.List; import static revxrsal.commands.reflect.ktx.KotlinConstants.defaultPrimitiveValue; @@ -118,14 +117,20 @@ private void checkForPermission(ExecutionContext context) { @Override public int compareTo(@NotNull CommandNode o) { if (o instanceof LiteralNodeImpl) - return -1; + return 1; else { ParameterNodeImpl n = (ParameterNodeImpl) o; if (isOptional && !n.isOptional) return 1; else if (n.isOptional && !isOptional) return -1; - return type.parsePriority().comparator().compare(parameterType(), n.parameterType()); + int compare = type.parsePriority().comparator().compare(parameterType(), n.parameterType()); + if (compare != 0) { + return compare; + } else { + compare = n.parameterType().parsePriority().comparator().compare(n.parameterType(), parameterType()); + return compare == 0 ? 0 : -compare; + } } }