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
This commit is contained in:
Stephen Colebourne 2003-07-19 21:55:05 +00:00
parent 9001aa2613
commit 895cedb404
6 changed files with 76 additions and 187 deletions

View File

@ -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.

View File

@ -65,7 +65,7 @@ import java.util.List;
* @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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);

View File

@ -98,7 +98,7 @@ import org.apache.commons.lang.math.NumberUtils;
* @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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 {
//-----------------------------------------------------------------------
/**
* <p>Checks if a String is empty ("").
* <p>Checks if a String is empty ("") or null.
* <code>null</code> returns <code>false</code></p>
*
* <pre>
* 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 {
* </pre>
*
* <p>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().</p>
* It no longer trims the String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty
* @return <code>true</code> 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);
}
/**
* <p>Checks if a String is not empty ("").
* <code>null</code> returns <code>true</code></p>
* <p>Checks if a String is not empty ("") and not null.</p>
*
* <pre>
* StringUtils.isNotEmpty(null) = true
* StringUtils.isNotEmpty(null) = false
* StringUtils.isNotEmpty("") = false
* StringUtils.isNotEmpty(" ") = true
* StringUtils.isNotEmpty("bob") = true
* StringUtils.isNotEmpty(" bob ") = true
* </pre>
*
* <p>NOTE: This method changed in version 2.0.
* It no longer returns false for null.
* That functionality is available in isNotEmptyOrNull().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is not empty
* @return <code>true</code> if the String is not empty and not null
*/
public static boolean isNotEmpty(String str) {
return (str == null || str.length() > 0);
}
/**
* <p>Checks if a String is empty ("") or <code>null</code>.</p>
*
* <pre>
* StringUtils.isEmptyOrNull(null) = true
* StringUtils.isEmptyOrNull("") = true
* StringUtils.isEmptyOrNull(" ") = false
* StringUtils.isEmptyOrNull("bob") = false
* StringUtils.isEmptyOrNull(" bob ") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmptyOrNull(String str) {
return (str == null || str.length() == 0);
}
/**
* <p>Checks if a String is not not empty ("") and not <code>null</code>.</p>
*
* <pre>
* StringUtils.isNotEmptyOrNull(null) = false
* StringUtils.isNotEmptyOrNull("") = false
* StringUtils.isNotEmptyOrNull(" ") = true
* StringUtils.isNotEmptyOrNull("bob") = true
* StringUtils.isNotEmptyOrNull(" bob ") = true
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is neither empty nor null
*/
public static boolean isNotEmptyOrNull(String str) {
return (str != null && str.length() > 0);
}
/**
* <p>Checks if a trimmed String is empty ("").
* <code>null</code> returns <code>false</code></p>
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
*
* <pre>
* 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
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty after trim()
* @return <code>true</code> 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;
}
/**
* <p>Checks if a trimmed String is not empty ("").</p>
* <code>null</code> returns <code>true</code></p>
* <p>Checks if a String is not empty ("") and not null.</p>
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
*
* <pre>
* 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
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to check, may be null
* @return <code>true</code> if the String is not empty after trim()
* @return <code>true</code> 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);
}
/**
* <p>Checks if a trimmed String is empty ("") or <code>null</code>.</p>
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
*
* <pre>
* StringUtils.isEmptyTrimmedOrNull(null) = true
* StringUtils.isEmptyTrimmedOrNull("") = true
* StringUtils.isEmptyTrimmedOrNull(" ") = true
* StringUtils.isEmptyTrimmedOrNull("bob") = false
* StringUtils.isEmptyTrimmedOrNull(" bob ") = false
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to check, may be null
* @return <code>true</code> if the String is <code>null</code>
* or empty after trim()
*/
public static boolean isEmptyTrimmedOrNull(String str) {
return (str == null || str.trim().length() == 0);
}
/**
* <p>Checks if a trimmed String is not empty ("") and not <code>null</code>.</p>
*
* <p>The String is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
*
* <pre>
* StringUtils.isNotEmptyTrimmedOrNull(null) = false
* StringUtils.isNotEmptyTrimmedOrNull("") = false
* StringUtils.isNotEmptyTrimmedOrNull(" ") = false
* StringUtils.isNotEmptyTrimmedOrNull("bob") = true
* StringUtils.isNotEmptyTrimmedOrNull(" bob ") = true
* </pre>
*
* @see java.lang.String#trim()
* @param str the String to check, may be null
* @return <code>true</code> if the String is not <code>null</code>
* 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

View File

@ -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;

View File

@ -65,7 +65,7 @@ import org.apache.commons.lang.StringUtils;
* reflection.</p>
*
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @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 {

View File

@ -63,7 +63,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @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() {