From 7ee589b29b7c72c423c1549f1edcb5c89981291a Mon Sep 17 00:00:00 2001 From: MrMicky Date: Thu, 4 Oct 2018 13:29:08 +0200 Subject: [PATCH] Remove unnecessary Guava usages (#13) --- build.gradle | 2 +- .../mojang/brigadier/CommandDispatcher.java | 12 ++++------ .../brigadier/SingleRedirectModifier.java | 2 -- .../brigadier/context/CommandContext.java | 21 ++++++++++++---- .../context/CommandContextBuilder.java | 11 ++++----- .../brigadier/suggestion/Suggestions.java | 9 ++++--- .../mojang/brigadier/tree/CommandNode.java | 24 +++++++++---------- 7 files changed, 43 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 89a4768..02181cb 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 4883db9..1035767 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -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; @@ -21,6 +18,7 @@ import java.util.ArrayList; 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; @@ -450,7 +448,7 @@ public class CommandDispatcher { * @return array of full usage strings under the target node */ public String[] getAllUsage(final CommandNode node, final S source, final boolean restricted) { - final ArrayList result = Lists.newArrayList(); + final ArrayList result = new ArrayList<>(); getAllUsage(node, source, result, "", restricted); return result.toArray(new String[result.size()]); } @@ -496,7 +494,7 @@ public class CommandDispatcher { * @return array of full usage strings under the target node */ public Map, String> getSmartUsage(final CommandNode node, final S source) { - final Map, String> result = Maps.newLinkedHashMap(); + final Map, String> result = new LinkedHashMap<>(); final boolean optional = node.getCommand() != null; for (final CommandNode child : node.getChildren()) { @@ -530,7 +528,7 @@ public class CommandDispatcher { return self + ARGUMENT_SEPARATOR + usage; } } else if (children.size() > 1) { - final Set childUsage = Sets.newLinkedHashSet(); + final Set childUsage = new LinkedHashSet<>(); for (final CommandNode child : children) { final String usage = getSmartUsage(child, source, childOptional, true); if (usage != null) { @@ -603,7 +601,7 @@ public class CommandDispatcher { final CompletableFuture result = new CompletableFuture<>(); CompletableFuture.allOf(futures).thenRun(() -> { - final List suggestions = Lists.newArrayList(); + final List suggestions = new ArrayList<>(); for (final CompletableFuture future : futures) { suggestions.add(future.join()); } diff --git a/src/main/java/com/mojang/brigadier/SingleRedirectModifier.java b/src/main/java/com/mojang/brigadier/SingleRedirectModifier.java index ce91c15..4601181 100644 --- a/src/main/java/com/mojang/brigadier/SingleRedirectModifier.java +++ b/src/main/java/com/mojang/brigadier/SingleRedirectModifier.java @@ -6,8 +6,6 @@ package com.mojang.brigadier; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import java.util.Collection; - @FunctionalInterface public interface SingleRedirectModifier { S apply(CommandContext context) throws CommandSyntaxException; diff --git a/src/main/java/com/mojang/brigadier/context/CommandContext.java b/src/main/java/com/mojang/brigadier/context/CommandContext.java index 73924db..bc5bb4a 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContext.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContext.java @@ -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 { + + private static final Map, 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 command; @@ -73,7 +86,7 @@ public class CommandContext { } 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); @@ -89,7 +102,7 @@ public class CommandContext { 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; diff --git a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java index e9ef918..1af2cb8 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java @@ -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 { - private final Map> arguments = Maps.newLinkedHashMap(); + private final Map> arguments = new LinkedHashMap<>(); private final CommandNode rootNode; - private final List> nodes = Lists.newArrayList(); + private final List> nodes = new ArrayList<>(); private final CommandDispatcher dispatcher; private S source; private Command command; @@ -122,7 +121,7 @@ public class CommandContextBuilder { if (child != null) { return child.findSuggestionContext(cursor); } else if (!nodes.isEmpty()) { - final ParsedCommandNode last = Iterables.getLast(nodes); + final ParsedCommandNode last = nodes.get(nodes.size() - 1); return new SuggestionContext<>(last.getNode(), last.getRange().getEnd() + 1); } else { return new SuggestionContext<>(rootNode, range.getStart()); diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java index c76532e..bab793f 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java @@ -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; @@ -15,7 +14,7 @@ import java.util.Set; 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 suggestions; @@ -92,11 +91,11 @@ public class Suggestions { end = Math.max(suggestion.getRange().getEnd(), end); } final StringRange range = new StringRange(start, end); - final Set texts = Sets.newHashSet(); + final Set texts = new HashSet<>(); for (final Suggestion suggestion : suggestions) { texts.add(suggestion.expand(command, range)); } - final List sorted = Lists.newArrayList(texts); + final List sorted = new ArrayList<>(texts); sorted.sort((a, b) -> a.compareToIgnoreCase(b)); return new Suggestions(range, sorted); } diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java index 30df856..15b584a 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -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; @@ -19,6 +16,7 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; @@ -27,9 +25,9 @@ import java.util.function.Predicate; import java.util.stream.Collectors; public abstract class CommandNode implements Comparable> { - private Map> children = Maps.newLinkedHashMap(); - private Map> literals = Maps.newLinkedHashMap(); - private Map> arguments = Maps.newLinkedHashMap(); + private Map> children = new LinkedHashMap<>(); + private Map> literals = new LinkedHashMap<>(); + private Map> arguments = new LinkedHashMap<>(); private final Predicate requirement; private final CommandNode redirect; private final RedirectModifier modifier; @@ -95,7 +93,7 @@ public abstract class CommandNode implements Comparable> { } public void findAmbiguities(final AmbiguityConsumer consumer) { - Set matches = Sets.newHashSet(); + Set matches = new HashSet<>(); for (final CommandNode child : children.values()) { for (final CommandNode sibling : children.values()) { @@ -111,7 +109,7 @@ public abstract class CommandNode implements Comparable> { if (matches.size() > 0) { consumer.ambiguous(this, child, sibling, matches); - matches = Sets.newHashSet(); + matches = new HashSet<>(); } } @@ -176,11 +174,11 @@ public abstract class CommandNode implements Comparable> { @Override public int compareTo(final CommandNode 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() {