Code cleanup

This commit is contained in:
Nathan Adams 2017-07-25 16:07:03 +02:00
parent 3bb1a888fb
commit d0be598676
38 changed files with 221 additions and 252 deletions

View file

@ -36,22 +36,22 @@ public class CommandDispatcher<S> {
private final RootCommandNode<S> root = new RootCommandNode<>();
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
@Override
public boolean test(CommandNode<S> input) {
public boolean test(final CommandNode<S> input) {
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
}
};
public void register(LiteralArgumentBuilder<S> command) {
public void register(final LiteralArgumentBuilder<S> command) {
final LiteralCommandNode<S> build = command.build();
root.addChild(build);
}
public int execute(String input, S source) throws CommandException {
public int execute(final String input, final S source) throws CommandException {
final ParseResults<S> parse = parse(input, source);
return execute(parse);
}
public int execute(ParseResults<S> parse) throws CommandException {
public int execute(final ParseResults<S> parse) throws CommandException {
if (parse.getRemaining().length() > 0) {
if (parse.getExceptions().size() == 1) {
throw parse.getExceptions().values().iterator().next();
@ -61,32 +61,32 @@ public class CommandDispatcher<S> {
throw ERROR_UNKNOWN_ARGUMENT.create(parse.getRemaining());
}
}
CommandContext<S> context = parse.getContext().build();
Command<S> command = context.getCommand();
final CommandContext<S> context = parse.getContext().build();
final Command<S> command = context.getCommand();
if (command == null) {
throw ERROR_UNKNOWN_COMMAND.create();
}
return command.run(context);
}
public ParseResults<S> parse(String command, S source) throws CommandException {
StringReader reader = new StringReader(command);
public ParseResults<S> parse(final String command, final S source) throws CommandException {
final StringReader reader = new StringReader(command);
return parseNodes(root, reader, new CommandContextBuilder<>(this, source));
}
private ParseResults<S> parseNodes(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
final S source = contextBuilder.getSource();
Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
final Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
for (CommandNode<S> child : node.getChildren()) {
for (final CommandNode<S> child : node.getChildren()) {
if (!child.canUse(source)) {
continue;
}
CommandContextBuilder<S> context = contextBuilder.copy();
int cursor = reader.getCursor();
final CommandContextBuilder<S> context = contextBuilder.copy();
final int cursor = reader.getCursor();
try {
child.parse(reader, context);
} catch (CommandException ex) {
} catch (final CommandException ex) {
errors.put(child, ex);
reader.setCursor(cursor);
continue;
@ -107,13 +107,13 @@ public class CommandDispatcher<S> {
return new ParseResults<>(contextBuilder, reader.getRemaining(), errors);
}
public String[] getAllUsage(CommandNode<S> node, S source) {
public String[] getAllUsage(final CommandNode<S> node, final S source) {
final ArrayList<String> result = Lists.newArrayList();
getAllUsage(node, source, result, "");
getAllUsage(node, source, result, "");
return result.toArray(new String[result.size()]);
}
private void getAllUsage(CommandNode<S> node, S source, ArrayList<String> result, String prefix) {
private void getAllUsage(final CommandNode<S> node, final S source, final ArrayList<String> result, final String prefix) {
if (!node.canUse(source)) {
return;
}
@ -129,12 +129,12 @@ public class CommandDispatcher<S> {
}
}
public Map<CommandNode<S>, String> getSmartUsage(CommandNode<S> node, S source) {
Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
final Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
final boolean optional = node.getCommand() != null;
for (CommandNode<S> child : node.getChildren()) {
String usage = getSmartUsage(child, source, optional, false);
for (final CommandNode<S> child : node.getChildren()) {
final String usage = getSmartUsage(child, source, optional, false);
if (usage != null) {
result.put(child, usage);
}
@ -142,15 +142,15 @@ public class CommandDispatcher<S> {
return result;
}
private String getSmartUsage(CommandNode<S> node, S source, boolean optional, boolean deep) {
private String getSmartUsage(final CommandNode<S> node, final S source, final boolean optional, final boolean deep) {
if (!node.canUse(source)) {
return null;
}
String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
boolean childOptional = node.getCommand() != null;
String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
final String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
final boolean childOptional = node.getCommand() != null;
final String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
final String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
if (!deep) {
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
@ -160,7 +160,7 @@ public class CommandDispatcher<S> {
return self + ARGUMENT_SEPARATOR + usage;
}
} else if (children.size() > 1) {
Set<String> childUsage = Sets.newLinkedHashSet();
final Set<String> childUsage = Sets.newLinkedHashSet();
for (final CommandNode<S> child : children) {
final String usage = getSmartUsage(child, source, childOptional, true);
if (usage != null) {
@ -171,7 +171,7 @@ public class CommandDispatcher<S> {
final String usage = childUsage.iterator().next();
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
} else if (childUsage.size() > 1) {
StringBuilder builder = new StringBuilder(open);
final StringBuilder builder = new StringBuilder(open);
int count = 0;
for (final CommandNode<S> child : children) {
if (count > 0) {
@ -191,14 +191,14 @@ public class CommandDispatcher<S> {
return self;
}
private Set<String> findSuggestions(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder, Set<String> result) {
private Set<String> findSuggestions(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder, final Set<String> result) {
final S source = contextBuilder.getSource();
for (CommandNode<S> child : node.getChildren()) {
for (final CommandNode<S> child : node.getChildren()) {
if (!child.canUse(source)) {
continue;
}
CommandContextBuilder<S> context = contextBuilder.copy();
int cursor = reader.getCursor();
final CommandContextBuilder<S> context = contextBuilder.copy();
final int cursor = reader.getCursor();
try {
child.parse(reader, context);
if (reader.canRead()) {
@ -210,7 +210,7 @@ public class CommandDispatcher<S> {
reader.setCursor(cursor);
child.listSuggestions(reader.getRemaining(), result, context);
}
} catch (CommandException e) {
} catch (final CommandException e) {
reader.setCursor(cursor);
child.listSuggestions(reader.getRemaining(), result, context);
}
@ -219,8 +219,8 @@ public class CommandDispatcher<S> {
return result;
}
public String[] getCompletionSuggestions(String command, S source) {
StringReader reader = new StringReader(command);
public String[] getCompletionSuggestions(final String command, final S source) {
final StringReader reader = new StringReader(command);
final Set<String> nodes = findSuggestions(root, reader, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet());
return nodes.toArray(new String[nodes.size()]);

View file

@ -12,13 +12,13 @@ public class ParseResults<S> {
private final String remaining;
private final Map<CommandNode<S>, CommandException> exceptions;
public ParseResults(CommandContextBuilder<S> context, String remaining, Map<CommandNode<S>, CommandException> exceptions) {
public ParseResults(final CommandContextBuilder<S> context, final String remaining, final Map<CommandNode<S>, CommandException> exceptions) {
this.context = context;
this.remaining = remaining;
this.exceptions = exceptions;
}
public ParseResults(CommandContextBuilder<S> context) {
public ParseResults(final CommandContextBuilder<S> context) {
this(context, "", Collections.emptyMap());
}

View file

@ -20,7 +20,7 @@ public class StringReader {
private final String string;
private int cursor;
public StringReader(String string) {
public StringReader(final String string) {
this.string = string;
}
@ -28,7 +28,7 @@ public class StringReader {
return string;
}
public void setCursor(int cursor) {
public void setCursor(final int cursor) {
this.cursor = cursor;
}
@ -116,7 +116,7 @@ public class StringReader {
|| c == '.' || c == '+';
}
public String readUnquotedString() throws CommandException {
public String readUnquotedString() {
final int start = cursor;
while (canRead() && isAllowedInUnquotedString(peek())) {
skip();
@ -163,7 +163,7 @@ public class StringReader {
}
public boolean readBoolean() throws CommandException {
String value = readString();
final String value = readString();
if (value.equals("true")) {
return true;
} else if (value.equals("false")) {

View file

@ -2,7 +2,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import java.util.Set;
@ -10,7 +9,8 @@ import java.util.Set;
public interface ArgumentType<T> {
<S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {}
default <S> void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
}
default String getUsageSuffix() {
return null;

View file

@ -1,16 +1,12 @@
package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
public class BoolArgumentType implements ArgumentType<Boolean> {
public static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType("argument.bool.invalid", "Value must be true or false");
private BoolArgumentType() {
}
@ -18,12 +14,12 @@ public class BoolArgumentType implements ArgumentType<Boolean> {
return new BoolArgumentType();
}
public static boolean getBool(CommandContext<?> context, String name) {
public static boolean getBool(final CommandContext<?> context, final String name) {
return context.getArgument(name, Boolean.class);
}
@Override
public <S> Boolean parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
public <S> Boolean parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
return reader.readBoolean();
}

View file

@ -1,10 +1,8 @@
package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
@ -19,7 +17,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
private final int maximum;
private final String suffix;
private IntegerArgumentType(int minimum, int maximum, String suffix) {
private IntegerArgumentType(final int minimum, final int maximum, final String suffix) {
this.minimum = minimum;
this.maximum = maximum;
this.suffix = suffix;
@ -29,25 +27,25 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
return integer(Integer.MIN_VALUE);
}
public static IntegerArgumentType integer(int min) {
public static IntegerArgumentType integer(final int min) {
return integer(min, Integer.MAX_VALUE);
}
public static IntegerArgumentType integer(int min, int max) {
public static IntegerArgumentType integer(final int min, final int max) {
return integer(min, max, "");
}
public static IntegerArgumentType integer(int min, int max, String suffix) {
public static IntegerArgumentType integer(final int min, final int max, final String suffix) {
return new IntegerArgumentType(min, max, suffix);
}
public static int getInteger(CommandContext<?> context, String name) {
public static int getInteger(final CommandContext<?> context, final String name) {
return context.getArgument(name, int.class);
}
@Override
public <S> Integer parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
int result = reader.readInt();
public <S> Integer parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
final int result = reader.readInt();
for (int i = 0; i < suffix.length(); i++) {
if (reader.canRead() && reader.peek() == suffix.charAt(i)) {
reader.skip();
@ -65,11 +63,11 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof IntegerArgumentType)) return false;
IntegerArgumentType that = (IntegerArgumentType) o;
final IntegerArgumentType that = (IntegerArgumentType) o;
return maximum == that.maximum && minimum == that.minimum && Objects.equals(suffix, that.suffix);
}

View file

@ -4,15 +4,12 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
public class StringArgumentType implements ArgumentType<String> {
private final StringType type;
private StringArgumentType(StringType type) {
private StringArgumentType(final StringType type) {
this.type = type;
}
@ -28,14 +25,14 @@ public class StringArgumentType implements ArgumentType<String> {
return new StringArgumentType(StringType.GREEDY_PHRASE);
}
public static String getString(CommandContext<?> context, String name) {
public static String getString(final CommandContext<?> context, final String name) {
return context.getArgument(name, String.class);
}
@Override
public <S> String parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
public <S> String parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
if (type == StringType.GREEDY_PHRASE) {
String text = reader.getRemaining();
final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
return text;
} else if (type == StringType.SINGLE_WORD) {
@ -50,15 +47,15 @@ public class StringArgumentType implements ArgumentType<String> {
return "string()";
}
public static String escapeIfRequired(String input) {
public static String escapeIfRequired(final String input) {
if (input.contains("\\") || input.contains("\"") || input.contains(CommandDispatcher.ARGUMENT_SEPARATOR)) {
return escape(input);
}
return input;
}
private static String escape(String input) {
StringBuilder result = new StringBuilder("\"");
private static String escape(final String input) {
final StringBuilder result = new StringBuilder("\"");
for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);

View file

@ -14,7 +14,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
protected abstract T getThis();
public T then(ArgumentBuilder<S, ?> argument) {
public T then(final ArgumentBuilder<S, ?> argument) {
arguments.addChild(argument.build());
return getThis();
}
@ -23,7 +23,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return arguments.getChildren();
}
public T executes(Command<S> command) {
public T executes(final Command<S> command) {
this.command = command;
return getThis();
}
@ -32,7 +32,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return command;
}
public T requires(Predicate<S> requirement) {
public T requires(final Predicate<S> requirement) {
this.requirement = requirement;
return getThis();
}

View file

@ -6,11 +6,11 @@ import com.mojang.brigadier.tree.LiteralCommandNode;
public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumentBuilder<S>> {
private final String literal;
protected LiteralArgumentBuilder(String literal) {
protected LiteralArgumentBuilder(final String literal) {
this.literal = literal;
}
public static <S> LiteralArgumentBuilder<S> literal(String name) {
public static <S> LiteralArgumentBuilder<S> literal(final String name) {
return new LiteralArgumentBuilder<>(name);
}
@ -25,9 +25,9 @@ public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumen
@Override
public LiteralCommandNode<S> build() {
LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
for (CommandNode<S> argument : getArguments()) {
for (final CommandNode<S> argument : getArguments()) {
result.addChild(argument);
}

View file

@ -8,12 +8,12 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
private final String name;
private final ArgumentType<T> type;
private RequiredArgumentBuilder(String name, ArgumentType<T> type) {
private RequiredArgumentBuilder(final String name, final ArgumentType<T> type) {
this.name = name;
this.type = type;
}
public static <S, T> RequiredArgumentBuilder<S, T> argument(String name, ArgumentType<T> type) {
public static <S, T> RequiredArgumentBuilder<S, T> argument(final String name, final ArgumentType<T> type) {
return new RequiredArgumentBuilder<>(name, type);
}
@ -31,9 +31,9 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
}
public ArgumentCommandNode<S, T> build() {
ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
final ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
for (CommandNode<S> argument : getArguments()) {
for (final CommandNode<S> argument : getArguments()) {
result.addChild(argument);
}

View file

@ -15,7 +15,7 @@ public class CommandContext<S> {
private final Map<CommandNode<S>, String> nodes;
private final String input;
public CommandContext(S source, Map<String, ParsedArgument<S, ?>> arguments, Command<S> command, Map<CommandNode<S>, String> nodes, String input) {
public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, String> nodes, final String input) {
this.source = source;
this.arguments = arguments;
this.command = command;
@ -32,8 +32,8 @@ public class CommandContext<S> {
}
@SuppressWarnings("unchecked")
public <V> V getArgument(String name, Class<V> clazz) {
ParsedArgument<S, ?> argument = arguments.get(name);
public <V> V getArgument(final String name, final Class<V> clazz) {
final ParsedArgument<S, ?> argument = arguments.get(name);
if (argument == null) {
throw new IllegalArgumentException("No such argument '" + name + "' exists on this command");
@ -48,11 +48,11 @@ public class CommandContext<S> {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof CommandContext)) return false;
CommandContext that = (CommandContext) o;
final CommandContext that = (CommandContext) o;
if (!arguments.equals(that.arguments)) return false;
if (!Iterables.elementsEqual(nodes.entrySet(), that.nodes.entrySet())) return false;
@ -80,7 +80,7 @@ public class CommandContext<S> {
}
public CommandContext<S> copy() {
Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
final Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
this.arguments.forEach((k, v) -> arguments.put(k, v.copy()));
return new CommandContext<>(source, arguments, command, nodes, input);
}

View file

@ -15,12 +15,12 @@ public class CommandContextBuilder<S> {
private S source;
private Command<S> command;
public CommandContextBuilder(CommandDispatcher<S> dispatcher, S source) {
public CommandContextBuilder(final CommandDispatcher<S> dispatcher, final S source) {
this.dispatcher = dispatcher;
this.source = source;
}
public CommandContextBuilder<S> withSource(S source) {
public CommandContextBuilder<S> withSource(final S source) {
this.source = source;
return this;
}
@ -29,7 +29,7 @@ public class CommandContextBuilder<S> {
return source;
}
public CommandContextBuilder<S> withArgument(String name, ParsedArgument<S, ?> argument) {
public CommandContextBuilder<S> withArgument(final String name, final ParsedArgument<S, ?> argument) {
this.arguments.put(name, argument);
return this;
}
@ -38,12 +38,12 @@ public class CommandContextBuilder<S> {
return arguments;
}
public CommandContextBuilder<S> withCommand(Command<S> command) {
public CommandContextBuilder<S> withCommand(final Command<S> command) {
this.command = command;
return this;
}
public CommandContextBuilder<S> withNode(CommandNode<S> node, String raw) {
public CommandContextBuilder<S> withNode(final CommandNode<S> node, final String raw) {
if (!nodes.isEmpty()) {
input.append(CommandDispatcher.ARGUMENT_SEPARATOR);
}
@ -53,7 +53,7 @@ public class CommandContextBuilder<S> {
}
public CommandContextBuilder<S> copy() {
CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
final CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
copy.command = this.command;
arguments.forEach((k, v) -> copy.arguments.put(k, v.copy()));
copy.nodes.putAll(this.nodes);

View file

@ -4,7 +4,7 @@ public class ParsedArgument<S, T> {
private final String raw;
private final T result;
public ParsedArgument(String raw, T result) {
public ParsedArgument(final String raw, final T result) {
this.raw = raw;
this.result = result;
}
@ -18,11 +18,11 @@ public class ParsedArgument<S, T> {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof ParsedArgument)) return false;
ParsedArgument that = (ParsedArgument) o;
final ParsedArgument that = (ParsedArgument) o;
if (!raw.equals(that.raw)) return false;
if (!result.equals(that.result)) return false;

View file

@ -6,7 +6,7 @@ public class CommandException extends Exception {
private final CommandExceptionType type;
private final Map<String, Object> data;
public CommandException(CommandExceptionType type, Map<String, Object> data) {
public CommandException(final CommandExceptionType type, final Map<String, Object> data) {
this.type = type;
this.data = data;
}

View file

@ -2,5 +2,6 @@ package com.mojang.brigadier.exceptions;
public interface CommandExceptionType {
String getTypeName();
String getErrorMessage(CommandException exception);
}

View file

@ -7,14 +7,14 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ParameterizedCommandExceptionType implements CommandExceptionType {
private static Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}");
private static final Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}");
private static final Joiner JOINER = Joiner.on(", ");
private final String name;
private final String message;
private final String[] keys;
public ParameterizedCommandExceptionType(String name, String message, String... keys) {
public ParameterizedCommandExceptionType(final String name, final String message, final String... keys) {
this.name = name;
this.message = message;
this.keys = keys;
@ -26,7 +26,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
}
@Override
public String getErrorMessage(CommandException exception) {
public String getErrorMessage(final CommandException exception) {
final Matcher matcher = PATTERN.matcher(message);
final StringBuffer result = new StringBuffer();
while (matcher.find()) {
@ -36,7 +36,7 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
return result.toString();
}
public CommandException create(Object... values) {
public CommandException create(final Object... values) {
if (values.length != keys.length) {
throw new IllegalArgumentException("Invalid values! (Expected: " + JOINER.join(keys) + ")");
}
@ -51,11 +51,11 @@ public class ParameterizedCommandExceptionType implements CommandExceptionType {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof CommandExceptionType)) return false;
CommandExceptionType that = (CommandExceptionType) o;
final CommandExceptionType that = (CommandExceptionType) o;
return getTypeName().equals(that.getTypeName());
}

View file

@ -6,7 +6,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
private final String name;
private final String message;
public SimpleCommandExceptionType(String name, String message) {
public SimpleCommandExceptionType(final String name, final String message) {
this.name = name;
this.message = message;
}
@ -17,7 +17,7 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
}
@Override
public String getErrorMessage(CommandException exception) {
public String getErrorMessage(final CommandException exception) {
return message;
}
@ -26,11 +26,11 @@ public class SimpleCommandExceptionType implements CommandExceptionType {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof CommandExceptionType)) return false;
CommandExceptionType that = (CommandExceptionType) o;
final CommandExceptionType that = (CommandExceptionType) o;
return getTypeName().equals(that.getTypeName());
}

View file

@ -18,7 +18,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
private final String name;
private final ArgumentType<T> type;
public ArgumentCommandNode(String name, ArgumentType<T> type, Command<S> command, Predicate<S> requirement) {
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement) {
super(command, requirement);
this.name = name;
this.type = type;
@ -51,17 +51,17 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
}
@Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
int start = reader.getCursor();
T result = type.parse(reader, contextBuilder);
ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
final int start = reader.getCursor();
final T result = type.parse(reader, contextBuilder);
final ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
contextBuilder.withArgument(name, parsed);
contextBuilder.withNode(this, parsed.getRaw());
}
@Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
type.listSuggestions(command, output, contextBuilder);
}
@ -76,11 +76,11 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof ArgumentCommandNode)) return false;
ArgumentCommandNode that = (ArgumentCommandNode) o;
final ArgumentCommandNode that = (ArgumentCommandNode) o;
if (!name.equals(that.name)) return false;
if (!type.equals(that.type)) return false;

View file

@ -17,10 +17,10 @@ import java.util.stream.Collectors;
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
private final Predicate<S> requirement;
private Command<S> command;
private Predicate<S> requirement;
protected CommandNode(Command<S> command, Predicate<S> requirement) {
protected CommandNode(final Command<S> command, final Predicate<S> requirement) {
this.command = command;
this.requirement = requirement;
}
@ -33,18 +33,18 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
return children.values();
}
public boolean canUse(S source) {
public boolean canUse(final S source) {
return requirement.test(source);
}
public void addChild(CommandNode<S> node) {
CommandNode<S> child = children.get(node.getMergeKey());
public void addChild(final CommandNode<S> node) {
final CommandNode<S> child = children.get(node.getMergeKey());
if (child != null) {
// We've found something to merge onto
if (node.getCommand() != null) {
child.command = node.getCommand();
}
for (CommandNode<S> grandchild : node.getChildren()) {
for (final CommandNode<S> grandchild : node.getChildren()) {
child.addChild(grandchild);
}
} else {
@ -55,11 +55,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof CommandNode)) return false;
CommandNode<S> that = (CommandNode<S>) o;
final CommandNode<S> that = (CommandNode<S>) o;
if (!children.equals(that.children)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false;
@ -89,7 +89,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
protected abstract String getSortedKey();
@Override
public int compareTo(CommandNode<S> o) {
public int compareTo(final CommandNode<S> o) {
return ComparisonChain
.start()
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)

View file

@ -1,7 +1,6 @@
package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder;
@ -16,7 +15,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal;
public LiteralCommandNode(String literal, Command<S> command, Predicate<S> requirement) {
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement) {
super(command, requirement);
this.literal = literal;
}
@ -31,7 +30,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
}
@Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
for (int i = 0; i < literal.length(); i++) {
if (reader.canRead() && reader.peek() == literal.charAt(i)) {
reader.skip();
@ -44,18 +43,18 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
}
@Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
if (literal.startsWith(command)) {
output.add(literal);
}
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof LiteralCommandNode)) return false;
LiteralCommandNode that = (LiteralCommandNode) o;
final LiteralCommandNode that = (LiteralCommandNode) o;
if (!literal.equals(that.literal)) return false;
return super.equals(o);

View file

@ -23,15 +23,15 @@ public class RootCommandNode<S> extends CommandNode<S> {
}
@Override
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
public void parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandException {
}
@Override
public void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
public void listSuggestions(final String command, final Set<String> output, final CommandContextBuilder<S> contextBuilder) {
}
@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof RootCommandNode)) return false;
return super.equals(o);

View file

@ -6,14 +6,11 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherCompletionsTest {
@ -36,9 +33,9 @@ public class CommandDispatcherCompletionsTest {
subject.register(literal("foo"));
subject.register(literal("bar"));
subject.register(literal("baz").requires(s -> false));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[] {"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[] {"foo"}));
assertThat(subject.getCompletionSuggestions("b", source), equalTo(new String[] {"bar"}));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[]{"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("b", source), equalTo(new String[]{"bar"}));
assertThat(subject.getCompletionSuggestions("q", source), is(emptyArray()));
}
@ -46,12 +43,12 @@ public class CommandDispatcherCompletionsTest {
public void testSubCommand() throws Exception {
subject.register(literal("foo").then(literal("abc")).then(literal("def")).then(literal("ghi").requires(s -> false)));
subject.register(literal("bar"));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[] {"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[] {"foo"}));
assertThat(subject.getCompletionSuggestions("foo", source), equalTo(new String[] {"foo"}));
assertThat(subject.getCompletionSuggestions("foo ", source), equalTo(new String[] {"abc", "def"}));
assertThat(subject.getCompletionSuggestions("foo a", source), equalTo(new String[] {"abc"}));
assertThat(subject.getCompletionSuggestions("foo d", source), equalTo(new String[] {"def"}));
assertThat(subject.getCompletionSuggestions("", source), equalTo(new String[]{"bar", "foo"}));
assertThat(subject.getCompletionSuggestions("f", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("foo", source), equalTo(new String[]{"foo"}));
assertThat(subject.getCompletionSuggestions("foo ", source), equalTo(new String[]{"abc", "def"}));
assertThat(subject.getCompletionSuggestions("foo a", source), equalTo(new String[]{"abc"}));
assertThat(subject.getCompletionSuggestions("foo d", source), equalTo(new String[]{"def"}));
assertThat(subject.getCompletionSuggestions("foo g", source), is(emptyArray()));
}
}

View file

@ -1,10 +1,7 @@
package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import org.junit.Before;
import org.junit.Test;
@ -13,7 +10,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
@ -21,7 +17,11 @@ 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.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherTest {
@ -65,7 +65,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
@ -78,7 +78,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
@ -91,7 +91,7 @@ public class CommandDispatcherTest {
try {
subject.execute("", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
@ -104,7 +104,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo bar", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT));
assertThat(ex.getData(), is(Collections.singletonMap("argument", "bar")));
}
@ -117,7 +117,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo baz", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is(Collections.singletonMap("expected", "bar")));
}
@ -134,7 +134,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo unknown", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_ARGUMENT));
assertThat(ex.getData(), is(Collections.singletonMap("argument", "unknown")));
}
@ -143,7 +143,7 @@ public class CommandDispatcherTest {
@SuppressWarnings("unchecked")
@Test
public void testExecuteSubcommand() throws Exception {
Command<Object> subCommand = mock(Command.class);
final Command<Object> subCommand = mock(Command.class);
when(subCommand.run(any())).thenReturn(100);
subject.register(literal("foo").then(
@ -167,7 +167,7 @@ public class CommandDispatcherTest {
try {
subject.execute("foo bar", source);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
assertThat(ex.getData(), is(Collections.emptyMap()));
}

View file

@ -2,7 +2,6 @@ package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.tree.CommandNode;
import org.junit.Before;
@ -13,15 +12,12 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherUsagesTest {
@ -93,10 +89,10 @@ public class CommandDispatcherUsagesTest {
);
}
private CommandNode<Object> get(String command) {
private CommandNode<Object> get(final String command) {
try {
return Iterators.getLast(subject.parse(command, source).getContext().getNodes().keySet().iterator());
} catch (CommandException e) {
} catch (final CommandException e) {
throw new AssertionError("get() failed unexpectedly", e);
}
}
@ -118,7 +114,7 @@ public class CommandDispatcherUsagesTest {
@Test
public void testAllUsage_root() throws Exception {
final String[] results = subject.getAllUsage(subject.getRoot(), source);
assertThat(results, equalTo(new String[] {
assertThat(results, equalTo(new String[]{
"a 1 i",
"a 1 ii",
"a 2 i",
@ -147,15 +143,15 @@ public class CommandDispatcherUsagesTest {
public void testSmartUsage_root() throws Exception {
final Map<CommandNode<Object>, String> results = subject.getSmartUsage(subject.getRoot(), source);
assertThat(results, equalTo(ImmutableMap.builder()
.put(get("a"), "a (1|2)")
.put(get("b"), "b 1")
.put(get("c"), "c")
.put(get("e"), "e [1]")
.put(get("f"), "f (1|2)")
.put(get("g"), "g [1]")
.put(get("h"), "h [1|2|3]")
.put(get("i"), "i [1|2]")
.build()
.put(get("a"), "a (1|2)")
.put(get("b"), "b 1")
.put(get("c"), "c")
.put(get("e"), "e [1]")
.put(get("f"), "f (1|2)")
.put(get("g"), "g [1]")
.put(get("h"), "h [1|2|3]")
.put(get("i"), "i [1|2]")
.build()
));
}

View file

@ -1,9 +1,7 @@
package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandException;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.Collections;
@ -15,7 +13,7 @@ import static org.junit.Assert.assertThat;
public class StringReaderTest {
@Test
public void canRead() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
assertThat(reader.canRead(), is(true));
reader.skip(); // 'a'
assertThat(reader.canRead(), is(true));
@ -27,7 +25,7 @@ public class StringReaderTest {
@Test
public void getRemainingLength() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
assertThat(reader.getRemainingLength(), is(3));
reader.setCursor(1);
assertThat(reader.getRemainingLength(), is(2));
@ -39,7 +37,7 @@ public class StringReaderTest {
@Test
public void canRead_length() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
assertThat(reader.canRead(1), is(true));
assertThat(reader.canRead(2), is(true));
assertThat(reader.canRead(3), is(true));
@ -49,7 +47,7 @@ public class StringReaderTest {
@Test
public void peek() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
assertThat(reader.peek(), is('a'));
assertThat(reader.getCursor(), is(0));
reader.setCursor(2);
@ -59,7 +57,7 @@ public class StringReaderTest {
@Test
public void read() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
assertThat(reader.read(), is('a'));
assertThat(reader.read(), is('b'));
assertThat(reader.read(), is('c'));
@ -68,14 +66,14 @@ public class StringReaderTest {
@Test
public void skip() throws Exception {
StringReader reader = new StringReader("abc");
final StringReader reader = new StringReader("abc");
reader.skip();
assertThat(reader.getCursor(), is(1));
}
@Test
public void getRemaining() throws Exception {
StringReader reader = new StringReader("Hello!");
final StringReader reader = new StringReader("Hello!");
assertThat(reader.getRemaining(), equalTo("Hello!"));
reader.setCursor(3);
assertThat(reader.getRemaining(), equalTo("lo!"));
@ -85,7 +83,7 @@ public class StringReaderTest {
@Test
public void getRead() throws Exception {
StringReader reader = new StringReader("Hello!");
final StringReader reader = new StringReader("Hello!");
assertThat(reader.getRead(), equalTo(""));
reader.setCursor(3);
assertThat(reader.getRead(), equalTo("Hel"));

View file

@ -2,22 +2,16 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import static com.mojang.brigadier.arguments.BoolArgumentType.ERROR_INVALID;
import static com.mojang.brigadier.arguments.BoolArgumentType.bool;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -35,7 +29,7 @@ public class BoolArgumentTypeTest {
@Test
public void parse() throws Exception {
StringReader reader = mock(StringReader.class);
final StringReader reader = mock(StringReader.class);
when(reader.readBoolean()).thenReturn(true);
assertThat(type.parse(reader, context), is(true));
verify(reader).readBoolean();

View file

@ -36,25 +36,25 @@ public class IntegerArgumentTypeTest {
@Test
public void parse_noSuffix() throws Exception {
StringReader reader = new StringReader("15");
final StringReader reader = new StringReader("15");
assertThat(integer().parse(reader, context), is(15));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix() throws Exception {
StringReader reader = new StringReader("15L");
final StringReader reader = new StringReader("15L");
assertThat(integer(0, 100, "L").parse(reader, context), is(15));
assertThat(reader.canRead(), is(false));
}
@Test
public void parse_suffix_incorrect() throws Exception {
StringReader reader = new StringReader("15W");
final StringReader reader = new StringReader("15W");
try {
integer(0, 100, "L").parse(reader, context);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
}
@ -62,11 +62,11 @@ public class IntegerArgumentTypeTest {
@Test
public void parse_suffix_missing() throws Exception {
StringReader reader = new StringReader("15");
final StringReader reader = new StringReader("15");
try {
integer(0, 100, "L").parse(reader, context);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
}
@ -74,11 +74,11 @@ public class IntegerArgumentTypeTest {
@Test
public void parse_tooSmall() throws Exception {
StringReader reader = new StringReader("-5");
final StringReader reader = new StringReader("-5");
try {
integer(0, 100).parse(reader, context);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", -5, "minimum", 0)));
}
@ -86,11 +86,11 @@ public class IntegerArgumentTypeTest {
@Test
public void parse_tooBig() throws Exception {
StringReader reader = new StringReader("5");
final StringReader reader = new StringReader("5");
try {
integer(-100, 0).parse(reader, context);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG));
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", 5, "maximum", 0)));
}
@ -98,7 +98,7 @@ public class IntegerArgumentTypeTest {
@Test
public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet();
final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
type.listSuggestions("", set, context);
assertThat(set, is(empty()));

View file

@ -30,7 +30,7 @@ public class StringArgumentTypeTest {
@Test
public void testParseWord() throws Exception {
StringReader reader = mock(StringReader.class);
final StringReader reader = mock(StringReader.class);
when(reader.readUnquotedString()).thenReturn("hello");
assertThat(word().parse(reader, context), equalTo("hello"));
verify(reader).readUnquotedString();
@ -38,7 +38,7 @@ public class StringArgumentTypeTest {
@Test
public void testParseString() throws Exception {
StringReader reader = mock(StringReader.class);
final StringReader reader = mock(StringReader.class);
when(reader.readString()).thenReturn("hello world");
assertThat(string().parse(reader, context), equalTo("hello world"));
verify(reader).readString();
@ -46,14 +46,14 @@ public class StringArgumentTypeTest {
@Test
public void testParseGreedyString() throws Exception {
StringReader reader = new StringReader("Hello world! This is a test.");
final StringReader reader = new StringReader("Hello world! This is a test.");
assertThat(greedyString().parse(reader, context), equalTo("Hello world! This is a test."));
assertThat(reader.canRead(), is(false));
}
@Test
public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet();
final Set<String> set = Sets.newHashSet();
string().listSuggestions("", set, context);
assertThat(set, is(empty()));
}

View file

@ -6,7 +6,6 @@ import org.junit.Test;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
@ -21,7 +20,7 @@ public class ArgumentBuilderTest {
@Test
public void testArguments() throws Exception {
RequiredArgumentBuilder<Object, ?> argument = argument("bar", integer());
final RequiredArgumentBuilder<Object, ?> argument = argument("bar", integer());
builder.then(argument);

View file

@ -25,14 +25,14 @@ public class LiteralArgumentBuilderTest {
@Test
public void testBuild() throws Exception {
LiteralCommandNode<Object> node = builder.build();
final LiteralCommandNode<Object> node = builder.build();
assertThat(node.getLiteral(), is("foo"));
}
@Test
public void testBuildWithExecutor() throws Exception {
LiteralCommandNode<Object> node = builder.executes(command).build();
final LiteralCommandNode<Object> node = builder.executes(command).build();
assertThat(node.getLiteral(), is("foo"));
assertThat(node.getCommand(), is(command));
@ -42,7 +42,7 @@ public class LiteralArgumentBuilderTest {
public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer()));
builder.then(argument("baz", integer()));
LiteralCommandNode<Object> node = builder.build();
final LiteralCommandNode<Object> node = builder.build();
assertThat(node.getChildren(), hasSize(2));
}

View file

@ -28,7 +28,7 @@ public class RequiredArgumentBuilderTest {
@Test
public void testBuild() throws Exception {
ArgumentCommandNode<Object, Integer> node = builder.build();
final ArgumentCommandNode<Object, Integer> node = builder.build();
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));
@ -36,7 +36,7 @@ public class RequiredArgumentBuilderTest {
@Test
public void testBuildWithExecutor() throws Exception {
ArgumentCommandNode<Object, Integer> node = builder.executes(command).build();
final ArgumentCommandNode<Object, Integer> node = builder.executes(command).build();
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));
@ -47,7 +47,7 @@ public class RequiredArgumentBuilderTest {
public void testBuildWithChildren() throws Exception {
builder.then(argument("bar", integer()));
builder.then(argument("baz", integer()));
ArgumentCommandNode<Object, Integer> node = builder.build();
final ArgumentCommandNode<Object, Integer> node = builder.build();
assertThat(node.getChildren(), hasSize(2));
}

View file

@ -37,13 +37,13 @@ public class CommandContextTest {
@Test(expected = IllegalArgumentException.class)
public void testGetArgument_wrongType() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
context.getArgument("foo", String.class);
}
@Test
public void testGetArgument() throws Exception {
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
assertThat(context.getArgument("foo", int.class), is(123));
}
@ -55,11 +55,11 @@ public class CommandContextTest {
@SuppressWarnings("unchecked")
@Test
public void testEquals() throws Exception {
Object otherSource = new Object();
Command<Object> command = mock(Command.class);
Command<Object> otherCommand = mock(Command.class);
CommandNode<Object> node = mock(CommandNode.class);
CommandNode<Object> otherNode = mock(CommandNode.class);
final Object otherSource = new Object();
final Command<Object> command = mock(Command.class);
final Command<Object> otherCommand = mock(Command.class);
final CommandNode<Object> node = mock(CommandNode.class);
final CommandNode<Object> otherNode = mock(CommandNode.class);
new EqualsTester()
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).build(), new CommandContextBuilder<>(dispatcher, source).build())
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build())
@ -73,7 +73,7 @@ public class CommandContextTest {
@Test
public void testGetInput() throws Exception {
CommandContext<Object> context = builder.withNode(literal("foo").build(), "foo").withNode(argument("bar", integer()).build(), "100").withNode(literal("baz").build(), "baz").build();
final CommandContext<Object> context = builder.withNode(literal("foo").build(), "foo").withNode(argument("bar", integer()).build(), "100").withNode(literal("baz").build(), "baz").build();
assertThat(context.getInput(), is("foo 100 baz"));
}

View file

@ -5,8 +5,6 @@ import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@ -31,7 +29,7 @@ public class ParameterizedCommandExceptionTypeTest {
@Test
public void testCreate() throws Exception {
CommandException exception = type.create("World");
final CommandException exception = type.create("World");
assertThat(exception.getType(), is(type));
assertThat(exception.getData(), is(ImmutableMap.<String, Object>of("name", "World")));
assertThat(exception.getMessage(), is("Hello, World!"));

View file

@ -11,8 +11,8 @@ import static org.junit.Assert.assertThat;
public class SimpleCommandExceptionTypeTest {
@Test
public void testCreate() throws Exception {
SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar");
CommandException exception = type.create();
final SimpleCommandExceptionType type = new SimpleCommandExceptionType("foo", "bar");
final CommandException exception = type.create();
assertThat(exception.getType(), is(type));
assertThat(exception.getMessage(), is("bar"));
assertThat(exception.getData().values(), empty());

View file

@ -13,13 +13,14 @@ import static org.junit.Assert.assertThat;
@RunWith(MockitoJUnitRunner.class)
public abstract class AbstractCommandNodeTest {
@Mock private Command command;
@Mock
private Command command;
protected abstract CommandNode<Object> getCommandNode();
@Test
public void testAddChild() throws Exception {
CommandNode<Object> node = getCommandNode();
final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child1").build());
node.addChild(literal("child2").build());
@ -30,7 +31,7 @@ public abstract class AbstractCommandNodeTest {
@Test
public void testAddChildMergesGrandchildren() throws Exception {
CommandNode<Object> node = getCommandNode();
final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").then(
literal("grandchild1")
@ -46,7 +47,7 @@ public abstract class AbstractCommandNodeTest {
@Test
public void testAddChildPreservesCommand() throws Exception {
CommandNode<Object> node = getCommandNode();
final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").executes(command).build());
node.addChild(literal("child").build());
@ -56,7 +57,7 @@ public abstract class AbstractCommandNodeTest {
@Test
public void testAddChildOverwritesCommand() throws Exception {
CommandNode<Object> node = getCommandNode();
final CommandNode<Object> node = getCommandNode();
node.addChild(literal("child").build());
node.addChild(literal("child").executes(command).build());

View file

@ -1,16 +1,13 @@
package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@ -22,14 +19,12 @@ import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
private ArgumentCommandNode<Object, Integer> node;
private CommandContextBuilder<Object> contextBuilder;
private Object source = new Object();
@Override
protected CommandNode<Object> getCommandNode() {
@ -44,7 +39,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testParse() throws Exception {
StringReader reader = new StringReader("123 456");
final StringReader reader = new StringReader("123 456");
node.parse(reader, contextBuilder);
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
@ -64,15 +59,15 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testUsage_empty() throws Exception {
@SuppressWarnings("unchecked") ArgumentType<String> type = mock(ArgumentType.class);
@SuppressWarnings("unchecked") final ArgumentType<String> type = mock(ArgumentType.class);
when(type.getUsageText()).thenReturn(null);
ArgumentCommandNode<Object, String> node = argument("foo", type).build();
final ArgumentCommandNode<Object, String> node = argument("foo", type).build();
assertThat(node.getUsageText(), is("<foo>"));
}
@Test
public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet();
final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context);
assertThat(set, is(empty()));
@ -80,7 +75,7 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testEquals() throws Exception {
@SuppressWarnings("unchecked") Command<Object> command = (Command<Object>) mock(Command.class);
@SuppressWarnings("unchecked") final Command<Object> command = (Command<Object>) mock(Command.class);
new EqualsTester()
.addEqualityGroup(

View file

@ -40,21 +40,21 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testParse() throws Exception {
StringReader reader = new StringReader("foo bar");
final StringReader reader = new StringReader("foo bar");
node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo(" bar"));
}
@Test
public void testParseExact() throws Exception {
StringReader reader = new StringReader("foo");
final StringReader reader = new StringReader("foo");
node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo(""));
}
@Test
public void testParseSimilar() throws Exception {
StringReader reader = new StringReader("foobar");
final StringReader reader = new StringReader("foobar");
node.parse(reader, contextBuilder);
assertThat(reader.getRemaining(), equalTo("bar"));
// This should succeed, because it's the responsibility of the dispatcher to realize there's trailing text
@ -62,11 +62,11 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testParseInvalid() throws Exception {
StringReader reader = new StringReader("bar");
final StringReader reader = new StringReader("bar");
try {
node.parse(reader, contextBuilder);
fail();
} catch (CommandException ex) {
} catch (final CommandException ex) {
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
}
@ -79,7 +79,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet();
final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context);
@ -100,7 +100,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testEquals() throws Exception {
@SuppressWarnings("unchecked") Command<Object> command = mock(Command.class);
@SuppressWarnings("unchecked") final Command<Object> command = mock(Command.class);
new EqualsTester()
.addEqualityGroup(

View file

@ -31,7 +31,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testParse() throws Exception {
StringReader reader = new StringReader("hello world");
final StringReader reader = new StringReader("hello world");
node.parse(reader, new CommandContextBuilder<>(new CommandDispatcher<>(), new Object()));
assertThat(reader.getCursor(), is(0));
}
@ -48,7 +48,7 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
@Test
public void testSuggestions() throws Exception {
Set<String> set = Sets.newHashSet();
final Set<String> set = Sets.newHashSet();
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
node.listSuggestions("", set, context);
assertThat(set, is(empty()));