From dddd8c4e26f1d254c55749e4b44ae7c6cce5001d Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 16 Jan 2021 22:30:34 -0500 Subject: [PATCH] Use isEmpty(). --- .../org/apache/commons/lang3/StringUtils.java | 2 +- .../apache/commons/lang3/text/StrBuilder.java | 17 +++++++++++++++-- .../apache/commons/lang3/ThreadUtilsTest.java | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index f6f7a16a0..478139e37 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -6707,7 +6707,7 @@ public class StringUtils { final Set searchSet = new HashSet<>(Arrays.asList(searchList)); final Set replacementSet = new HashSet<>(Arrays.asList(replacementList)); searchSet.retainAll(replacementSet); - if (searchSet.size() > 0) { + if (!searchSet.isEmpty()) { throw new IllegalStateException("Aborting to protect against StackOverflowError - " + "output of one loop is the input of another"); } diff --git a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java index ad8ef61d9..9bf72ed77 100644 --- a/src/main/java/org/apache/commons/lang3/text/StrBuilder.java +++ b/src/main/java/org/apache/commons/lang3/text/StrBuilder.java @@ -282,6 +282,19 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build return size == 0; } + /** + * Checks is the string builder is not empty (convenience Collections API style method). + *

+ * This method is the same as checking {@link #length()} and is provided to match the + * API of Collections. + * + * @return {@code true} if the size is greater than {@code 0}. + * @since 3.12 + */ + public boolean isNotEmpty() { + return size > 0; + } + /** * Clears the string builder (convenience Collections API style method). *

@@ -1390,7 +1403,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build * @since 2.3 */ public StrBuilder appendSeparator(final char separator) { - if (size() > 0) { + if (isNotEmpty()) { append(separator); } return this; @@ -1408,7 +1421,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build * @since 2.5 */ public StrBuilder appendSeparator(final char standard, final char defaultIfEmpty) { - if (size() > 0) { + if (isNotEmpty()) { append(standard); } else { append(defaultIfEmpty); diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java index a0539b145..436a7b8b7 100644 --- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java @@ -127,12 +127,12 @@ public class ThreadUtilsTest { @Test public void testAtLeastOneThreadExists() { - assertTrue(ThreadUtils.getAllThreads().size() > 0); + assertFalse(ThreadUtils.getAllThreads().isEmpty()); } @Test public void testAtLeastOneThreadGroupsExists() { - assertTrue(ThreadUtils.getAllThreadGroups().size() > 0); + assertFalse(ThreadUtils.getAllThreadGroups().isEmpty()); } @Test