Minor: Rename StringUtils#isEmpty to #isBlank

- Better fits the naming used by other tools and also the JDK (String#isBlank as of JDK 11)
This commit is contained in:
ljacqu 2022-12-30 08:44:29 +01:00
parent 779e62674e
commit 9fd532d798
20 changed files with 42 additions and 46 deletions

View File

@ -226,8 +226,8 @@ public class CommandDescription {
*/ */
public CommandDescription build() { public CommandDescription build() {
checkArgument(!Utils.isCollectionEmpty(labels), "Labels may not be empty"); checkArgument(!Utils.isCollectionEmpty(labels), "Labels may not be empty");
checkArgument(!StringUtils.isEmpty(description), "Description may not be empty"); checkArgument(!StringUtils.isBlank(description), "Description may not be empty");
checkArgument(!StringUtils.isEmpty(detailedDescription), "Detailed description may not be empty"); checkArgument(!StringUtils.isBlank(detailedDescription), "Detailed description may not be empty");
checkArgument(executableCommand != null, "Executable command must be set"); checkArgument(executableCommand != null, "Executable command must be set");
// parents and permissions may be null; arguments may be empty // parents and permissions may be null; arguments may be empty

View File

@ -131,7 +131,7 @@ public class CommandHandler {
private static List<String> skipEmptyArguments(String[] args) { private static List<String> skipEmptyArguments(String[] args) {
List<String> cleanArguments = new ArrayList<>(); List<String> cleanArguments = new ArrayList<>();
for (String argument : args) { for (String argument : args) {
if (!StringUtils.isEmpty(argument)) { if (!StringUtils.isBlank(argument)) {
cleanArguments.add(argument); cleanArguments.add(argument);
} }
} }

View File

@ -89,7 +89,7 @@ class PlayerAuthViewer implements DebugSection {
* or empty string if the string is null or empty * or empty string if the string is null or empty
*/ */
private static String safeSubstring(String str, int length) { private static String safeSubstring(String str, int length) {
if (StringUtils.isEmpty(str)) { if (StringUtils.isBlank(str)) {
return ""; return "";
} else if (str.length() < length) { } else if (str.length() < length) {
return str.substring(0, str.length() / 2) + "..."; return str.substring(0, str.length() / 2) + "...";

View File

@ -160,7 +160,7 @@ public class OnJoinVerifier implements Reloadable {
if (auth != null && settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)) { if (auth != null && settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE)) {
String realName = auth.getRealName(); // might be null or "Player" String realName = auth.getRealName(); // might be null or "Player"
if (StringUtils.isEmpty(realName) || "Player".equals(realName)) { if (StringUtils.isBlank(realName) || "Player".equals(realName)) {
dataSource.updateRealName(connectingName.toLowerCase(Locale.ROOT), connectingName); dataSource.updateRealName(connectingName.toLowerCase(Locale.ROOT), connectingName);
} else if (!realName.equals(connectingName)) { } else if (!realName.equals(connectingName)) {
throw new FailedVerificationException(MessageKey.INVALID_NAME_CASE, realName, connectingName); throw new FailedVerificationException(MessageKey.INVALID_NAME_CASE, realName, connectingName);

View File

@ -51,11 +51,11 @@ public class SendMailSsl {
* @throws EmailException if the mail is invalid * @throws EmailException if the mail is invalid
*/ */
public HtmlEmail initializeMail(String emailAddress) throws EmailException { public HtmlEmail initializeMail(String emailAddress) throws EmailException {
String senderMail = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_ADDRESS)) String senderMail = StringUtils.isBlank(settings.getProperty(EmailSettings.MAIL_ADDRESS))
? settings.getProperty(EmailSettings.MAIL_ACCOUNT) ? settings.getProperty(EmailSettings.MAIL_ACCOUNT)
: settings.getProperty(EmailSettings.MAIL_ADDRESS); : settings.getProperty(EmailSettings.MAIL_ADDRESS);
String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)) String senderName = StringUtils.isBlank(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
? senderMail ? senderMail
: settings.getProperty(EmailSettings.MAIL_SENDER_NAME); : settings.getProperty(EmailSettings.MAIL_SENDER_NAME);
String mailPassword = settings.getProperty(EmailSettings.MAIL_PASSWORD); String mailPassword = settings.getProperty(EmailSettings.MAIL_PASSWORD);

View File

@ -324,7 +324,7 @@ public class PermissionsManager implements Reloadable {
* False is also returned if this feature isn't supported for the current permissions system. * False is also returned if this feature isn't supported for the current permissions system.
*/ */
public boolean addGroup(OfflinePlayer player, UserGroup groupName) { public boolean addGroup(OfflinePlayer player, UserGroup groupName) {
if (!isEnabled() || StringUtils.isEmpty(groupName.getGroupName())) { if (!isEnabled() || StringUtils.isBlank(groupName.getGroupName())) {
return false; return false;
} }
return handler.addToGroup(player, groupName); return handler.addToGroup(player, groupName);

View File

@ -38,7 +38,7 @@ public class JoinMessageService {
*/ */
public void sendMessage(String playerName) { public void sendMessage(String playerName) {
String joinMessage = joinMessages.remove(playerName); String joinMessage = joinMessages.remove(playerName);
if (!StringUtils.isEmpty(joinMessage)) { if (!StringUtils.isBlank(joinMessage)) {
bukkitService.broadcastMessage(joinMessage); bukkitService.broadcastMessage(joinMessage);
} }
} }

View File

@ -114,7 +114,7 @@ public class SettingsMigrationService extends PlainMigrationService {
// Old other accounts // Old other accounts
// -------- // --------
public boolean hasOldOtherAccountsCommand() { public boolean hasOldOtherAccountsCommand() {
return !StringUtils.isEmpty(oldOtherAccountsCommand); return !StringUtils.isBlank(oldOtherAccountsCommand);
} }
public String getOldOtherAccountsCommand() { public String getOldOtherAccountsCommand() {

View File

@ -293,7 +293,7 @@ public class SpawnLoader implements Reloadable {
String prefix = pathPrefix + "."; String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "world"); String worldName = configuration.getString(prefix + "world");
World world = Bukkit.getWorld(worldName); World world = Bukkit.getWorld(worldName);
if (!StringUtils.isEmpty(worldName) && world != null) { if (!StringUtils.isBlank(worldName) && world != null) {
return new Location(world, configuration.getDouble(prefix + "x"), return new Location(world, configuration.getDouble(prefix + "x"),
configuration.getDouble(prefix + "y"), configuration.getDouble(prefix + "z"), configuration.getDouble(prefix + "y"), configuration.getDouble(prefix + "z"),
getFloat(configuration, prefix + "yaw"), getFloat(configuration, prefix + "pitch")); getFloat(configuration, prefix + "yaw"), getFloat(configuration, prefix + "pitch"));
@ -315,7 +315,7 @@ public class SpawnLoader implements Reloadable {
String prefix = pathPrefix + "."; String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "World"); String worldName = configuration.getString(prefix + "World");
World world = Bukkit.getWorld(worldName); World world = Bukkit.getWorld(worldName);
if (!StringUtils.isEmpty(worldName) && world != null) { if (!StringUtils.isBlank(worldName) && world != null) {
return new Location(world, configuration.getDouble(prefix + "X"), return new Location(world, configuration.getDouble(prefix + "X"),
configuration.getDouble(prefix + "Y"), configuration.getDouble(prefix + "Z"), configuration.getDouble(prefix + "Y"), configuration.getDouble(prefix + "Z"),
getFloat(configuration, prefix + "Yaw"), getFloat(configuration, prefix + "Pitch")); getFloat(configuration, prefix + "Yaw"), getFloat(configuration, prefix + "Pitch"));

View File

@ -14,12 +14,11 @@ public final class StringUtils {
} }
/** /**
* Get the difference of two strings. * Calculates the difference of two strings.
* *
* @param first First string * @param first first string
* @param second Second string * @param second second string
* * @return the difference value
* @return The difference value
*/ */
public static double getDifference(String first, String second) { public static double getDifference(String first, String second) {
// Make sure the strings are valid. // Make sure the strings are valid.
@ -35,12 +34,11 @@ public final class StringUtils {
} }
/** /**
* Return whether the given string contains any of the provided elements. * Returns whether the given string contains any of the provided elements.
* *
* @param str The string to analyze * @param str the string to analyze
* @param pieces The items to check the string for * @param pieces the items to check the string for
* * @return true if the string contains at least one of the items
* @return True if the string contains at least one of the items
*/ */
public static boolean containsAny(String str, Iterable<String> pieces) { public static boolean containsAny(String str, Iterable<String> pieces) {
if (str == null) { if (str == null) {
@ -58,21 +56,19 @@ public final class StringUtils {
* Null-safe method for checking whether a string is empty. Note that the string * Null-safe method for checking whether a string is empty. Note that the string
* is trimmed, so this method also considers a string with whitespace as empty. * is trimmed, so this method also considers a string with whitespace as empty.
* *
* @param str The string to verify * @param str the string to verify
* * @return true if the string is empty, false otherwise
* @return True if the string is empty, false otherwise
*/ */
public static boolean isEmpty(String str) { public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty(); return str == null || str.trim().isEmpty();
} }
/** /**
* Check that the given needle is in the middle of the haystack, i.e. that the haystack * Checks that the given needle is in the middle of the haystack, i.e. that the haystack
* contains the needle and that it is not at the very start or end. * contains the needle and that it is not at the very start or end.
* *
* @param needle the needle to search for * @param needle the needle to search for
* @param haystack the haystack to search in * @param haystack the haystack to search in
*
* @return true if the needle is in the middle of the word, false otherwise * @return true if the needle is in the middle of the word, false otherwise
*/ */
// Note ljacqu 20170314: `needle` is restricted to char type intentionally because something like // Note ljacqu 20170314: `needle` is restricted to char type intentionally because something like

View File

@ -105,6 +105,6 @@ public final class Utils {
* @return true if the email is empty * @return true if the email is empty
*/ */
public static boolean isEmailEmpty(String email) { public static boolean isEmailEmpty(String email) {
return StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email); return StringUtils.isBlank(email) || "your@email.com".equalsIgnoreCase(email);
} }
} }

View File

@ -138,11 +138,11 @@ public class CommandInitializerTest {
String forCommandText = " for command with labels '" + command.getLabels() + "'"; String forCommandText = " for command with labels '" + command.getLabels() + "'";
assertThat("has description" + forCommandText, assertThat("has description" + forCommandText,
StringUtils.isEmpty(command.getDescription()), equalTo(false)); StringUtils.isBlank(command.getDescription()), equalTo(false));
assertThat("short description doesn't end in '.'" + forCommandText, assertThat("short description doesn't end in '.'" + forCommandText,
command.getDescription().endsWith("."), equalTo(false)); command.getDescription().endsWith("."), equalTo(false));
assertThat("has detailed description" + forCommandText, assertThat("has detailed description" + forCommandText,
StringUtils.isEmpty(command.getDetailedDescription()), equalTo(false)); StringUtils.isBlank(command.getDetailedDescription()), equalTo(false));
assertThat("detailed description ends in '.'" + forCommandText, assertThat("detailed description ends in '.'" + forCommandText,
command.getDetailedDescription().endsWith("."), equalTo(true)); command.getDetailedDescription().endsWith("."), equalTo(true));
} }

View File

@ -93,7 +93,7 @@ public class ConverterCommandTest {
// when / then // when / then
for (Map.Entry<String, Class<? extends Converter>> entry : ConverterCommand.CONVERTERS.entrySet()) { for (Map.Entry<String, Class<? extends Converter>> entry : ConverterCommand.CONVERTERS.entrySet()) {
assertThat("Name is not null or empty", assertThat("Name is not null or empty",
StringUtils.isEmpty(entry.getKey()), equalTo(false)); StringUtils.isBlank(entry.getKey()), equalTo(false));
assertThat("Converter class is unique for each entry", assertThat("Converter class is unique for each entry",
classes.add(entry.getValue()), equalTo(true)); classes.add(entry.getValue()), equalTo(true));

View File

@ -23,14 +23,14 @@ public class HelpMessageAndHelpSectionConsistencyTest {
// when / then // when / then
for (HelpMessage message : HelpMessage.values()) { for (HelpMessage message : HelpMessage.values()) {
assertThat("Key for message '" + message + "' is empty", assertThat("Key for message '" + message + "' is empty",
StringUtils.isEmpty(message.getKey()), equalTo(false)); StringUtils.isBlank(message.getKey()), equalTo(false));
if (!keys.add(message.getKey())) { if (!keys.add(message.getKey())) {
fail("Key for message '" + message + "' is already used elsewhere"); fail("Key for message '" + message + "' is already used elsewhere");
} }
} }
for (HelpSection section : HelpSection.values()) { for (HelpSection section : HelpSection.values()) {
assertThat("Key for section '" + section + "' is empty", assertThat("Key for section '" + section + "' is empty",
StringUtils.isEmpty(section.getKey()), equalTo(false)); StringUtils.isBlank(section.getKey()), equalTo(false));
if (!keys.add(section.getKey())) { if (!keys.add(section.getKey())) {
fail("Key for section '" + section + "' is already used elsewhere"); fail("Key for section '" + section + "' is already used elsewhere");
} }

View File

@ -27,7 +27,7 @@ public class MessageKeyTest {
String key = messageKey.getKey(); String key = messageKey.getKey();
if (!keys.add(key)) { if (!keys.add(key)) {
fail("Found key '" + messageKey.getKey() + "' twice!"); fail("Found key '" + messageKey.getKey() + "' twice!");
} else if (StringUtils.isEmpty(key)) { } else if (StringUtils.isBlank(key)) {
fail("Key for message key '" + messageKey + "' is empty"); fail("Key for message key '" + messageKey + "' is empty");
} }
} }

View File

@ -40,7 +40,7 @@ public class MessagesFileConsistencyTest {
final String key = messageKey.getKey(); final String key = messageKey.getKey();
final String message = reader.getString(key); final String message = reader.getString(key);
if (StringUtils.isEmpty(message)) { if (StringUtils.isBlank(message)) {
errors.add("Messages file should have message for key '" + key + "'"); errors.add("Messages file should have message for key '" + key + "'");
return; return;
} }

View File

@ -81,7 +81,7 @@ public class YamlTextFileCheckerTest {
private void checkFile(File file, String mandatoryKey, List<String> errors) { private void checkFile(File file, String mandatoryKey, List<String> errors) {
try { try {
PropertyReader reader = new YamlFileReader(file); PropertyReader reader = new YamlFileReader(file);
if (StringUtils.isEmpty(reader.getString(mandatoryKey))) { if (StringUtils.isBlank(reader.getString(mandatoryKey))) {
errors.add("Message for '" + mandatoryKey + "' is empty"); errors.add("Message for '" + mandatoryKey + "' is empty");
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -74,9 +74,9 @@ public class HashAlgorithmIntegrationTest {
} }
HashedPassword hashedPassword = method.computeHash("pwd", "name"); HashedPassword hashedPassword = method.computeHash("pwd", "name");
assertThat("Salt should not be null if method.hasSeparateSalt(), and vice versa. Method: '" assertThat("Salt should not be null if method.hasSeparateSalt(), and vice versa. Method: '"
+ method + "'", StringUtils.isEmpty(hashedPassword.getSalt()), equalTo(!method.hasSeparateSalt())); + method + "'", StringUtils.isBlank(hashedPassword.getSalt()), equalTo(!method.hasSeparateSalt()));
assertThat("Hash should not be empty for method '" + method + "'", assertThat("Hash should not be empty for method '" + method + "'",
StringUtils.isEmpty(hashedPassword.getHash()), equalTo(false)); StringUtils.isBlank(hashedPassword.getHash()), equalTo(false));
} }
} }
} }

View File

@ -49,15 +49,15 @@ public class StringUtilsTest {
} }
@Test @Test
public void shouldCheckIsEmptyUtil() { public void shouldCheckIfIsBlankString() {
// Should be true for null/empty/whitespace // Should be true for null/empty/whitespace
assertTrue(StringUtils.isEmpty(null)); assertTrue(StringUtils.isBlank(null));
assertTrue(StringUtils.isEmpty("")); assertTrue(StringUtils.isBlank(""));
assertTrue(StringUtils.isEmpty(" \t")); assertTrue(StringUtils.isBlank(" \t"));
// Should be false if string has content // Should be false if string has content
assertFalse(StringUtils.isEmpty("P")); assertFalse(StringUtils.isBlank("P"));
assertFalse(StringUtils.isEmpty(" test")); assertFalse(StringUtils.isBlank(" test"));
} }
@Test @Test

View File

@ -44,7 +44,7 @@ public final class VerifyMessagesTask implements ToolTask {
boolean addMissingKeys = "y".equalsIgnoreCase(scanner.nextLine()); boolean addMissingKeys = "y".equalsIgnoreCase(scanner.nextLine());
List<File> messageFiles; List<File> messageFiles;
if (StringUtils.isEmpty(inputFile)) { if (StringUtils.isBlank(inputFile)) {
messageFiles = getMessagesFiles(); messageFiles = getMessagesFiles();
} else { } else {
File customFile = new File(MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile)); File customFile = new File(MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile));