From e337e214fc3007ebc30c14d611836f19bd4b2568 Mon Sep 17 00:00:00 2001 From: qwertyuioplkjhgfd Date: Sun, 2 Jan 2022 20:30:16 -0800 Subject: [PATCH] switch statement --- .../commands/subcommands/TimingsCommand.java | 43 ++++++++++--------- .../services/profiler/SummaryOrderType.java | 37 ++++++++-------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java index 401e62b2a6..a22d2683f9 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java @@ -37,33 +37,34 @@ protected String getDescription() { @Override public void onExecute(CommandSender sender, String[] args) { - if (sender.hasPermission("slimefun.command.timings") || sender instanceof ConsoleCommandSender) { - if (hasInvalidFlags(sender, args)) { - return; - } - - boolean verbose = hasFlag(args, "verbose"); + if (!sender.hasPermission("slimefun.command.timings") && !(sender instanceof ConsoleCommandSender)) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } - if (verbose && sender instanceof Player) { - Slimefun.getLocalization().sendMessage(sender, "commands.timings.verbose-player", true); - return; - } + if (hasInvalidFlags(sender, args)) { + return; + } - SummaryOrderType orderType = SummaryOrderType.HIGHEST; + boolean verbose = hasFlag(args, "verbose"); - if (hasFlag(args, "avg")) { - orderType = SummaryOrderType.AVERAGE; - } else if (hasFlag(args, "low")) { - orderType = SummaryOrderType.LOWEST; - } + if (verbose && sender instanceof Player) { + Slimefun.getLocalization().sendMessage(sender, "commands.timings.verbose-player", true); + return; + } - Slimefun.getLocalization().sendMessage(sender, "commands.timings.please-wait", true); + SummaryOrderType orderType = SummaryOrderType.HIGHEST; - PerformanceInspector inspector = inspectorOf(sender, verbose, orderType); - Slimefun.getProfiler().requestSummary(inspector); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + if (hasFlag(args, "avg")) { + orderType = SummaryOrderType.AVERAGE; + } else if (hasFlag(args, "low")) { + orderType = SummaryOrderType.LOWEST; } + + Slimefun.getLocalization().sendMessage(sender, "commands.timings.please-wait", true); + + PerformanceInspector inspector = inspectorOf(sender, verbose, orderType); + Slimefun.getProfiler().requestSummary(inspector); } @ParametersAreNonnullByDefault diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SummaryOrderType.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SummaryOrderType.java index 1255220739..2f1e2ef195 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SummaryOrderType.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SummaryOrderType.java @@ -30,25 +30,26 @@ public enum SummaryOrderType { @ParametersAreNonnullByDefault List> sort(SlimefunProfiler profiler, Set> entrySet) { - if (this == SummaryOrderType.HIGHEST) { - return entrySet.stream() - .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) - .collect(Collectors.toList()); - } else if (this == SummaryOrderType.LOWEST) { - return entrySet.stream() - .sorted(Comparator.comparingLong(Map.Entry::getValue)) - .collect(Collectors.toList()); - } else { - final Map map = new HashMap<>(); - for (Map.Entry entry : entrySet) { - int count = profiler.getBlocksOfId(entry.getKey()); - long avg = count > 0 ? entry.getValue() / count : entry.getValue(); + switch(this) { + case HIGHEST: + return entrySet.stream() + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) + .collect(Collectors.toList()); + case LOWEST: + return entrySet.stream() + .sorted(Comparator.comparingLong(Map.Entry::getValue)) + .collect(Collectors.toList()); + default: + final Map map = new HashMap<>(); + for (Map.Entry entry : entrySet) { + int count = profiler.getBlocksOfId(entry.getKey()); + long avg = count > 0 ? entry.getValue() / count : entry.getValue(); - map.put(entry.getKey(), avg); - } - return map.entrySet().stream() - .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) - .collect(Collectors.toList()); + map.put(entry.getKey(), avg); + } + return map.entrySet().stream() + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) + .collect(Collectors.toList()); } } }