Code cleanup

This commit is contained in:
Nathan Adams
2017-06-21 14:29:26 +02:00
parent 400657b472
commit c2141ce38b
17 changed files with 58 additions and 65 deletions
@@ -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;
}
}