Skip to content

Commit

Permalink
switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyuioplkjhgfd committed Jan 3, 2022
1 parent f7dd128 commit e337e21
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ public enum SummaryOrderType {

@ParametersAreNonnullByDefault
List<Map.Entry<String, Long>> sort(SlimefunProfiler profiler, Set<Map.Entry<String, Long>> 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<String, Long> map = new HashMap<>();
for (Map.Entry<String, Long> 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<String, Long> map = new HashMap<>();
for (Map.Entry<String, Long> 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());
}
}
}

0 comments on commit e337e21

Please sign in to comment.