From af916ef57739b71aa29754249f538f8e7361aae5 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 13 Jun 2021 04:34:12 -0400 Subject: [PATCH] Cache several common argument types --- .../mojang/brigadier/arguments/BoolArgumentType.java | 3 ++- .../mojang/brigadier/arguments/DoubleArgumentType.java | 3 ++- .../mojang/brigadier/arguments/FloatArgumentType.java | 3 ++- .../brigadier/arguments/IntegerArgumentType.java | 3 ++- .../mojang/brigadier/arguments/LongArgumentType.java | 3 ++- .../mojang/brigadier/arguments/StringArgumentType.java | 10 +++++++--- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java index 935bb845..d3ee17b2 100644 --- a/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/BoolArgumentType.java @@ -14,13 +14,14 @@ import java.util.concurrent.CompletableFuture; public class BoolArgumentType implements ArgumentType { + private static final BoolArgumentType INSTANCE = new BoolArgumentType(); private static final Collection EXAMPLES = Arrays.asList("true", "false"); private BoolArgumentType() { } public static BoolArgumentType bool() { - return new BoolArgumentType(); + return INSTANCE; } public static boolean getBool(final CommandContext context, final String name) { diff --git a/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java index 8da87d0d..a62af432 100644 --- a/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/DoubleArgumentType.java @@ -11,6 +11,7 @@ import java.util.Collection; public class DoubleArgumentType implements ArgumentType { + private static final DoubleArgumentType ALL = new DoubleArgumentType(-Double.MAX_VALUE, Double.MAX_VALUE); private static final Collection EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private final double minimum; @@ -22,7 +23,7 @@ private DoubleArgumentType(final double minimum, final double maximum) { } public static DoubleArgumentType doubleArg() { - return doubleArg(-Double.MAX_VALUE); + return ALL; } public static DoubleArgumentType doubleArg(final double min) { diff --git a/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java index e6811ac2..1c07843d 100644 --- a/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/FloatArgumentType.java @@ -11,6 +11,7 @@ import java.util.Collection; public class FloatArgumentType implements ArgumentType { + private static final FloatArgumentType ALL = new FloatArgumentType(-Float.MAX_VALUE, Float.MAX_VALUE); private static final Collection EXAMPLES = Arrays.asList("0", "1.2", ".5", "-1", "-.5", "-1234.56"); private final float minimum; @@ -22,7 +23,7 @@ private FloatArgumentType(final float minimum, final float maximum) { } public static FloatArgumentType floatArg() { - return floatArg(-Float.MAX_VALUE); + return ALL; } public static FloatArgumentType floatArg(final float min) { diff --git a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java index d4e23655..bcac9c9d 100644 --- a/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/IntegerArgumentType.java @@ -11,6 +11,7 @@ import java.util.Collection; public class IntegerArgumentType implements ArgumentType { + private static final IntegerArgumentType ALL = new IntegerArgumentType(Integer.MIN_VALUE, Integer.MAX_VALUE); private static final Collection EXAMPLES = Arrays.asList("0", "123", "-123"); private final int minimum; @@ -22,7 +23,7 @@ private IntegerArgumentType(final int minimum, final int maximum) { } public static IntegerArgumentType integer() { - return integer(Integer.MIN_VALUE); + return ALL; } public static IntegerArgumentType integer(final int min) { diff --git a/src/main/java/com/mojang/brigadier/arguments/LongArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/LongArgumentType.java index b322bec0..8b35947a 100644 --- a/src/main/java/com/mojang/brigadier/arguments/LongArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/LongArgumentType.java @@ -11,6 +11,7 @@ import java.util.Collection; public class LongArgumentType implements ArgumentType { + private static final LongArgumentType ALL = new LongArgumentType(Long.MIN_VALUE, Long.MAX_VALUE); private static final Collection EXAMPLES = Arrays.asList("0", "123", "-123"); private final long minimum; @@ -22,7 +23,7 @@ private LongArgumentType(final long minimum, final long maximum) { } public static LongArgumentType longArg() { - return longArg(Long.MIN_VALUE); + return ALL; } public static LongArgumentType longArg(final long min) { diff --git a/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java index 0e36f340..8981e91f 100644 --- a/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java @@ -11,6 +11,10 @@ import java.util.Collection; public class StringArgumentType implements ArgumentType { + private static final StringArgumentType WORD_ARGUMENT = new StringArgumentType(StringType.SINGLE_WORD); + private static final StringArgumentType QUOTED_ARGUMENT = new StringArgumentType(StringType.QUOTABLE_PHRASE); + private static final StringArgumentType GREEDY_ARGUMENT = new StringArgumentType(StringType.GREEDY_PHRASE); + private final StringType type; private StringArgumentType(final StringType type) { @@ -18,15 +22,15 @@ private StringArgumentType(final StringType type) { } public static StringArgumentType word() { - return new StringArgumentType(StringType.SINGLE_WORD); + return WORD_ARGUMENT; } public static StringArgumentType string() { - return new StringArgumentType(StringType.QUOTABLE_PHRASE); + return QUOTED_ARGUMENT; } public static StringArgumentType greedyString() { - return new StringArgumentType(StringType.GREEDY_PHRASE); + return GREEDY_ARGUMENT; } public static String getString(final CommandContext context, final String name) {