From 35a4b39758ea9bab40942f1d61755fac8e4cfcd4 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 30 Dec 2020 13:53:47 +0100 Subject: [PATCH] Optimize ArgumentUtils Optimize SWCommand Optimize SWCommandBundle --- .../de/steamwar/command/ArgumentUtils.java | 63 +++---------------- .../src/de/steamwar/command/SWCommand.java | 11 +--- .../de/steamwar/command/SWCommandBundle.java | 4 +- 3 files changed, 13 insertions(+), 65 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java index 676482f..65b57d8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java @@ -35,8 +35,9 @@ public class ArgumentUtils { return false; } - public static Argument of(String argument) { - return new Argument<>(s -> s, string -> string.equals(argument), argument); + private static String[] supply(String s, String[] empty, String[] notEmpty) { + if (s.isEmpty()) return empty; + return notEmpty; } public static Argument of(String... arguments) { @@ -44,14 +45,7 @@ public class ArgumentUtils { } public static Argument of(String[] commands, String[] tabCompletes) { - return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> { - if (s.isEmpty()) return tabCompletes; - return commands; - }); - } - - public static Argument ofIgnoreCase(String argument) { - return new Argument<>(s -> s, string -> string.equalsIgnoreCase(argument), argument); + return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands)); } public static Argument ofIgnoreCase(String... arguments) { @@ -59,70 +53,31 @@ public class ArgumentUtils { } public static Argument ofIgnoreCase(String[] commands, String[] tabCompletes) { - return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> { - if (s.isEmpty()) return tabCompletes; - return commands; - }); - } - - public static Argument above(int minValue, int... tabValues) { - return between(minValue, Integer.MAX_VALUE, tabValues); - } - - public static Argument below(int maxValue, int... tabValues) { - return between(Integer.MIN_VALUE, maxValue, tabValues); + return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands)); } public static Argument between(int minValue, int maxValue, int... tabValues) { Predicate predicate = i -> i >= minValue && i <= maxValue; - for (int tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException(); - String[] array = Arrays.stream(tabValues).mapToObj(i -> i + "").toArray(String[]::new); + String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(i -> i + "").toArray(String[]::new); return new Argument<>(Integer::parseInt, predicate, array); } - public static Argument above(long minValue, long... tabValues) { - return between(minValue, Long.MAX_VALUE, tabValues); - } - - public static Argument below(long maxValue, long... tabValues) { - return between(Long.MIN_VALUE, maxValue, tabValues); - } - public static Argument between(long minValue, long maxValue, long... tabValues) { Predicate predicate = l -> l >= minValue && l <= maxValue; - for (long tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException(); - String[] array = Arrays.stream(tabValues).mapToObj(l -> l + "").toArray(String[]::new); + String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(l -> l + "").toArray(String[]::new); return new Argument<>(Long::parseLong, predicate, array); } - public static Argument above(float minValue, float... tabValues) { - return between(minValue, Float.MAX_VALUE, tabValues); - } - - public static Argument below(float maxValue, float... tabValues) { - return between(Float.MIN_VALUE, maxValue, tabValues); - } - public static Argument between(float minValue, float maxValue, float... tabValues) { Predicate predicate = f -> f >= minValue && f <= maxValue; - for (float tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException(); String[] array = new String[tabValues.length]; - for (int i = 0; i < tabValues.length; i++) array[i] = tabValues[i] + ""; + for (int i = 0; i < tabValues.length; i++) if (predicate.test(tabValues[i])) array[i] = tabValues[i] + ""; return new Argument<>(Float::parseFloat, predicate, array); } - public static Argument above(double minValue, double... tabValues) { - return between(minValue, Double.MAX_VALUE, tabValues); - } - - public static Argument below(double maxValue, double... tabValues) { - return between(Double.MIN_VALUE, maxValue, tabValues); - } - public static Argument between(double minValue, double maxValue, double... tabValues) { Predicate predicate = d -> d >= minValue && d <= maxValue; - for (double tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException(); - String[] array = Arrays.stream(tabValues).mapToObj(d -> d + "").toArray(String[]::new); + String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(d -> d + "").toArray(String[]::new); return new Argument<>(Double::parseDouble, predicate, array); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 34159e9..948c8de 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,9 +36,7 @@ public class SWCommand { } public boolean execute(Player player, String[] args) { - if (args.length != arguments.length) { - return false; - } + if (args.length != arguments.length) return false; Object[] objects = new Object[args.length]; for (int i = 0; i < args.length; i++) { Optional optional = arguments[i].valueSupplier(args[i]); @@ -50,12 +48,9 @@ public class SWCommand { } public Optional> tabComplete(String[] args) { - if (args.length > arguments.length) { - return Optional.empty(); - } + if (args.length > arguments.length) return Optional.empty(); for (int i = 0; i < args.length - 1; i++) { - Optional optional = arguments[i].valueSupplier(args[i]); - if (!optional.isPresent()) return Optional.empty(); + if (!arguments[i].valueSupplier(args[i]).isPresent()) return Optional.empty(); } int index = args.length - 1; return arguments[index].tabCompleteSupplier(args[index]); diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java index 52ce3fd..1f798ca 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java @@ -55,9 +55,7 @@ public class SWCommandBundle { public List tabComplete(String[] args) { List strings = new ArrayList<>(); - for (SWCommand swCommand : swCommandList) { - swCommand.tabComplete(args).ifPresent(strings::addAll); - } + swCommandList.forEach(swCommand -> swCommand.tabComplete(args).ifPresent(strings::addAll)); return strings; }