From 895cedb4049f691516460a6f39eca37411fb5dcf Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 19 Jul 2003 21:55:05 +0000 Subject: [PATCH] Rework isEmpty and isNotEmpty following user feedback git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137459 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.txt | 7 +- .../org/apache/commons/lang/ClassUtils.java | 6 +- .../org/apache/commons/lang/StringUtils.java | 172 +++++------------- .../org/apache/commons/lang/enum/Enum.java | 4 +- .../commons/lang/reflect/ReflectionUtils.java | 4 +- .../lang/StringUtilsTrimEmptyTest.java | 70 ++----- 6 files changed, 76 insertions(+), 187 deletions(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 917fd7242..a04850337 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,4 +1,4 @@ -$Id: RELEASE-NOTES.txt,v 1.13 2003/07/16 21:23:50 scolebourne Exp $ +$Id: RELEASE-NOTES.txt,v 1.14 2003/07/19 21:55:05 scolebourne Exp $ Commons Lang Package Version 2.0 @@ -15,9 +15,9 @@ components that should be a help in any Java environment. INCOMPATIBLE CHANGES: Some StringUtils methods have changed functionality from 1.0. isEmpty() - isNotEmpty() chomp(String) chomp(String,String) + plus others have changed null handling Check your code before implementing this release. @@ -73,8 +73,7 @@ lang: SerializationUtils: added empty constructor StringUtils: - isEmpty() changed to not trim and return false for null. - isNotEmpty() changed return true for null. + isEmpty() changed to not trim chomp() changed to be more like Perl. Various methods changed in the handling of null (less exceptions). Thirty new methods. diff --git a/src/java/org/apache/commons/lang/ClassUtils.java b/src/java/org/apache/commons/lang/ClassUtils.java index 891090b36..602c3cb57 100644 --- a/src/java/org/apache/commons/lang/ClassUtils.java +++ b/src/java/org/apache/commons/lang/ClassUtils.java @@ -65,7 +65,7 @@ import java.util.List; * @author Stephen Colebourne * @author Gary Gregory * @since 2.0 - * @version $Id: ClassUtils.java,v 1.16 2003/07/19 20:17:12 scolebourne Exp $ + * @version $Id: ClassUtils.java,v 1.17 2003/07/19 21:55:05 scolebourne Exp $ */ public class ClassUtils { @@ -142,7 +142,7 @@ public class ClassUtils { * @throws IllegalArgumentException if the className is empty */ public static String getShortClassName(String className) { - if (StringUtils.isEmptyOrNull(className)) { + if (StringUtils.isEmpty(className)) { throw new IllegalArgumentException("The class name must not be empty"); } char[] chars = className.toCharArray(); @@ -199,7 +199,7 @@ public class ClassUtils { * @throws IllegalArgumentException if the className is empty */ public static String getPackageName(String className) { - if (StringUtils.isEmptyOrNull(className)) { + if (StringUtils.isEmpty(className)) { throw new IllegalArgumentException("The class name must not be empty"); } int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index bf2881d59..a879acb58 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -98,7 +98,7 @@ import org.apache.commons.lang.math.NumberUtils; * @author Arun Mammen Thomas * @author Gary Gregory * @since 1.0 - * @version $Id: StringUtils.java,v 1.65 2003/07/19 18:09:33 scolebourne Exp $ + * @version $Id: StringUtils.java,v 1.66 2003/07/19 21:55:05 scolebourne Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -314,11 +314,11 @@ public class StringUtils { //----------------------------------------------------------------------- /** - *

Checks if a String is empty (""). + *

Checks if a String is empty ("") or null. * null returns false

* *
-     * StringUtils.isEmpty(null)      = false
+     * StringUtils.isEmpty(null)      = true
      * StringUtils.isEmpty("")        = true
      * StringUtils.isEmpty(" ")       = false
      * StringUtils.isEmpty("bob")     = false
@@ -326,165 +326,87 @@ public class StringUtils {
      * 
* *

NOTE: This method changed in version 2.0. - * It no longer trims the String, and null is no longer true. - * That functionality is available in isEmptyTrimmedOrNull().

+ * It no longer trims the String. + * That functionality is available in isBlank().

* * @param str the String to check, may be null - * @return true if the String is empty + * @return true if the String is empty or null */ public static boolean isEmpty(String str) { - return (str != null && str.length() == 0); + return (str == null || str.length() == 0); } /** - *

Checks if a String is not empty (""). - * null returns true

+ *

Checks if a String is not empty ("") and not null.

* *
-     * StringUtils.isNotEmpty(null)      = true
+     * StringUtils.isNotEmpty(null)      = false
      * StringUtils.isNotEmpty("")        = false
      * StringUtils.isNotEmpty(" ")       = true
      * StringUtils.isNotEmpty("bob")     = true
      * StringUtils.isNotEmpty("  bob  ") = true
      * 
* - *

NOTE: This method changed in version 2.0. - * It no longer returns false for null. - * That functionality is available in isNotEmptyOrNull().

- * * @param str the String to check, may be null - * @return true if the String is not empty + * @return true if the String is not empty and not null */ public static boolean isNotEmpty(String str) { - return (str == null || str.length() > 0); - } - - /** - *

Checks if a String is empty ("") or null.

- * - *
-     * StringUtils.isEmptyOrNull(null)      = true
-     * StringUtils.isEmptyOrNull("")        = true
-     * StringUtils.isEmptyOrNull(" ")       = false
-     * StringUtils.isEmptyOrNull("bob")     = false
-     * StringUtils.isEmptyOrNull("  bob  ") = false
-     * 
- * - * @param str the String to check, may be null - * @return true if the String is empty or null - */ - public static boolean isEmptyOrNull(String str) { - return (str == null || str.length() == 0); - } - - /** - *

Checks if a String is not not empty ("") and not null.

- * - *
-     * StringUtils.isNotEmptyOrNull(null)      = false
-     * StringUtils.isNotEmptyOrNull("")        = false
-     * StringUtils.isNotEmptyOrNull(" ")       = true
-     * StringUtils.isNotEmptyOrNull("bob")     = true
-     * StringUtils.isNotEmptyOrNull("  bob  ") = true
-     * 
- * - * @param str the String to check, may be null - * @return true if the String is neither empty nor null - */ - public static boolean isNotEmptyOrNull(String str) { return (str != null && str.length() > 0); } /** - *

Checks if a trimmed String is empty (""). - * null returns false

+ *

Checks if a String is whitespace, empty ("") or null.

* - *

The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.

- * *
-     * StringUtils.isEmptyTrimmed(null)      = false
-     * StringUtils.isEmptyTrimmed("")        = true
-     * StringUtils.isEmptyTrimmed(" ")       = true
-     * StringUtils.isEmptyTrimmed("bob")     = false
-     * StringUtils.isEmptyTrimmed("  bob  ") = false
+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
      * 
* - * @see java.lang.String#trim() * @param str the String to check, may be null - * @return true if the String is empty after trim() + * @return true if the String is null, empty or whitespace */ - public static boolean isEmptyTrimmed(String str) { - return (str != null && str.trim().length() == 0); + public static boolean isBlank(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if ((Character.isWhitespace(str.charAt(i)) == false) ) { + return false; + } + } + return true; } /** - *

Checks if a trimmed String is not empty ("").

- * null returns true

+ *

Checks if a String is not empty ("") and not null.

* - *

The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.

- * *
-     * StringUtils.isNotEmptyTrimmed(null)      = true
-     * StringUtils.isNotEmptyTrimmed("")        = false
-     * StringUtils.isNotEmptyTrimmed(" ")       = false
-     * StringUtils.isNotEmptyTrimmed("bob")     = true
-     * StringUtils.isNotEmptyTrimmed("  bob  ") = true
+     * StringUtils.isNotBlank(null)      = false
+     * StringUtils.isNotBlank("")        = false
+     * StringUtils.isNotBlank(" ")       = false
+     * StringUtils.isNotBlank("bob")     = true
+     * StringUtils.isNotBlank("  bob  ") = true
      * 
* - * @see java.lang.String#trim() * @param str the String to check, may be null - * @return true if the String is not empty after trim() + * @return true if the String is + * not empty and not null and not whitespace */ - public static boolean isNotEmptyTrimmed(String str) { - return (str == null || str.trim().length() > 0); - } - - /** - *

Checks if a trimmed String is empty ("") or null.

- * - *

The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.

- * - *
-     * StringUtils.isEmptyTrimmedOrNull(null)      = true
-     * StringUtils.isEmptyTrimmedOrNull("")        = true
-     * StringUtils.isEmptyTrimmedOrNull(" ")       = true
-     * StringUtils.isEmptyTrimmedOrNull("bob")     = false
-     * StringUtils.isEmptyTrimmedOrNull("  bob  ") = false
-     * 
- * - * @see java.lang.String#trim() - * @param str the String to check, may be null - * @return true if the String is null - * or empty after trim() - */ - public static boolean isEmptyTrimmedOrNull(String str) { - return (str == null || str.trim().length() == 0); - } - - /** - *

Checks if a trimmed String is not empty ("") and not null.

- * - *

The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.

- * - *
-     * StringUtils.isNotEmptyTrimmedOrNull(null)      = false
-     * StringUtils.isNotEmptyTrimmedOrNull("")        = false
-     * StringUtils.isNotEmptyTrimmedOrNull(" ")       = false
-     * StringUtils.isNotEmptyTrimmedOrNull("bob")     = true
-     * StringUtils.isNotEmptyTrimmedOrNull("  bob  ") = true
-     * 
- * - * @see java.lang.String#trim() - * @param str the String to check, may be null - * @return true if the String is not null - * and not empty after trim() - */ - public static boolean isNotEmptyTrimmedOrNull(String str) { - return (str != null && str.trim().length() > 0); + public static boolean isNotBlank(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return false; + } + for (int i = 0; i < strLen; i++) { + if ((Character.isWhitespace(str.charAt(i)) == false) ) { + return true; + } + } + return false; } // Equals diff --git a/src/java/org/apache/commons/lang/enum/Enum.java b/src/java/org/apache/commons/lang/enum/Enum.java index cae3cc106..533101c2d 100644 --- a/src/java/org/apache/commons/lang/enum/Enum.java +++ b/src/java/org/apache/commons/lang/enum/Enum.java @@ -201,7 +201,7 @@ import org.apache.commons.lang.StringUtils; * @author Chris Webb * @author Mike Bowler * @since 1.0 - * @version $Id: Enum.java,v 1.13 2003/07/16 21:19:21 scolebourne Exp $ + * @version $Id: Enum.java,v 1.14 2003/07/19 21:55:04 scolebourne Exp $ */ public abstract class Enum implements Comparable, Serializable { // After discussion, the default size for HashMaps is used, as the @@ -251,7 +251,7 @@ public abstract class Enum implements Comparable, Serializable { protected Enum(String name) { super(); - if (StringUtils.isEmptyOrNull(name)) { + if (StringUtils.isEmpty(name)) { throw new IllegalArgumentException("The Enum name must not be empty or null"); } iName = name; diff --git a/src/java/org/apache/commons/lang/reflect/ReflectionUtils.java b/src/java/org/apache/commons/lang/reflect/ReflectionUtils.java index 4e60d59e7..77203f61e 100644 --- a/src/java/org/apache/commons/lang/reflect/ReflectionUtils.java +++ b/src/java/org/apache/commons/lang/reflect/ReflectionUtils.java @@ -65,7 +65,7 @@ import org.apache.commons.lang.StringUtils; * reflection.

* * @author Stephen Colebourne - * @version $Id: ReflectionUtils.java,v 1.8 2003/07/16 21:19:22 scolebourne Exp $ + * @version $Id: ReflectionUtils.java,v 1.9 2003/07/19 21:55:05 scolebourne Exp $ */ public class ReflectionUtils { @@ -174,7 +174,7 @@ public class ReflectionUtils { * @throws IllegalArgumentException if the class name is empty */ public static Class getClass(String className) throws ReflectionException { - if (StringUtils.isEmptyOrNull(className)) { + if (StringUtils.isEmpty(className)) { throw new IllegalArgumentException("The class name must not be null"); } try { diff --git a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java index 50703af05..596ef0ecf 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java @@ -63,7 +63,7 @@ import junit.textui.TestRunner; * * @author Stephen Colebourne * @author Ringo De Smet - * @version $Id: StringUtilsTrimEmptyTest.java,v 1.11 2003/07/19 18:10:30 scolebourne Exp $ + * @version $Id: StringUtilsTrimEmptyTest.java,v 1.12 2003/07/19 21:55:05 scolebourne Exp $ */ public class StringUtilsTrimEmptyTest extends TestCase { private static final String FOO = "foo"; @@ -139,67 +139,35 @@ public class StringUtilsTrimEmptyTest extends TestCase { } public void testIsEmpty() { + assertEquals(true, StringUtils.isEmpty(null)); + assertEquals(true, StringUtils.isEmpty("")); + assertEquals(false, StringUtils.isEmpty(" ")); assertEquals(false, StringUtils.isEmpty("foo")); assertEquals(false, StringUtils.isEmpty(" foo ")); - assertEquals(false, StringUtils.isEmpty(" ")); - assertEquals(true, StringUtils.isEmpty("")); - assertEquals(false, StringUtils.isEmpty(null)); } public void testIsNotEmpty() { + assertEquals(false, StringUtils.isNotEmpty(null)); + assertEquals(false, StringUtils.isNotEmpty("")); + assertEquals(true, StringUtils.isNotEmpty(" ")); assertEquals(true, StringUtils.isNotEmpty("foo")); assertEquals(true, StringUtils.isNotEmpty(" foo ")); - assertEquals(true, StringUtils.isNotEmpty(" ")); - assertEquals(false, StringUtils.isNotEmpty("")); - assertEquals(true, StringUtils.isNotEmpty(null)); } - public void testIsEmptyOrNull() { - assertEquals(false, StringUtils.isEmptyOrNull("foo")); - assertEquals(false, StringUtils.isEmptyOrNull(" foo ")); - assertEquals(false, StringUtils.isEmptyOrNull(" ")); - assertEquals(true, StringUtils.isEmptyOrNull("")); - assertEquals(true, StringUtils.isEmptyOrNull(null)); + public void testIsBlank() { + assertEquals(true, StringUtils.isBlank(null)); + assertEquals(true, StringUtils.isBlank("")); + assertEquals(true, StringUtils.isBlank(StringUtilsTest.WHITESPACE)); + assertEquals(false, StringUtils.isBlank("foo")); + assertEquals(false, StringUtils.isBlank(" foo ")); } - public void testIsNotEmptyOrNull() { - assertEquals(true, StringUtils.isNotEmptyOrNull("foo")); - assertEquals(true, StringUtils.isNotEmptyOrNull(" foo ")); - assertEquals(true, StringUtils.isNotEmptyOrNull(" ")); - assertEquals(false, StringUtils.isNotEmptyOrNull("")); - assertEquals(false, StringUtils.isNotEmptyOrNull(null)); - } - - public void testIsEmptyTrimmed() { - assertEquals(false, StringUtils.isEmptyTrimmed("foo")); - assertEquals(false, StringUtils.isEmptyTrimmed(" foo ")); - assertEquals(true, StringUtils.isEmptyTrimmed(" ")); - assertEquals(true, StringUtils.isEmptyTrimmed("")); - assertEquals(false, StringUtils.isEmptyTrimmed(null)); - } - - public void testIsNotEmptyTrimmed() { - assertEquals(true, StringUtils.isNotEmptyTrimmed("foo")); - assertEquals(true, StringUtils.isNotEmptyTrimmed(" foo ")); - assertEquals(false, StringUtils.isNotEmptyTrimmed(" ")); - assertEquals(false, StringUtils.isNotEmptyTrimmed("")); - assertEquals(true, StringUtils.isNotEmptyTrimmed(null)); - } - - public void testIsEmptyTrimmedOrNull() { - assertEquals(false, StringUtils.isEmptyTrimmedOrNull("foo")); - assertEquals(false, StringUtils.isEmptyTrimmedOrNull(" foo ")); - assertEquals(true, StringUtils.isEmptyTrimmedOrNull(" ")); - assertEquals(true, StringUtils.isEmptyTrimmedOrNull("")); - assertEquals(true, StringUtils.isEmptyTrimmedOrNull(null)); - } - - public void testIsNotEmptyTrimmedOrNull() { - assertEquals(true, StringUtils.isNotEmptyTrimmedOrNull("foo")); - assertEquals(true, StringUtils.isNotEmptyTrimmedOrNull(" foo ")); - assertEquals(false, StringUtils.isNotEmptyTrimmedOrNull(" ")); - assertEquals(false, StringUtils.isNotEmptyTrimmedOrNull("")); - assertEquals(false, StringUtils.isNotEmptyTrimmedOrNull(null)); + public void testIsNotBlank() { + assertEquals(false, StringUtils.isNotBlank(null)); + assertEquals(false, StringUtils.isNotBlank("")); + assertEquals(false, StringUtils.isNotBlank(StringUtilsTest.WHITESPACE)); + assertEquals(true, StringUtils.isNotBlank("foo")); + assertEquals(true, StringUtils.isNotBlank(" foo ")); } public void testDeleteSpace() {