From 9b5a7c887cd03362aafac3479965e20b3f9718bb Mon Sep 17 00:00:00 2001 From: Tastybento Date: Sat, 23 Dec 2017 21:58:32 -0800 Subject: [PATCH] Made parent Optional in CompositeCommand to avoid null checks. --- .../api/commands/CompositeCommand.java | 25 ++++++++++--------- .../java/bskyblock/TestIslandCommand.java | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java index 902e75f40..c6470e519 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java @@ -6,6 +6,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -38,9 +39,9 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi */ private boolean onlyPlayer = false; /** - * The parent command to this one. If this is a top-level command it will be null. + * The parent command to this one. If this is a top-level command it will be empty. */ - private final CompositeCommand parent; + private final Optional parent; /** * The permission required to execute this command */ @@ -64,7 +65,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi */ public CompositeCommand(CompositeCommand parent, String label, String... aliases) { super(label); - this.parent = parent; + this.parent = Optional.of(parent); this.subCommandLevel = parent.getLevel() + 1; // Add this sub-command to the parent parent.getSubCommands().put(label, this); @@ -82,12 +83,12 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi /** * This is the top-level command constructor for commands that have no parent. * @param label - string for this command - * @param aliases - aliases for this command + * @param string - aliases for this command */ - public CompositeCommand(String label, String... aliases) { + public CompositeCommand(String label, String... string) { super(label); - this.setAliases(new ArrayList<>(Arrays.asList(aliases))); - this.parent = null; + this.setAliases(new ArrayList<>(Arrays.asList(string))); + this.parent = Optional.empty(); setUsage(""); this.subCommandLevel = 0; // Top level this.subCommands = new LinkedHashMap<>(); @@ -201,7 +202,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi /** * @return the parent command object */ - public CompositeCommand getParent() { + public Optional getParent() { return parent; } @@ -261,11 +262,11 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi @Override public Command setUsage(String usage) { // Go up the chain - CompositeCommand parent = this.getParent(); + Optional parent = this.getParent(); this.usage = this.getLabel() + " " + usage; - while (parent != null) { - this.usage = parent.getLabel() + " " + this.usage; - parent = parent.getParent(); + while (parent.isPresent()) { + this.usage = parent.get().getLabel() + " " + this.usage; + parent = parent.get().getParent(); } this.usage = "/" + this.usage; this.usage = this.usage.trim(); diff --git a/src/test/java/bskyblock/TestIslandCommand.java b/src/test/java/bskyblock/TestIslandCommand.java index 9fcb44a26..79a5adbe0 100644 --- a/src/test/java/bskyblock/TestIslandCommand.java +++ b/src/test/java/bskyblock/TestIslandCommand.java @@ -85,7 +85,7 @@ public class TestIslandCommand { assertEquals(2, testCommand.getAliases().size()); assertEquals("t", testCommand.getAliases().get(0)); assertTrue(testCommand.isOnlyPlayer()); - assertNull(testCommand.getParent()); + assertEquals(testCommand.getParent(), Optional.empty()); assertEquals(Settings.PERMPREFIX + "default.permission", testCommand.getPermission()); // Check commands and aliases match to correct class for (Entry command : testCommand.getSubCommands().entrySet()) {