Skip to content

Commit

Permalink
Remove unnecessary Guava usages (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR authored and Dinnerbone committed Oct 4, 2018
1 parent bcbd596 commit 7ee589b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repositories {
}

dependencies {
api 'com.google.guava:guava:21.0'
testCompile 'com.google.guava:guava:21.0'
testCompile 'junit:junit-dep:4.10'
testCompile 'org.hamcrest:hamcrest-library:1.2.1'
testCompile 'org.mockito:mockito-core:1.8.5'
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/mojang/brigadier/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

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.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
Expand All @@ -21,6 +18,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -450,7 +448,7 @@ private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader
* @return array of full usage strings under the target node
*/
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);
return result.toArray(new String[result.size()]);
}
Expand Down Expand Up @@ -496,7 +494,7 @@ private void getAllUsage(final CommandNode<S> node, final S source, final ArrayL
* @return array of full usage strings under the target node
*/
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;
for (final CommandNode<S> child : node.getChildren()) {
Expand Down Expand Up @@ -530,7 +528,7 @@ private String getSmartUsage(final CommandNode<S> node, final S source, final bo
return self + ARGUMENT_SEPARATOR + usage;
}
} else if (children.size() > 1) {
final Set<String> childUsage = Sets.newLinkedHashSet();
final Set<String> childUsage = new LinkedHashSet<>();
for (final CommandNode<S> child : children) {
final String usage = getSmartUsage(child, source, childOptional, true);
if (usage != null) {
Expand Down Expand Up @@ -603,7 +601,7 @@ public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResult

final CompletableFuture<Suggestions> result = new CompletableFuture<>();
CompletableFuture.allOf(futures).thenRun(() -> {
final List<Suggestions> suggestions = Lists.newArrayList();
final List<Suggestions> suggestions = new ArrayList<>();
for (final CompletableFuture<Suggestions> future : futures) {
suggestions.add(future.join());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import java.util.Collection;

@FunctionalInterface
public interface SingleRedirectModifier<S> {
S apply(CommandContext<S> context) throws CommandSyntaxException;
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/mojang/brigadier/context/CommandContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@

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.RedirectModifier;
import com.mojang.brigadier.tree.CommandNode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 String input;
private final Command<S> command;
Expand Down Expand Up @@ -73,7 +86,7 @@ public <V> V getArgument(final String name, final Class<V> clazz) {
}

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;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
Expand All @@ -89,7 +102,7 @@ public boolean equals(final Object o) {

if (!arguments.equals(that.arguments)) 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 (!source.equals(that.source)) return false;
if (child != null ? !child.equals(that.child) : that.child != null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

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.CommandDispatcher;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.tree.CommandNode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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 List<ParsedCommandNode<S>> nodes = Lists.newArrayList();
private final List<ParsedCommandNode<S>> nodes = new ArrayList<>();
private final CommandDispatcher<S> dispatcher;
private S source;
private Command<S> command;
Expand Down Expand Up @@ -122,7 +121,7 @@ public SuggestionContext<S> findSuggestionContext(final int cursor) {
if (child != null) {
return child.findSuggestionContext(cursor);
} 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);
} else {
return new SuggestionContext<>(rootNode, range.getStart());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

package com.mojang.brigadier.suggestion;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mojang.brigadier.context.StringRange;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -15,7 +14,7 @@
import java.util.concurrent.CompletableFuture;

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 List<Suggestion> suggestions;
Expand Down Expand Up @@ -92,11 +91,11 @@ public static Suggestions create(final String command, final Collection<Suggesti
end = Math.max(suggestion.getRange().getEnd(), 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) {
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));
return new Suggestions(range, sorted);
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

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.Command;
import com.mojang.brigadier.RedirectModifier;
Expand All @@ -19,6 +16,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -27,9 +25,9 @@
import java.util.stream.Collectors;

public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private Map<String, CommandNode<S>> children = Maps.newLinkedHashMap();
private Map<String, LiteralCommandNode<S>> literals = Maps.newLinkedHashMap();
private Map<String, ArgumentCommandNode<S, ?>> arguments = Maps.newLinkedHashMap();
private Map<String, CommandNode<S>> children = new LinkedHashMap<>();
private Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>();
private Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>();
private final Predicate<S> requirement;
private final CommandNode<S> redirect;
private final RedirectModifier<S> modifier;
Expand Down Expand Up @@ -95,7 +93,7 @@ public void addChild(final CommandNode<S> node) {
}

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> sibling : children.values()) {
Expand All @@ -111,7 +109,7 @@ public void findAmbiguities(final AmbiguityConsumer<S> consumer) {

if (matches.size() > 0) {
consumer.ambiguous(this, child, sibling, matches);
matches = Sets.newHashSet();
matches = new HashSet<>();
}
}

Expand Down Expand Up @@ -176,11 +174,11 @@ public Collection<? extends CommandNode<S>> getRelevantNodes(final StringReader

@Override
public int compareTo(final CommandNode<S> o) {
return ComparisonChain
.start()
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)
.compare(getSortedKey(), o.getSortedKey())
.result();
if (this instanceof LiteralCommandNode == o instanceof LiteralCommandNode) {
return getSortedKey().compareTo(o.getSortedKey());
}

return (o instanceof LiteralCommandNode) ? 1 : -1;
}

public boolean isFork() {
Expand Down

0 comments on commit 7ee589b

Please sign in to comment.