From 895cedb4049f691516460a6f39eca37411fb5dcf Mon Sep 17 00:00:00 2001
From: Stephen Colebourne Checks if a String is empty ("").
+ * Checks if a String is empty ("") or null.
* 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().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 {
*
*
*
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 - * @returntrue
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() {