Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch statement for SummaryOrderType #3405

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}