Change initialized command descriptions to Set

- Set is more efficient if it's frequently used for `contains()`, which is what we use it for after initialization
This commit is contained in:
ljacqu
2015-12-04 21:58:16 +01:00
parent c78e12de04
commit 9140ebe602
4 changed files with 86 additions and 72 deletions
@@ -9,7 +9,10 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
@@ -29,7 +32,7 @@ import static org.mockito.Mockito.verify;
*/
public class CommandHandlerTest {
private static List<CommandDescription> commands;
private static Set<CommandDescription> commands;
private static CommandHandler handler;
@BeforeClass
@@ -42,7 +45,7 @@ public class CommandHandlerTest {
newArgument("password", false), newArgument("confirmation", false));
CommandDescription testBase = createCommand(null, null, singletonList("test"), newArgument("test", true));
commands = asList(authMeBase, testBase);
commands = new HashSet<>(asList(authMeBase, testBase));
handler = new CommandHandler(commands);
}
@@ -58,7 +61,7 @@ public class CommandHandlerTest {
handler.processCommand(sender, bukkitLabel, args);
// then
final CommandDescription loginCmd = commands.get(0).getChildren().get(0);
final CommandDescription loginCmd = getChildWithLabel("login", getCommandWithLabel("authme", commands));
verify(sender, never()).sendMessage(anyString());
verify(loginCmd.getExecutableCommand()).executeCommand(
eq(sender), any(CommandParts.class), any(CommandParts.class));
@@ -78,10 +81,7 @@ public class CommandHandlerTest {
// then
assertThat(result, equalTo(true));
final CommandDescription loginCmd = commands.get(0).getChildren().get(0);
assertSenderGotMessageContaining("help", sender);
verify(loginCmd.getExecutableCommand()).executeCommand(
eq(sender), any(CommandParts.class), any(CommandParts.class));
}
private static CommandDescription createCommand(PlayerPermission permission, CommandDescription parent,
@@ -112,4 +112,22 @@ public class CommandHandlerTest {
verify(sender).sendMessage(captor.capture());
assertThat(captor.getValue(), stringContainsInOrder(text));
}
private static CommandDescription getCommandWithLabel(String label, Collection<CommandDescription> commands) {
for (CommandDescription command : commands) {
if (command.getLabels().contains(label)) {
return command;
}
}
return null;
}
private static CommandDescription getChildWithLabel(String label, CommandDescription command) {
for (CommandDescription child : command.getChildren()) {
if (child.getLabels().contains(label)) {
return child;
}
}
return null;
}
}
@@ -5,12 +5,7 @@ import fr.xephi.authme.util.WrapperMock;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.equalTo;
@@ -30,7 +25,7 @@ public class CommandInitializerTest {
*/
private static int MAX_ALLOWED_DEPTH = 1;
private static List<CommandDescription> commands;
private static Set<CommandDescription> commands;
@BeforeClass
public static void initializeCommandManager() {
@@ -232,11 +227,11 @@ public class CommandInitializerTest {
// ------------
// Helper methods
// ------------
private static void walkThroughCommands(List<CommandDescription> commands, BiConsumer consumer) {
private static void walkThroughCommands(Collection<CommandDescription> commands, BiConsumer consumer) {
walkThroughCommands(commands, consumer, 0);
}
private static void walkThroughCommands(List<CommandDescription> commands, BiConsumer consumer, int depth) {
private static void walkThroughCommands(Collection<CommandDescription> commands, BiConsumer consumer, int depth) {
for (CommandDescription command : commands) {
consumer.accept(command, depth);
if (command.hasChildren()) {