Skip to content

Commit

Permalink
implement comparison transitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Sep 10, 2024
1 parent d69200b commit 8dd2081
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
35 changes: 18 additions & 17 deletions common/src/main/java/revxrsal/commands/node/parser/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -186,25 +185,27 @@ public String toString() {
@Override
public int compareTo(@NotNull ExecutableCommand<A> o) {
if (!(o instanceof Execution<A> 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<A> ourLeaf = lastNode();
CommandNode<A> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,14 +117,20 @@ private void checkForPermission(ExecutionContext<A> context) {
@Override
public int compareTo(@NotNull CommandNode<A> o) {
if (o instanceof LiteralNodeImpl)
return -1;
return 1;
else {
ParameterNodeImpl<?, A> n = (ParameterNodeImpl<?, A>) 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;
}
}
}

Expand Down

0 comments on commit 8dd2081

Please sign in to comment.