Rename node executor

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-07-17 22:41:28 +02:00
parent c885ca1d7c
commit 07a0fb129c
4 changed files with 31 additions and 31 deletions

View File

@ -38,12 +38,12 @@ sealed interface Graph permits GraphImpl {
sealed interface Node permits GraphImpl.NodeImpl { sealed interface Node permits GraphImpl.NodeImpl {
@NotNull Argument<?> argument(); @NotNull Argument<?> argument();
@UnknownNullability Executor executor(); @UnknownNullability Execution execution();
@NotNull List<@NotNull Node> next(); @NotNull List<@NotNull Node> next();
} }
sealed interface Executor extends Predicate<CommandSender> permits GraphImpl.ExecutorImpl { sealed interface Execution extends Predicate<CommandSender> permits GraphImpl.ExecutionImpl {
// TODO execute the node // TODO execute the node
} }

View File

@ -32,9 +32,9 @@ final class GraphConverter {
private static int[] append(Graph.Node graphNode, List<DeclareCommandsPacket.Node> to, private static int[] append(Graph.Node graphNode, List<DeclareCommandsPacket.Node> to,
List<Consumer<Integer>> rootRedirect, AtomicInteger id, @Nullable AtomicInteger redirect, List<Consumer<Integer>> rootRedirect, AtomicInteger id, @Nullable AtomicInteger redirect,
List<Runnable> redirectSetters, @Nullable Player player) { List<Runnable> redirectSetters, @Nullable Player player) {
final Graph.Executor executor = graphNode.executor(); final Graph.Execution execution = graphNode.execution();
if (player != null && executor != null) { if (player != null && execution != null) {
if (!executor.test(player)) return new int[0]; if (!execution.test(player)) return new int[0];
} }
final Argument<?> argument = graphNode.argument(); final Argument<?> argument = graphNode.argument();

View File

@ -57,7 +57,7 @@ record GraphImpl(NodeImpl root) implements Graph {
} }
} }
record NodeImpl(Argument<?> argument, ExecutorImpl executor, List<Graph.Node> next) implements Graph.Node { record NodeImpl(Argument<?> argument, ExecutionImpl execution, List<Graph.Node> next) implements Graph.Node {
static NodeImpl fromBuilder(BuilderImpl builder) { static NodeImpl fromBuilder(BuilderImpl builder) {
final List<BuilderImpl> children = builder.children; final List<BuilderImpl> children = builder.children;
Node[] nodes = new NodeImpl[children.size()]; Node[] nodes = new NodeImpl[children.size()];
@ -74,47 +74,47 @@ record GraphImpl(NodeImpl root) implements Graph {
} }
} }
record ExecutorImpl(Predicate<CommandSender> predicate) implements Graph.Executor { record ExecutionImpl(Predicate<CommandSender> predicate) implements Execution {
@Override @Override
public boolean test(CommandSender commandSender) { public boolean test(CommandSender commandSender) {
return predicate.test(commandSender); return predicate.test(commandSender);
} }
static ExecutorImpl fromCommand(Command command) { static ExecutionImpl fromCommand(Command command) {
final CommandCondition condition = command.getCondition(); final CommandCondition condition = command.getCondition();
if (condition == null) return null; if (condition == null) return null;
return new ExecutorImpl(commandSender -> condition.canUse(commandSender, null)); return new ExecutionImpl(commandSender -> condition.canUse(commandSender, null));
} }
static ExecutorImpl fromSyntax(CommandSyntax syntax) { static ExecutionImpl fromSyntax(CommandSyntax syntax) {
final CommandCondition condition = syntax.getCommandCondition(); final CommandCondition condition = syntax.getCommandCondition();
if (condition == null) return null; if (condition == null) return null;
return new ExecutorImpl(commandSender -> condition.canUse(commandSender, null)); return new ExecutionImpl(commandSender -> condition.canUse(commandSender, null));
} }
} }
private record ConversionNode(Argument<?> argument, ExecutorImpl executor, private record ConversionNode(Argument<?> argument, ExecutionImpl execution,
Map<Argument<?>, ConversionNode> nextMap) { Map<Argument<?>, ConversionNode> nextMap) {
ConversionNode(Argument<?> argument, ExecutorImpl executor) { ConversionNode(Argument<?> argument, ExecutionImpl execution) {
this(argument, executor, new LinkedHashMap<>()); this(argument, execution, new LinkedHashMap<>());
} }
private NodeImpl toNode() { private NodeImpl toNode() {
Node[] nodes = new NodeImpl[nextMap.size()]; Node[] nodes = new NodeImpl[nextMap.size()];
int i = 0; int i = 0;
for (var entry : nextMap.values()) nodes[i++] = entry.toNode(); for (var entry : nextMap.values()) nodes[i++] = entry.toNode();
return new NodeImpl(argument, executor, List.of(nodes)); return new NodeImpl(argument, execution, List.of(nodes));
} }
static ConversionNode fromCommand(Command command) { static ConversionNode fromCommand(Command command) {
ConversionNode root = new ConversionNode(Literal(command.getName()), ExecutorImpl.fromCommand(command)); ConversionNode root = new ConversionNode(Literal(command.getName()), ExecutionImpl.fromCommand(command));
// Syntaxes // Syntaxes
for (CommandSyntax syntax : command.getSyntaxes()) { for (CommandSyntax syntax : command.getSyntaxes()) {
ConversionNode syntaxNode = root; ConversionNode syntaxNode = root;
for (Argument<?> arg : syntax.getArguments()) { for (Argument<?> arg : syntax.getArguments()) {
boolean last = arg == syntax.getArguments()[syntax.getArguments().length - 1]; boolean last = arg == syntax.getArguments()[syntax.getArguments().length - 1];
syntaxNode = syntaxNode.nextMap.computeIfAbsent(arg, argument -> { syntaxNode = syntaxNode.nextMap.computeIfAbsent(arg, argument -> {
var ex = last ? ExecutorImpl.fromSyntax(syntax) : null; var ex = last ? ExecutionImpl.fromSyntax(syntax) : null;
return new ConversionNode(argument, ex); return new ConversionNode(argument, ex);
}); });
} }

View File

@ -12,7 +12,7 @@ public class GraphConversionExecutorTest {
public void empty() { public void empty() {
final Command foo = new Command("foo"); final Command foo = new Command("foo");
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
assertNull(graph.root().executor()); assertNull(graph.root().execution());
} }
@Test @Test
@ -22,17 +22,17 @@ public class GraphConversionExecutorTest {
{ {
foo.setCondition((sender, commandString) -> true); foo.setCondition((sender, commandString) -> true);
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
var executor = graph.root().executor(); var execution = graph.root().execution();
assertNotNull(executor); assertNotNull(execution);
assertTrue(executor.test(null)); assertTrue(execution.test(null));
} }
// Constant false // Constant false
{ {
foo.setCondition((sender, commandString) -> false); foo.setCondition((sender, commandString) -> false);
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
var executor = graph.root().executor(); var execution = graph.root().execution();
assertNotNull(executor); assertNotNull(execution);
assertFalse(executor.test(null)); assertFalse(execution.test(null));
} }
} }
@ -43,7 +43,7 @@ public class GraphConversionExecutorTest {
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
assertEquals(1, graph.root().next().size()); assertEquals(1, graph.root().next().size());
assertNull(graph.root().next().get(0).executor()); assertNull(graph.root().next().get(0).execution());
} }
@Test @Test
@ -54,9 +54,9 @@ public class GraphConversionExecutorTest {
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
assertEquals(1, graph.root().next().size()); assertEquals(1, graph.root().next().size());
var executor = graph.root().next().get(0).executor(); var execution = graph.root().next().get(0).execution();
assertNotNull(executor); assertNotNull(execution);
assertTrue(executor.test(null)); assertTrue(execution.test(null));
} }
@Test @Test
@ -67,9 +67,9 @@ public class GraphConversionExecutorTest {
var graph = Graph.fromCommand(foo); var graph = Graph.fromCommand(foo);
assertEquals(1, graph.root().next().size()); assertEquals(1, graph.root().next().size());
var executor = graph.root().next().get(0).executor(); var execution = graph.root().next().get(0).execution();
assertNotNull(executor); assertNotNull(execution);
assertFalse(executor.test(null)); assertFalse(execution.test(null));
} }
private static void dummyExecutor(CommandSender sender, CommandContext context) { private static void dummyExecutor(CommandSender sender, CommandContext context) {