Remove unnecessary Guava usages (#13)
This commit is contained in:
parent
bcbd596c24
commit
7ee589b29b
7 changed files with 43 additions and 38 deletions
|
@ -33,7 +33,7 @@ repositories {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api 'com.google.guava:guava:21.0'
|
testCompile 'com.google.guava:guava:21.0'
|
||||||
testCompile 'junit:junit-dep:4.10'
|
testCompile 'junit:junit-dep:4.10'
|
||||||
testCompile 'org.hamcrest:hamcrest-library:1.2.1'
|
testCompile 'org.hamcrest:hamcrest-library:1.2.1'
|
||||||
testCompile 'org.mockito:mockito-core:1.8.5'
|
testCompile 'org.mockito:mockito-core:1.8.5'
|
||||||
|
|
|
@ -3,9 +3,6 @@
|
||||||
|
|
||||||
package com.mojang.brigadier;
|
package com.mojang.brigadier;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
|
@ -21,6 +18,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -450,7 +448,7 @@ public class CommandDispatcher<S> {
|
||||||
* @return array of full usage strings under the target node
|
* @return array of full usage strings under the target node
|
||||||
*/
|
*/
|
||||||
public String[] getAllUsage(final CommandNode<S> node, final S source, final boolean restricted) {
|
public String[] getAllUsage(final CommandNode<S> node, final S source, final boolean restricted) {
|
||||||
final ArrayList<String> result = Lists.newArrayList();
|
final ArrayList<String> result = new ArrayList<>();
|
||||||
getAllUsage(node, source, result, "", restricted);
|
getAllUsage(node, source, result, "", restricted);
|
||||||
return result.toArray(new String[result.size()]);
|
return result.toArray(new String[result.size()]);
|
||||||
}
|
}
|
||||||
|
@ -496,7 +494,7 @@ public class CommandDispatcher<S> {
|
||||||
* @return array of full usage strings under the target node
|
* @return array of full usage strings under the target node
|
||||||
*/
|
*/
|
||||||
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
|
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
|
||||||
final Map<CommandNode<S>, String> result = Maps.newLinkedHashMap();
|
final Map<CommandNode<S>, String> result = new LinkedHashMap<>();
|
||||||
|
|
||||||
final boolean optional = node.getCommand() != null;
|
final boolean optional = node.getCommand() != null;
|
||||||
for (final CommandNode<S> child : node.getChildren()) {
|
for (final CommandNode<S> child : node.getChildren()) {
|
||||||
|
@ -530,7 +528,7 @@ public class CommandDispatcher<S> {
|
||||||
return self + ARGUMENT_SEPARATOR + usage;
|
return self + ARGUMENT_SEPARATOR + usage;
|
||||||
}
|
}
|
||||||
} else if (children.size() > 1) {
|
} else if (children.size() > 1) {
|
||||||
final Set<String> childUsage = Sets.newLinkedHashSet();
|
final Set<String> childUsage = new LinkedHashSet<>();
|
||||||
for (final CommandNode<S> child : children) {
|
for (final CommandNode<S> child : children) {
|
||||||
final String usage = getSmartUsage(child, source, childOptional, true);
|
final String usage = getSmartUsage(child, source, childOptional, true);
|
||||||
if (usage != null) {
|
if (usage != null) {
|
||||||
|
@ -603,7 +601,7 @@ public class CommandDispatcher<S> {
|
||||||
|
|
||||||
final CompletableFuture<Suggestions> result = new CompletableFuture<>();
|
final CompletableFuture<Suggestions> result = new CompletableFuture<>();
|
||||||
CompletableFuture.allOf(futures).thenRun(() -> {
|
CompletableFuture.allOf(futures).thenRun(() -> {
|
||||||
final List<Suggestions> suggestions = Lists.newArrayList();
|
final List<Suggestions> suggestions = new ArrayList<>();
|
||||||
for (final CompletableFuture<Suggestions> future : futures) {
|
for (final CompletableFuture<Suggestions> future : futures) {
|
||||||
suggestions.add(future.join());
|
suggestions.add(future.join());
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@ package com.mojang.brigadier;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface SingleRedirectModifier<S> {
|
public interface SingleRedirectModifier<S> {
|
||||||
S apply(CommandContext<S> context) throws CommandSyntaxException;
|
S apply(CommandContext<S> context) throws CommandSyntaxException;
|
||||||
|
|
|
@ -3,16 +3,29 @@
|
||||||
|
|
||||||
package com.mojang.brigadier.context;
|
package com.mojang.brigadier.context;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import com.google.common.primitives.Primitives;
|
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.RedirectModifier;
|
import com.mojang.brigadier.RedirectModifier;
|
||||||
import com.mojang.brigadier.tree.CommandNode;
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandContext<S> {
|
public class CommandContext<S> {
|
||||||
|
|
||||||
|
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = new HashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(boolean.class, Boolean.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(byte.class, Byte.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(short.class, Short.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(char.class, Character.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(int.class, Integer.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(long.class, Long.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(float.class, Float.class);
|
||||||
|
PRIMITIVE_TO_WRAPPER.put(double.class, Double.class);
|
||||||
|
}
|
||||||
|
|
||||||
private final S source;
|
private final S source;
|
||||||
private final String input;
|
private final String input;
|
||||||
private final Command<S> command;
|
private final Command<S> command;
|
||||||
|
@ -73,7 +86,7 @@ public class CommandContext<S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
final Object result = argument.getResult();
|
final Object result = argument.getResult();
|
||||||
if (Primitives.wrap(clazz).isAssignableFrom(result.getClass())) {
|
if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) {
|
||||||
return (V) result;
|
return (V) result;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
|
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
|
||||||
|
@ -89,7 +102,7 @@ public class CommandContext<S> {
|
||||||
|
|
||||||
if (!arguments.equals(that.arguments)) return false;
|
if (!arguments.equals(that.arguments)) return false;
|
||||||
if (!rootNode.equals(that.rootNode)) return false;
|
if (!rootNode.equals(that.rootNode)) return false;
|
||||||
if (!Iterables.elementsEqual(nodes, that.nodes)) return false;
|
if (nodes.size() != that.nodes.size() || !nodes.equals(that.nodes)) return false;
|
||||||
if (command != null ? !command.equals(that.command) : that.command != null) return false;
|
if (command != null ? !command.equals(that.command) : that.command != null) return false;
|
||||||
if (!source.equals(that.source)) return false;
|
if (!source.equals(that.source)) return false;
|
||||||
if (child != null ? !child.equals(that.child) : that.child != null) return false;
|
if (child != null ? !child.equals(that.child) : that.child != null) return false;
|
||||||
|
|
|
@ -3,21 +3,20 @@
|
||||||
|
|
||||||
package com.mojang.brigadier.context;
|
package com.mojang.brigadier.context;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
import com.mojang.brigadier.RedirectModifier;
|
import com.mojang.brigadier.RedirectModifier;
|
||||||
import com.mojang.brigadier.tree.CommandNode;
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandContextBuilder<S> {
|
public class CommandContextBuilder<S> {
|
||||||
private final Map<String, ParsedArgument<S, ?>> arguments = Maps.newLinkedHashMap();
|
private final Map<String, ParsedArgument<S, ?>> arguments = new LinkedHashMap<>();
|
||||||
private final CommandNode<S> rootNode;
|
private final CommandNode<S> rootNode;
|
||||||
private final List<ParsedCommandNode<S>> nodes = Lists.newArrayList();
|
private final List<ParsedCommandNode<S>> nodes = new ArrayList<>();
|
||||||
private final CommandDispatcher<S> dispatcher;
|
private final CommandDispatcher<S> dispatcher;
|
||||||
private S source;
|
private S source;
|
||||||
private Command<S> command;
|
private Command<S> command;
|
||||||
|
@ -122,7 +121,7 @@ public class CommandContextBuilder<S> {
|
||||||
if (child != null) {
|
if (child != null) {
|
||||||
return child.findSuggestionContext(cursor);
|
return child.findSuggestionContext(cursor);
|
||||||
} else if (!nodes.isEmpty()) {
|
} else if (!nodes.isEmpty()) {
|
||||||
final ParsedCommandNode<S> last = Iterables.getLast(nodes);
|
final ParsedCommandNode<S> last = nodes.get(nodes.size() - 1);
|
||||||
return new SuggestionContext<>(last.getNode(), last.getRange().getEnd() + 1);
|
return new SuggestionContext<>(last.getNode(), last.getRange().getEnd() + 1);
|
||||||
} else {
|
} else {
|
||||||
return new SuggestionContext<>(rootNode, range.getStart());
|
return new SuggestionContext<>(rootNode, range.getStart());
|
||||||
|
|
|
@ -3,10 +3,9 @@
|
||||||
|
|
||||||
package com.mojang.brigadier.suggestion;
|
package com.mojang.brigadier.suggestion;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.mojang.brigadier.context.StringRange;
|
import com.mojang.brigadier.context.StringRange;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -15,7 +14,7 @@ import java.util.Set;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class Suggestions {
|
public class Suggestions {
|
||||||
private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), Lists.newArrayList());
|
private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), new ArrayList<>());
|
||||||
|
|
||||||
private final StringRange range;
|
private final StringRange range;
|
||||||
private final List<Suggestion> suggestions;
|
private final List<Suggestion> suggestions;
|
||||||
|
@ -92,11 +91,11 @@ public class Suggestions {
|
||||||
end = Math.max(suggestion.getRange().getEnd(), end);
|
end = Math.max(suggestion.getRange().getEnd(), end);
|
||||||
}
|
}
|
||||||
final StringRange range = new StringRange(start, end);
|
final StringRange range = new StringRange(start, end);
|
||||||
final Set<Suggestion> texts = Sets.newHashSet();
|
final Set<Suggestion> texts = new HashSet<>();
|
||||||
for (final Suggestion suggestion : suggestions) {
|
for (final Suggestion suggestion : suggestions) {
|
||||||
texts.add(suggestion.expand(command, range));
|
texts.add(suggestion.expand(command, range));
|
||||||
}
|
}
|
||||||
final List<Suggestion> sorted = Lists.newArrayList(texts);
|
final List<Suggestion> sorted = new ArrayList<>(texts);
|
||||||
sorted.sort((a, b) -> a.compareToIgnoreCase(b));
|
sorted.sort((a, b) -> a.compareToIgnoreCase(b));
|
||||||
return new Suggestions(range, sorted);
|
return new Suggestions(range, sorted);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,6 @@
|
||||||
|
|
||||||
package com.mojang.brigadier.tree;
|
package com.mojang.brigadier.tree;
|
||||||
|
|
||||||
import com.google.common.collect.ComparisonChain;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.mojang.brigadier.AmbiguityConsumer;
|
import com.mojang.brigadier.AmbiguityConsumer;
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.RedirectModifier;
|
import com.mojang.brigadier.RedirectModifier;
|
||||||
|
@ -19,6 +16,7 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -27,9 +25,9 @@ import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||||
private Map<String, CommandNode<S>> children = Maps.newLinkedHashMap();
|
private Map<String, CommandNode<S>> children = new LinkedHashMap<>();
|
||||||
private Map<String, LiteralCommandNode<S>> literals = Maps.newLinkedHashMap();
|
private Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
|
||||||
private Map<String, ArgumentCommandNode<S, ?>> arguments = Maps.newLinkedHashMap();
|
private Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
|
||||||
private final Predicate<S> requirement;
|
private final Predicate<S> requirement;
|
||||||
private final CommandNode<S> redirect;
|
private final CommandNode<S> redirect;
|
||||||
private final RedirectModifier<S> modifier;
|
private final RedirectModifier<S> modifier;
|
||||||
|
@ -95,7 +93,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findAmbiguities(final AmbiguityConsumer<S> consumer) {
|
public void findAmbiguities(final AmbiguityConsumer<S> consumer) {
|
||||||
Set<String> matches = Sets.newHashSet();
|
Set<String> matches = new HashSet<>();
|
||||||
|
|
||||||
for (final CommandNode<S> child : children.values()) {
|
for (final CommandNode<S> child : children.values()) {
|
||||||
for (final CommandNode<S> sibling : children.values()) {
|
for (final CommandNode<S> sibling : children.values()) {
|
||||||
|
@ -111,7 +109,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||||
|
|
||||||
if (matches.size() > 0) {
|
if (matches.size() > 0) {
|
||||||
consumer.ambiguous(this, child, sibling, matches);
|
consumer.ambiguous(this, child, sibling, matches);
|
||||||
matches = Sets.newHashSet();
|
matches = new HashSet<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,11 +174,11 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(final CommandNode<S> o) {
|
public int compareTo(final CommandNode<S> o) {
|
||||||
return ComparisonChain
|
if (this instanceof LiteralCommandNode == o instanceof LiteralCommandNode) {
|
||||||
.start()
|
return getSortedKey().compareTo(o.getSortedKey());
|
||||||
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)
|
}
|
||||||
.compare(getSortedKey(), o.getSortedKey())
|
|
||||||
.result();
|
return (o instanceof LiteralCommandNode) ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFork() {
|
public boolean isFork() {
|
||||||
|
|
Loading…
Reference in a new issue