package com.xfestudio.xfeservermanager.api.policy;
import com.xfestudio.xfeservermanager.api.command.CommandNames;
import com.xfestudio.xfeservermanager.api.command.InvocationAst;
import com.xfestudio.xfeservermanager.api.util.ApiChecks;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/** Matches a canonical command id and an optional literal path prefix. */
public record CommandMatcher(Set<String> commandIds, List<String> pathPrefix) {
public CommandMatcher {
commandIds = ApiChecks.immutableSet(commandIds, "commandIds").stream()
.map(CommandNames::canonicalize)
.collect(java.util.stream.Collectors.toUnmodifiableSet());
pathPrefix = ApiChecks.immutableList(pathPrefix, "pathPrefix").stream()
.map(value -> ApiChecks.notBlank(value, "pathPrefix element").toLowerCase(Locale.ROOT))
.toList();
}
public static CommandMatcher any() {
return new CommandMatcher(Set.of(), List.of());
}
public static CommandMatcher command(String commandId) {
return new CommandMatcher(Set.of(commandId), List.of());
}
public boolean matches(InvocationAst invocation) {
if (!commandIds.isEmpty() && !commandIds.contains(invocation.commandId())) {
return false;
}
if (pathPrefix.size() > invocation.commandPath().size()) {
return false;
}
for (int index = 0; index < pathPrefix.size(); index++) {
if (!pathPrefix.get(index).equals(invocation.commandPath().get(index))) {
return false;
}
}
return true;
}
public int specificity() {
int commandSpecificity = commandIds.isEmpty() ? 0 : Math.max(1, 1_000 - commandIds.size());
return commandSpecificity + pathPrefix.size() * 2_000;
}
}
package com.xfestudio.xfeservermanager.api.policy;
import com.xfestudio.xfeservermanager.api.command.CommandNames;
import com.xfestudio.xfeservermanager.api.command.InvocationAst;
import com.xfestudio.xfeservermanager.api.util.ApiChecks;
import java.util.List;
import java.util.Locale;
import java.util.Set;
/** Matches a canonical command id and an optional literal path prefix. */
public record CommandMatcher(Set<String> commandIds, List<String> pathPrefix) {
public CommandMatcher {
commandIds = ApiChecks.immutableSet(commandIds, "commandIds").stream()
.map(CommandNames::canonicalize)
.collect(java.util.stream.Collectors.toUnmodifiableSet());
pathPrefix = ApiChecks.immutableList(pathPrefix, "pathPrefix").stream()
.map(value -> ApiChecks.notBlank(value, "pathPrefix element").toLowerCase(Locale.ROOT))
.toList();
}
public static CommandMatcher any() {
return new CommandMatcher(Set.of(), List.of());
}
public static CommandMatcher command(String commandId) {
return new CommandMatcher(Set.of(commandId), List.of());
}
public boolean matches(InvocationAst invocation) {
if (!commandIds.isEmpty() && !commandIds.contains(invocation.commandId())) {
return false;
}
if (pathPrefix.size() > invocation.commandPath().size()) {
return false;
}
for (int index = 0; index < pathPrefix.size(); index++) {
if (!pathPrefix.get(index).equals(invocation.commandPath().get(index))) {
return false;
}
}
return true;
}
public int specificity() {
int commandSpecificity = commandIds.isEmpty() ? 0 : Math.max(1, 1_000 - commandIds.size());
return commandSpecificity + pathPrefix.size() * 2_000;
}
}