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() {
checkArgument(!Utils.isCollectionEmpty(labels), "Labels may not be empty");
checkArgument(!StringUtils.isEmpty(description), "Description may not be empty");
checkArgument(!StringUtils.isEmpty(detailedDescription), "Detailed description may not be empty");
checkArgument(!StringUtils.isBlank(description), "Description may not be empty");
checkArgument(!StringUtils.isBlank(detailedDescription), "Detailed description may not be empty");
checkArgument(executableCommand != null, "Executable command must be set");
// 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) {
List<String> cleanArguments = new ArrayList<>();
for (String argument : args) {
if (!StringUtils.isEmpty(argument)) {
if (!StringUtils.isBlank(argument)) {
cleanArguments.add(argument);
}
}

View File

@ -89,7 +89,7 @@ class PlayerAuthViewer implements DebugSection {
* or empty string if the string is null or empty
*/
private static String safeSubstring(String str, int length) {
if (StringUtils.isEmpty(str)) {
if (StringUtils.isBlank(str)) {
return "";
} else if (str.length() < length) {
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)) {
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);
} else if (!realName.equals(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
*/
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_ADDRESS);
String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
String senderName = StringUtils.isBlank(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
? senderMail
: settings.getProperty(EmailSettings.MAIL_SENDER_NAME);
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.
*/
public boolean addGroup(OfflinePlayer player, UserGroup groupName) {
if (!isEnabled() || StringUtils.isEmpty(groupName.getGroupName())) {
if (!isEnabled() || StringUtils.isBlank(groupName.getGroupName())) {
return false;
}
return handler.addToGroup(player, groupName);

View File

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

View File

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

View File

@ -293,7 +293,7 @@ public class SpawnLoader implements Reloadable {
String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "world");
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"),
configuration.getDouble(prefix + "y"), configuration.getDouble(prefix + "z"),
getFloat(configuration, prefix + "yaw"), getFloat(configuration, prefix + "pitch"));
@ -315,7 +315,7 @@ public class SpawnLoader implements Reloadable {
String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "World");
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"),
configuration.getDouble(prefix + "Y"), configuration.getDouble(prefix + "Z"),
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 second Second string
*
* @return The difference value
* @param first first string
* @param second second string
* @return the difference value
*/
public static double getDifference(String first, String second) {
// 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 pieces The items to check the string for
*
* @return True if the string contains at least one of the items
* @param str the string to analyze
* @param pieces the items to check the string for
* @return true if the string contains at least one of the items
*/
public static boolean containsAny(String str, Iterable<String> pieces) {
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
* is trimmed, so this method also considers a string with whitespace as empty.
*
* @param str The string to verify
*
* @return True if the string is empty, false otherwise
* @param str the string to verify
* @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();
}
/**
* 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.
*
* @param needle the needle to search for
* @param haystack the haystack to search in
*
* @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

View File

@ -105,6 +105,6 @@ public final class Utils {
* @return true if the email is empty
*/
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() + "'";
assertThat("has description" + forCommandText,
StringUtils.isEmpty(command.getDescription()), equalTo(false));
StringUtils.isBlank(command.getDescription()), equalTo(false));
assertThat("short description doesn't end in '.'" + forCommandText,
command.getDescription().endsWith("."), equalTo(false));
assertThat("has detailed description" + forCommandText,
StringUtils.isEmpty(command.getDetailedDescription()), equalTo(false));
StringUtils.isBlank(command.getDetailedDescription()), equalTo(false));
assertThat("detailed description ends in '.'" + forCommandText,
command.getDetailedDescription().endsWith("."), equalTo(true));
}

View File

@ -93,7 +93,7 @@ public class ConverterCommandTest {
// when / then
for (Map.Entry<String, Class<? extends Converter>> entry : ConverterCommand.CONVERTERS.entrySet()) {
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",
classes.add(entry.getValue()), equalTo(true));

View File

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

View File

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

View File

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

View File

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

View File

@ -74,9 +74,9 @@ public class HashAlgorithmIntegrationTest {
}
HashedPassword hashedPassword = method.computeHash("pwd", "name");
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 + "'",
StringUtils.isEmpty(hashedPassword.getHash()), equalTo(false));
StringUtils.isBlank(hashedPassword.getHash()), equalTo(false));
}
}
}

View File

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

View File

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