Code cleanup
This commit is contained in:
parent
400657b472
commit
c2141ce38b
17 changed files with 58 additions and 65 deletions
|
@ -1,3 +1,5 @@
|
|||
import groovy.io.FileType
|
||||
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'maven'
|
||||
|
||||
|
@ -78,7 +80,7 @@ uploadArchives {
|
|||
|
||||
doLast {
|
||||
// Purge all annoying files that arent needed
|
||||
repoDir.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.(xml|xml\.sha1|md5)$/) {
|
||||
repoDir.traverse(type: FileType.FILES, nameFilter: ~/.*\.(xml|xml\.sha1|md5)$/) {
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package com.mojang.brigadier;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
|
@ -13,15 +11,15 @@ import com.mojang.brigadier.tree.CommandNode;
|
|||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import com.mojang.brigadier.tree.RootCommandNode;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandDispatcher<T> {
|
||||
private static final Predicate<CommandNode> HAS_COMMAND = new Predicate<CommandNode>() {
|
||||
@Override
|
||||
public boolean apply(CommandNode input) {
|
||||
return input != null && (input.getCommand() != null || Iterables.any(input.getChildren(), HAS_COMMAND));
|
||||
public boolean test(CommandNode input) {
|
||||
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(HAS_COMMAND));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -40,7 +38,7 @@ public class CommandDispatcher<T> {
|
|||
}
|
||||
|
||||
public void execute(String command, T source) throws CommandException {
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<T>(source));
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
context.getCommand().run(context);
|
||||
}
|
||||
|
||||
|
@ -71,23 +69,18 @@ public class CommandDispatcher<T> {
|
|||
}
|
||||
|
||||
public String getUsage(String command, T source) throws CommandException {
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<T>(source));
|
||||
CommandContext<T> context = parseNodes(root, command, new CommandContextBuilder<>(source));
|
||||
CommandNode base = Iterables.getLast(context.getNodes().keySet());
|
||||
List<CommandNode> children = Lists.newArrayList(Iterables.filter(base.getChildren(), HAS_COMMAND));
|
||||
List<CommandNode> children = base.getChildren().stream().filter(HAS_COMMAND).collect(Collectors.toList());
|
||||
boolean optional = base.getCommand() != null;
|
||||
|
||||
if (children.isEmpty()) {
|
||||
return context.getInput();
|
||||
}
|
||||
|
||||
Collections.sort(children, new Comparator<CommandNode>() {
|
||||
@Override
|
||||
public int compare(CommandNode o1, CommandNode o2) {
|
||||
return ComparisonChain.start()
|
||||
.compareTrueFirst(o1 instanceof LiteralCommandNode, o2 instanceof LiteralCommandNode)
|
||||
.result();
|
||||
}
|
||||
});
|
||||
children.sort((o1, o2) -> ComparisonChain.start()
|
||||
.compareTrueFirst(o1 instanceof LiteralCommandNode, o2 instanceof LiteralCommandNode)
|
||||
.result());
|
||||
|
||||
StringBuilder result = new StringBuilder(context.getInput());
|
||||
result.append(ARGUMENT_SEPARATOR);
|
||||
|
|
|
@ -52,7 +52,7 @@ public class IntegerArgumentType implements CommandArgumentType<Integer> {
|
|||
throw ERROR_TOO_BIG.create(value, maximum);
|
||||
}
|
||||
|
||||
return new ParsedArgument<Integer>(raw, value);
|
||||
return new ParsedArgument<>(raw, value);
|
||||
} catch (NumberFormatException ignored) {
|
||||
throw ERROR_NOT_A_NUMBER.create(raw);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgument
|
|||
}
|
||||
|
||||
public static <T> RequiredArgumentBuilder<T> argument(String name, CommandArgumentType<T> type) {
|
||||
return new RequiredArgumentBuilder<T>(name, type);
|
||||
return new RequiredArgumentBuilder<>(name, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,7 +31,7 @@ public class RequiredArgumentBuilder<T> extends ArgumentBuilder<RequiredArgument
|
|||
}
|
||||
|
||||
public ArgumentCommandNode<T> build() {
|
||||
ArgumentCommandNode<T> result = new ArgumentCommandNode<T>(getName(), getType(), getCommand());
|
||||
ArgumentCommandNode<T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand());
|
||||
|
||||
for (CommandNode argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
|
|
|
@ -36,7 +36,7 @@ public class CommandContextBuilder<T> {
|
|||
}
|
||||
|
||||
public CommandContextBuilder<T> copy() {
|
||||
CommandContextBuilder<T> copy = new CommandContextBuilder<T>(source);
|
||||
CommandContextBuilder<T> copy = new CommandContextBuilder<>(source);
|
||||
copy.command = this.command;
|
||||
copy.arguments.putAll(this.arguments);
|
||||
copy.nodes.putAll(this.nodes);
|
||||
|
@ -44,6 +44,6 @@ public class CommandContextBuilder<T> {
|
|||
}
|
||||
|
||||
public CommandContext<T> build() {
|
||||
return new CommandContext<T>(source, arguments, command, nodes);
|
||||
return new CommandContext<>(source, arguments, command, nodes);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
|
|||
}
|
||||
|
||||
public CommandException create() {
|
||||
return new CommandException(this, ImmutableMap.<String, Object>of());
|
||||
return new CommandException(this, ImmutableMap.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -68,7 +68,6 @@ public class ArgumentCommandNode<T> extends CommandNode {
|
|||
public int hashCode() {
|
||||
int result = name.hashCode();
|
||||
result = 31 * result + type.hashCode();
|
||||
result = 31 * super.hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
|||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
|
@ -31,7 +30,7 @@ public class CommandDispatcherTest {
|
|||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
subject = new CommandDispatcher<Object>();
|
||||
subject = new CommandDispatcher<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -90,7 +89,7 @@ public class CommandDispatcherTest {
|
|||
subject.execute("foo", source);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +102,7 @@ public class CommandDispatcherTest {
|
|||
subject.execute("foo bar", source);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
|
||||
}
|
||||
}
|
||||
|
@ -134,8 +133,8 @@ public class CommandDispatcherTest {
|
|||
subject.execute("foo bar", source);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", "bar")));
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", "bar")));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -26,7 +26,7 @@ public class CommandDispatcherUsagesTest {
|
|||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
subject = new CommandDispatcher<Object>();
|
||||
subject = new CommandDispatcher<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -35,7 +35,7 @@ public class CommandDispatcherUsagesTest {
|
|||
subject.getUsage("foo", source);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
|
||||
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,8 +40,8 @@ public class IntegerArgumentTypeTest {
|
|||
type.parse("fifty");
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", "fifty")));
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", "fifty")));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,8 +51,8 @@ public class IntegerArgumentTypeTest {
|
|||
type.parse("-101");
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_TOO_SMALL));
|
||||
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", -101, "minimum", -100)));
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", -101, "minimum", -100)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,8 +70,8 @@ public class IntegerArgumentTypeTest {
|
|||
type.parse("101");
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_TOO_BIG));
|
||||
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", 101, "maximum", 100)));
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", 101, "maximum", 100)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ public class IntegerArgumentTypeTest {
|
|||
|
||||
@Test
|
||||
public void testGetInteger() throws Exception {
|
||||
CommandContext context = new CommandContextBuilder<Object>(new Object()).withArgument("foo", type.parse("100")).build();
|
||||
CommandContext context = new CommandContextBuilder<>(new Object()).withArgument("foo", type.parse("100")).build();
|
||||
|
||||
assertThat(IntegerArgumentType.getInteger(context, "foo"), is(100));
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public class CommandContextTest {
|
|||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder = new CommandContextBuilder<Object>(source);
|
||||
builder = new CommandContextBuilder<>(source);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
@ -56,13 +56,13 @@ public class CommandContextTest {
|
|||
CommandNode node = mock(CommandNode.class);
|
||||
CommandNode otherNode = mock(CommandNode.class);
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).build(), new CommandContextBuilder<Object>(source).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(otherSource).build(), new CommandContextBuilder<Object>(otherSource).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).withCommand(command).build(), new CommandContextBuilder<Object>(source).withCommand(command).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).withCommand(otherCommand).build(), new CommandContextBuilder<Object>(source).withCommand(otherCommand).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).withArgument("foo", integer().parse("123")).build(), new CommandContextBuilder<Object>(source).withArgument("foo", integer().parse("123")).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).withNode(node, "foo").withNode(otherNode, "bar").build(), new CommandContextBuilder<Object>(source).withNode(node, "foo").withNode(otherNode, "bar").build())
|
||||
.addEqualityGroup(new CommandContextBuilder<Object>(source).withNode(otherNode, "bar").withNode(node, "foo").build(), new CommandContextBuilder<Object>(source).withNode(otherNode, "bar").withNode(node, "foo").build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).build(), new CommandContextBuilder<>(source).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(otherSource).build(), new CommandContextBuilder<>(otherSource).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).withCommand(command).build(), new CommandContextBuilder<>(source).withCommand(command).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).withCommand(otherCommand).build(), new CommandContextBuilder<>(source).withCommand(otherCommand).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).withArgument("foo", integer().parse("123")).build(), new CommandContextBuilder<>(source).withArgument("foo", integer().parse("123")).build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).withNode(node, "foo").withNode(otherNode, "bar").build(), new CommandContextBuilder<>(source).withNode(node, "foo").withNode(otherNode, "bar").build())
|
||||
.addEqualityGroup(new CommandContextBuilder<>(source).withNode(otherNode, "bar").withNode(node, "foo").build(), new CommandContextBuilder<>(source).withNode(otherNode, "bar").withNode(node, "foo").build())
|
||||
.testEquals();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,9 +7,9 @@ public class ParsedArgumentTest {
|
|||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(new ParsedArgument<String>("foo", "bar"), new ParsedArgument<String>("foo", "bar"))
|
||||
.addEqualityGroup(new ParsedArgument<String>("bar", "baz"), new ParsedArgument<String>("bar", "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<String>("foo", "baz"), new ParsedArgument<String>("foo", "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<>("foo", "bar"), new ParsedArgument<>("foo", "bar"))
|
||||
.addEqualityGroup(new ParsedArgument<>("bar", "baz"), new ParsedArgument<>("bar", "baz"))
|
||||
.addEqualityGroup(new ParsedArgument<>("foo", "baz"), new ParsedArgument<>("foo", "baz"))
|
||||
.testEquals();
|
||||
}
|
||||
}
|
|
@ -32,8 +32,8 @@ public class ParameterizedCommandExceptionTypeTest {
|
|||
@Test
|
||||
public void testCreate() throws Exception {
|
||||
CommandException exception = type.create("World");
|
||||
assertThat(exception.getType(), is((CommandExceptionType) type));
|
||||
assertThat(exception.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("name", "World")));
|
||||
assertThat(exception.getType(), is(type));
|
||||
assertThat(exception.getData(), is(ImmutableMap.<String, Object>of("name", "World")));
|
||||
assertThat(exception.getMessage(), is("Hello, World!"));
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ public class SimpleCommandExceptionTypeTest {
|
|||
public void testCreate() throws Exception {
|
||||
SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar");
|
||||
CommandException exception = type.create();
|
||||
assertThat(exception.getType(), is((CommandExceptionType) type));
|
||||
assertThat(exception.getType(), is(type));
|
||||
assertThat(exception.getMessage(), is("bar"));
|
||||
assertThat(exception.getData().values(), empty());
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
|||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = argument("foo", integer()).build();
|
||||
contextBuilder = new CommandContextBuilder<Object>(new Object());
|
||||
contextBuilder = new CommandContextBuilder<>(new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -39,7 +39,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
|||
assertThat(node.parse("123 456", contextBuilder), is("456"));
|
||||
|
||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -47,7 +47,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
|||
assertThat(node.parse("123", contextBuilder), is(""));
|
||||
|
||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,8 +56,8 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
|||
node.parse("foo", contextBuilder);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", "foo")));
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", "foo")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
|||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = literal("foo").build();
|
||||
contextBuilder = new CommandContextBuilder<Object>(new Object());
|
||||
contextBuilder = new CommandContextBuilder<>(new Object());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -48,8 +48,8 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
|||
node.parse("foobar", contextBuilder);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
||||
assertThat(ex.getData(), is((Map<String, Object>)ImmutableMap.<String, Object>of("expected", "foo")));
|
||||
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,8 +59,8 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
|||
node.parse("bar", contextBuilder);
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is((CommandExceptionType) LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
||||
assertThat(ex.getData(), is((Map<String, Object>)ImmutableMap.<String, Object>of("expected", "foo")));
|
||||
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
|
|||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
assertThat(node.parse("foo bar baz", new CommandContextBuilder<Object>(new Object())), is("foo bar baz"));
|
||||
assertThat(node.parse("foo bar baz", new CommandContextBuilder<>(new Object())), is("foo bar baz"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
|
|
Loading…
Reference in a new issue