Fix minor checkstyle issues

- Add JavaDoc where checkstyle expects it
- Fix line too long issues
- ...
This commit is contained in:
ljacqu
2017-05-07 11:59:01 +02:00
parent 1a48348824
commit 1f8307c8f6
43 changed files with 605 additions and 134 deletions
@@ -44,7 +44,7 @@ public class CommandMapper {
* @param parts The parts to map to commands and arguments
* @return The generated {@link FoundCommandResult}
*/
public FoundCommandResult mapPartsToCommand(CommandSender sender, final List<String> parts) {
public FoundCommandResult mapPartsToCommand(CommandSender sender, List<String> parts) {
if (Utils.isCollectionEmpty(parts)) {
return new FoundCommandResult(null, parts, null, 0.0, MISSING_BASE_COMMAND);
}
@@ -87,6 +87,14 @@ public class CommandMapper {
return classes;
}
/**
* Return the command whose label matches the given parts the best. This method is called when
* a successful mapping could not be performed.
*
* @param base the base command
* @param parts the command parts
* @return the closest result
*/
private static FoundCommandResult getCommandWithSmallestDifference(CommandDescription base, List<String> parts) {
// Return the base command with incorrect arg count error if we only have one part
if (parts.size() <= 1) {
@@ -189,14 +197,10 @@ public class CommandMapper {
}
private static double getLabelDifference(CommandDescription command, String givenLabel) {
double minDifference = Double.POSITIVE_INFINITY;
for (String commandLabel : command.getLabels()) {
double difference = StringUtils.getDifference(commandLabel, givenLabel);
if (difference < minDifference) {
minDifference = difference;
}
}
return minDifference;
return command.getLabels().stream()
.map(label -> StringUtils.getDifference(label, givenLabel))
.min(Double::compareTo)
.orElseThrow(() -> new IllegalStateException("Command does not have any labels set"));
}
}
@@ -7,11 +7,20 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Utility functions for {@link CommandDescription} objects.
*/
public final class CommandUtils {
private CommandUtils() {
}
/**
* Returns the minimum number of arguments required for running the command (= number of mandatory arguments).
*
* @param command the command to process
* @return min number of arguments required by the command
*/
public static int getMinNumberOfArguments(CommandDescription command) {
int mandatoryArguments = 0;
for (CommandArgumentDescription argument : command.getArguments()) {
@@ -22,20 +31,16 @@ public final class CommandUtils {
return mandatoryArguments;
}
/**
* Returns the maximum number of arguments the command accepts.
*
* @param command the command to process
* @return max number of arguments that may be passed to the command
*/
public static int getMaxNumberOfArguments(CommandDescription command) {
return command.getArguments().size();
}
public static String constructCommandPath(CommandDescription command) {
StringBuilder sb = new StringBuilder();
String prefix = "/";
for (CommandDescription ancestor : constructParentList(command)) {
sb.append(prefix).append(ancestor.getLabels().get(0));
prefix = " ";
}
return sb.toString();
}
/**
* Constructs a hierarchical list of commands for the given command. The commands are in order:
* the parents of the given command precede the provided command. For example, given the command
@@ -54,6 +59,29 @@ public final class CommandUtils {
return Lists.reverse(commands);
}
/**
* Returns a textual representation of the command, e.g. {@code /authme register}.
*
* @param command the command to create the path for
* @return the command string
*/
public static String constructCommandPath(CommandDescription command) {
StringBuilder sb = new StringBuilder();
String prefix = "/";
for (CommandDescription ancestor : constructParentList(command)) {
sb.append(prefix).append(ancestor.getLabels().get(0));
prefix = " ";
}
return sb.toString();
}
/**
* Returns a textual representation of the command, including its arguments.
* For example: {@code /authme purge <days> [includeZero]}.
*
* @param command the command to create a usage string for
* @return the command's path and arguments
*/
public static String buildSyntax(CommandDescription command) {
String arguments = command.getArguments().stream()
.map(arg -> formatArgument(arg))
@@ -73,12 +101,12 @@ public final class CommandUtils {
}
/**
* Format a command argument with the proper type of brackets.
* Formats a command argument with the proper type of brackets.
*
* @param argument the argument to format
* @return the formatted argument
*/
public static String formatArgument(CommandArgumentDescription argument) {
private static String formatArgument(CommandArgumentDescription argument) {
if (argument.isOptional()) {
return "[" + argument.getName() + "]";
}
@@ -20,6 +20,9 @@ import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
/**
* Displays help information to a user.
*/
public class HelpCommand implements ExecutableCommand {
@Inject
@@ -51,7 +54,8 @@ public class HelpCommand implements ExecutableCommand {
int mappedCommandLevel = result.getCommandDescription().getLabelCount();
if (mappedCommandLevel == 1) {
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES);
helpProvider.outputHelp(sender, result,
SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES);
} else {
helpProvider.outputHelp(sender, result, ALL_OPTIONS);
}
@@ -68,6 +68,17 @@ class TestEmailSender implements DebugSection {
return DebugSectionPermissions.TEST_EMAIL;
}
/**
* Gets the email address to use based on the sender and the arguments. If the arguments are empty,
* we attempt to retrieve the email from the sender. If there is an argument, we verify that it is
* an email address.
* {@code null} is returned if no email address could be found. This method informs the sender of
* the specific error in such cases.
*
* @param sender the command sender
* @param arguments the provided arguments
* @return the email to use, or null if none found
*/
private String getEmail(CommandSender sender, List<String> arguments) {
if (arguments.isEmpty()) {
DataSourceResult<String> emailResult = dataSource.getEmail(sender.getName());
@@ -97,6 +97,15 @@ public class RegisterCommand extends PlayerCommand {
return null;
}
/**
* Verifies that the second argument is valid (based on the configuration)
* to perform a password registration. The player is informed if the check
* is unsuccessful.
*
* @param player the player to register
* @param arguments the provided arguments
* @return true if valid, false otherwise
*/
private boolean isSecondArgValidForPasswordRegistration(Player player, List<String> arguments) {
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
// cases where args.size < 2
@@ -143,6 +152,15 @@ public class RegisterCommand extends PlayerCommand {
}
}
/**
* Verifies that the second argument is valid (based on the configuration)
* to perform an email registration. The player is informed if the check
* is unsuccessful.
*
* @param player the player to register
* @param arguments the provided arguments
* @return true if valid, false otherwise
*/
private boolean isSecondArgValidForEmailRegistration(Player player, List<String> arguments) {
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
// cases where args.size < 2
@@ -58,7 +58,15 @@ public class HelpProvider implements Reloadable {
this.helpMessagesService = helpMessagesService;
}
private List<String> printHelp(CommandSender sender, FoundCommandResult result, int options) {
/**
* Builds the help messages based on the provided arguments.
*
* @param sender the sender to evaluate permissions with
* @param result the command result to create help for
* @param options output options
* @return the generated help messages
*/
private List<String> buildHelpOutput(CommandSender sender, FoundCommandResult result, int options) {
if (result.getCommandDescription() == null) {
return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
}
@@ -75,8 +83,7 @@ public class HelpProvider implements Reloadable {
}
CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription());
List<String> labels = ImmutableList.copyOf(result.getLabels());
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, result.getLabels()));
if (hasFlag(SHOW_COMMAND, options)) {
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMAND) + ": "
@@ -91,30 +98,30 @@ public class HelpProvider implements Reloadable {
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
}
if (hasFlag(SHOW_ARGUMENTS, options)) {
printArguments(command, lines);
addArgumentsInfo(command, lines);
}
if (hasFlag(SHOW_PERMISSIONS, options) && sender != null) {
printPermissions(command, sender, lines);
addPermissionsInfo(command, sender, lines);
}
if (hasFlag(SHOW_ALTERNATIVES, options)) {
printAlternatives(command, correctLabels, lines);
addAlternativesInfo(command, correctLabels, lines);
}
if (hasFlag(SHOW_CHILDREN, options)) {
printChildren(command, labels, lines);
addChildrenInfo(command, correctLabels, lines);
}
return lines;
}
/**
* Output the help for a given command.
* Outputs the help for a given command.
*
* @param sender The sender to output the help to
* @param result The result to output information about
* @param options Output options, see {@link HelpProvider}
* @param sender the sender to output the help to
* @param result the result to output information about
* @param options output options
*/
public void outputHelp(CommandSender sender, FoundCommandResult result, int options) {
List<String> lines = printHelp(sender, result, options);
List<String> lines = buildHelpOutput(sender, result, options);
for (String line : lines) {
sender.sendMessage(line);
}
@@ -134,6 +141,7 @@ public class HelpProvider implements Reloadable {
* @param options the options to process
* @return the options without any disabled sections
*/
@SuppressWarnings("checkstyle:BooleanExpressionComplexity")
private int filterDisabledSections(int options) {
if (enabledSections == null) {
enabledSections = flagFor(HelpSection.COMMAND, SHOW_COMMAND)
@@ -151,7 +159,13 @@ public class HelpProvider implements Reloadable {
return helpMessagesService.getMessage(section).isEmpty() ? 0 : flag;
}
private void printArguments(CommandDescription command, List<String> lines) {
/**
* Adds help info about the given command's arguments into the provided list.
*
* @param command the command to generate arguments info for
* @param lines the output collection to add the info to
*/
private void addArgumentsInfo(CommandDescription command, List<String> lines) {
if (command.getArguments().isEmpty()) {
return;
}
@@ -171,7 +185,14 @@ public class HelpProvider implements Reloadable {
}
}
private void printAlternatives(CommandDescription command, List<String> correctLabels, List<String> lines) {
/**
* Adds help info about the given command's alternative labels into the provided list.
*
* @param command the command for which to generate info about its labels
* @param correctLabels labels used to access the command (sanitized)
* @param lines the output collection to add the info to
*/
private void addAlternativesInfo(CommandDescription command, List<String> correctLabels, List<String> lines) {
if (command.getLabels().size() <= 1) {
return;
}
@@ -199,7 +220,14 @@ public class HelpProvider implements Reloadable {
}
}
private void printPermissions(CommandDescription command, CommandSender sender, List<String> lines) {
/**
* Adds help info about the given command's permissions into the provided list.
*
* @param command the command to generate permissions info for
* @param sender the command sender, used to evaluate permissions
* @param lines the output collection to add the info to
*/
private void addPermissionsInfo(CommandDescription command, CommandSender sender, List<String> lines) {
PermissionNode permission = command.getPermission();
if (permission == null) {
return;
@@ -240,13 +268,20 @@ public class HelpProvider implements Reloadable {
return helpMessagesService.getMessage(HelpMessage.NO_PERMISSION);
}
private void printChildren(CommandDescription command, List<String> parentLabels, List<String> lines) {
/**
* Adds help info about the given command's child command into the provided list.
*
* @param command the command for which to generate info about its child commands
* @param correctLabels the labels used to access the given command (sanitized)
* @param lines the output collection to add the info to
*/
private void addChildrenInfo(CommandDescription command, List<String> correctLabels, List<String> lines) {
if (command.getChildren().isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.CHILDREN) + ":");
String parentCommandPath = String.join(" ", parentLabels);
String parentCommandPath = String.join(" ", correctLabels);
for (CommandDescription child : command.getChildren()) {
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)
+ ChatColor.GRAY + ChatColor.ITALIC + ": " + helpMessagesService.getDescription(child));
@@ -257,6 +292,23 @@ public class HelpProvider implements Reloadable {
return (flag & options) != 0;
}
/**
* Returns a list of labels for the given command, using the labels from the provided labels list
* as long as they are correct.
* <p>
* Background: commands may have multiple labels (e.g. /authme register vs. /authme reg). It is interesting
* for us to keep with which label the user requested the command. At the same time, when a user inputs a
* non-existent label, we try to find the most similar one. This method keeps all labels that exists and will
* default to the command's first label when an invalid label is encountered.
* <p>
* Examples:
* command = "authme register", labels = {authme, egister}. Output: {authme, register}
* command = "authme register", labels = {authme, reg}. Output: {authme, reg}
*
* @param command the command to compare the labels against
* @param labels the labels as input by the user
* @return list of correct labels, keeping the user's input where possible
*/
@VisibleForTesting
static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) {
List<CommandDescription> commands = CommandUtils.constructParentList(command);