Implement changes to StringUtils.isEmpty and friends

This is an incompatible change for isEmpty and isNotEmpty from 1.0


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137441 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-16 21:19:22 +00:00
parent 25b9bd6244
commit f59fb46272
6 changed files with 242 additions and 62 deletions

View File

@ -1,4 +1,4 @@
$Id: RELEASE-NOTES.txt,v 1.11 2003/07/15 23:42:13 scolebourne Exp $ $Id: RELEASE-NOTES.txt,v 1.12 2003/07/16 21:19:22 scolebourne Exp $
Commons Lang Package Commons Lang Package
Version 2.0 Version 2.0
@ -11,6 +11,16 @@ This document contains the release notes for this version of the Commons
Lang package. Commons Lang is a set of utility functions and reusable Lang package. Commons Lang is a set of utility functions and reusable
components that should be a help in any Java environment. 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)
Check your code before implementing this release.
NEW FEATURES: NEW FEATURES:
Since the release of the 1.0 package the following classes have been added: Since the release of the 1.0 package the following classes have been added:
@ -63,9 +73,11 @@ lang:
SerializationUtils: SerializationUtils:
added empty constructor added empty constructor
StringUtils: StringUtils:
isEmpty() changed to not trim. isEmpty() changed to not trim and return false for null.
isNotEmpty() changed return true for null.
chomp() changed to be more like Perl. chomp() changed to be more like Perl.
Twenty-one new methods. Various methods changed in the handling of null (less exceptions).
Thirty-one new methods.
Eight methods deprecated. Eight methods deprecated.
SystemUtils: SystemUtils:
isJavaVersionAtLeast(int) added. getJavaVersion() deprecated. isJavaVersionAtLeast(int) added. getJavaVersion() deprecated.
@ -150,6 +162,7 @@ lang:
NumberUtils: NumberUtils:
now deprecated, see math subpackage now deprecated, see math subpackage
CHANGES: [In 'diff' format] CHANGES: [In 'diff' format]
Jar changes Jar changes
@ -248,8 +261,12 @@ org.apache.commons.lang.StringUtils
-------------------- --------------------
> public static java.lang.String trimToNull(java.lang.String); > public static java.lang.String trimToNull(java.lang.String);
> public static java.lang.String trimToEmpty(java.lang.String); > public static java.lang.String trimToEmpty(java.lang.String);
> public static boolean isEmptyOrNull(java.lang.String);
> public static boolean isNotEmptyOrNull(java.lang.String);
> public static boolean isEmptyTrimmed(java.lang.String); > public static boolean isEmptyTrimmed(java.lang.String);
> public static boolean isNotEmptyTrimmed(java.lang.String); > public static boolean isNotEmptyTrimmed(java.lang.String);
> public static boolean isEmptyTrimmedOrNull(java.lang.String);
> public static boolean isNotEmptyTrimmedOrNull(java.lang.String);
> public static java.lang.String join(java.lang.Object[], char); > public static java.lang.String join(java.lang.Object[], char);
> public static java.lang.String join(java.util.Iterator, char); > public static java.lang.String join(java.util.Iterator, char);
> public static java.lang.String slice(java.lang.String); > public static java.lang.String slice(java.lang.String);
@ -268,16 +285,12 @@ org.apache.commons.lang.StringUtils
> public static boolean containsOnly(java.lang.String, java.lang.String); > public static boolean containsOnly(java.lang.String, java.lang.String);
> public static boolean containsNone(java.lang.String, java.lang.String); > public static boolean containsNone(java.lang.String, java.lang.String);
> public static boolean containsNone(java.lang.String, char[]); > public static boolean containsNone(java.lang.String, char[]);
> public static boolean containsOnly(java.lang.String, char[]);
> public static int indexOfAnyBut(java.lang.String, char[]); > public static int indexOfAnyBut(java.lang.String, char[]);
> public static int indexOfAnyBut(java.lang.String, java.lang.String); > public static int indexOfAnyBut(java.lang.String, java.lang.String);
> public static java.lang.String defaultString(java.lang.Object);
> public static java.lang.String defaultString(java.lang.Object, java.lang.String);
> public static java.lang.String abbreviate(java.lang.String, int); > public static java.lang.String abbreviate(java.lang.String, int);
> public static java.lang.String abbreviate(java.lang.String, int, int); > public static java.lang.String abbreviate(java.lang.String, int, int);
> public static java.lang.String difference(java.lang.String, java.lang.String); > public static java.lang.String difference(java.lang.String, java.lang.String);
> public static int differenceAt(java.lang.String, java.lang.String); > public static int differenceAt(java.lang.String, java.lang.String);
< public static boolean containsOnly(java.lang.String, char[]);
--- ---
> static {}; > static {};

View File

@ -62,7 +62,7 @@ import java.util.List;
* @author Stephen Colebourne * @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 2.0 * @since 2.0
* @version $Id: ClassUtils.java,v 1.14 2003/07/16 00:39:05 scolebourne Exp $ * @version $Id: ClassUtils.java,v 1.15 2003/07/16 21:19:22 scolebourne Exp $
*/ */
public class ClassUtils { public class ClassUtils {
@ -133,12 +133,13 @@ public class ClassUtils {
* *
* <p>The string passed in is assumed to be a class name - it is not checked.</p> * <p>The string passed in is assumed to be a class name - it is not checked.</p>
* *
* @param className the className to get the short name for, must not be empty * @param className the className to get the short name for,
* must not be empty or <code>null</code>
* @return the class name of the class without the package name * @return the class name of the class without the package name
* @throws IllegalArgumentException if the className is empty * @throws IllegalArgumentException if the className is empty
*/ */
public static String getShortClassName(String className) { public static String getShortClassName(String className) {
if (StringUtils.isEmpty(className)) { if (StringUtils.isEmptyOrNull(className)) {
throw new IllegalArgumentException("The class name must not be empty"); throw new IllegalArgumentException("The class name must not be empty");
} }
char[] chars = className.toCharArray(); char[] chars = className.toCharArray();
@ -172,8 +173,8 @@ public class ClassUtils {
/** /**
* <p>Gets the package name of a <code>Class</code>.</p> * <p>Gets the package name of a <code>Class</code>.</p>
* *
* @param cls the class to get the package name for, must not be * @param cls the class to get the package name for,
* <code>null</code> * must not be <code>null</code>
* @return the package name * @return the package name
* @throws IllegalArgumentException if the class is <code>null</code> * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
@ -189,12 +190,13 @@ public class ClassUtils {
* *
* <p>The string passed in is assumed to be a class name - it is not checked.</p> * <p>The string passed in is assumed to be a class name - it is not checked.</p>
* *
* @param className the className to get the package name for, must not be empty * @param className the className to get the package name for,
* must not be empty or <code>null</code>
* @return the package name * @return the package name
* @throws IllegalArgumentException if the className is empty * @throws IllegalArgumentException if the className is empty
*/ */
public static String getPackageName(String className) { public static String getPackageName(String className) {
if (StringUtils.isEmpty(className)) { if (StringUtils.isEmptyOrNull(className)) {
throw new IllegalArgumentException("The class name must not be empty"); throw new IllegalArgumentException("The class name must not be empty");
} }
int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);

View File

@ -90,7 +90,7 @@ import org.apache.commons.lang.math.NumberUtils;
* @author Arun Mammen Thomas * @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0 * @since 1.0
* @version $Id: StringUtils.java,v 1.57 2003/07/16 20:19:24 scolebourne Exp $ * @version $Id: StringUtils.java,v 1.58 2003/07/16 21:19:22 scolebourne Exp $
*/ */
public class StringUtils { public class StringUtils {
@ -129,16 +129,16 @@ public class StringUtils {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p>Removes control characters, including whitespace, from both * <p>Removes control characters (char &lt;= 32) from both
* ends of this String, handling <code>null</code> by returning * ends of this String, handling <code>null</code> by returning
* an empty String ("").</p> * an empty String ("").</p>
* *
* <pre> * <pre>
* StringUtils.clean(null) = ""
* StringUtils.clean("abc") = "abc" * StringUtils.clean("abc") = "abc"
* StringUtils.clean(" abc ") = "abc" * StringUtils.clean(" abc ") = "abc"
* StringUtils.clean(" ") = "" * StringUtils.clean(" ") = ""
* StringUtils.clean("") = "" * StringUtils.clean("") = ""
* StringUtils.clean(null) = ""
* </pre> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -152,18 +152,19 @@ public class StringUtils {
} }
/** /**
* <p>Removes control characters, including whitespace, from both * <p>Removes control characters (char &lt;= 32) from both
* ends of this String, handling <code>null</code> by returning * ends of this String, handling <code>null</code> by returning
* <code>null</code>.</p> * <code>null</code>.</p>
* *
* <p>The string is trimmed using {@link String#trim()}.</p> * <p>The string is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* *
* <pre> * <pre>
* StringUtils.trim(null) = null
* StringUtils.trim("abc") = "abc" * StringUtils.trim("abc") = "abc"
* StringUtils.trim(" abc ") = "abc" * StringUtils.trim(" abc ") = "abc"
* StringUtils.trim(" ") = "" * StringUtils.trim(" ") = ""
* StringUtils.trim("") = "" * StringUtils.trim("") = ""
* StringUtils.trim(null) = null
* </pre> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -176,18 +177,19 @@ public class StringUtils {
} }
/** /**
* <p>Removes control characters, including whitespace, from both * <p>Removes control characters (char &lt;= 32) from both
* ends of this string returning <code>null</code> if the string is * ends of this string returning <code>null</code> if the string is
* empty ("") after the trim or if it is <code>null</code>. * empty ("") after the trim or if it is <code>null</code>.
* *
* <p>The string is trimmed using {@link String#trim()}.</p> * <p>The string is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* *
* <pre> * <pre>
* StringUtils.trimToNull(null) = null
* StringUtils.trimToNull("abc") = "abc" * StringUtils.trimToNull("abc") = "abc"
* StringUtils.trimToNull(" abc ") = "abc" * StringUtils.trimToNull(" abc ") = "abc"
* StringUtils.trimToNull(" ") = null * StringUtils.trimToNull(" ") = null
* StringUtils.trimToNull("") = null * StringUtils.trimToNull("") = null
* StringUtils.trimToNull(null) = null
* </pre> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -201,18 +203,19 @@ public class StringUtils {
} }
/** /**
* <p>Removes control characters, including whitespace, from both * <p>Removes control characters (char &lt;= 32) from both
* ends of this string returning an empty string ("") if the string * ends of this string returning an empty string ("") if the string
* is empty ("") after the trim or if it is <code>null</code>. * is empty ("") after the trim or if it is <code>null</code>.
* *
* <p>The string is trimmed using {@link String#trim()}.</p> * <p>The string is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* *
* <pre> * <pre>
* StringUtils.trimToEmpty(null) = ""
* StringUtils.trimToEmpty("abc") = "abc" * StringUtils.trimToEmpty("abc") = "abc"
* StringUtils.trimToEmpty(" abc ") = "abc" * StringUtils.trimToEmpty(" abc ") = "abc"
* StringUtils.trimToEmpty(" ") = "" * StringUtils.trimToEmpty(" ") = ""
* StringUtils.trimToEmpty("") = "" * StringUtils.trimToEmpty("") = ""
* StringUtils.trimToEmpty(null) = ""
* </pre> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -227,6 +230,14 @@ public class StringUtils {
* <p>Deletes all 'space' characters from a String as defined by * <p>Deletes all 'space' characters from a String as defined by
* {@link Character#isSpace(char)}.</p> * {@link Character#isSpace(char)}.</p>
* *
* <pre>
* StringUtils.deleteSpaces(null) = null
* StringUtils.deleteSpaces("abc") = "abc"
* StringUtils.deleteSpaces(" \t abc \n ") = "abc"
* StringUtils.deleteSpaces("ab c") = "abc"
* StringUtils.deleteSpaces("a\nb\tc ") = "abc"
* </pre>
*
* <p>Spaces are defined as <code>{' ', '\t', '\r', '\n', '\b'}</code> * <p>Spaces are defined as <code>{' ', '\t', '\r', '\n', '\b'}</code>
* in line with the deprecated <code>isSpace</code> method.</p> * in line with the deprecated <code>isSpace</code> method.</p>
* *
@ -244,6 +255,12 @@ public class StringUtils {
* <p>Deletes all whitespaces from a String as defined by * <p>Deletes all whitespaces from a String as defined by
* {@link Character#isWhitespace(char)}.</p> * {@link Character#isWhitespace(char)}.</p>
* *
* <pre>
* StringUtils.deleteWhitespace(null) = null
* StringUtils.deleteWhitespace("abc") = "abc"
* StringUtils.deleteWhitespace(" abc ") = "abc"
* </pre>
*
* @param str the String to delete whitespace from, may be null * @param str the String to delete whitespace from, may be null
* @return the String without whitespaces, <code>null</code> if null string input * @return the String without whitespaces, <code>null</code> if null string input
*/ */
@ -265,10 +282,11 @@ public class StringUtils {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p>Checks if a String is <code>null</code> or empty ("").</p> * <p>Checks if a String is empty ("").
* <code>null</code> returns <code>false</code></p>
* *
* <pre> * <pre>
* StringUtils.isEmpty(null) = true * StringUtils.isEmpty(null) = false
* StringUtils.isEmpty("") = true * StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false * StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false * StringUtils.isEmpty("bob") = false
@ -276,43 +294,133 @@ public class StringUtils {
* </pre> * </pre>
* *
* <p>NOTE: This method changed in version 2.0. * <p>NOTE: This method changed in version 2.0.
* It no longer trims the String. * It no longer trims the String, and null is no longer true.
* That functionality is available in isEmptyTrimmed().</p> * That functionality is available in isEmptyTrimmedOrNull().</p>
* *
* @param str the String to check, may be null * @param str the String to check, may be null
* @return <code>true</code> if the String is <code>null</code> or empty * @return <code>true</code> if the String is empty
*/ */
public static boolean isEmpty(String str) { 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 <code>null</code> and not empty ("").</p> * <p>Checks if a String is not empty ("").
* <code>null</code> returns <code>true</code></p>
* *
* <pre> * <pre>
* StringUtils.isNotEmpty(null) = false * StringUtils.isNotEmpty(null) = true
* StringUtils.isNotEmpty("") = false * StringUtils.isNotEmpty("") = false
* StringUtils.isNotEmpty(" ") = true * StringUtils.isNotEmpty(" ") = true
* StringUtils.isNotEmpty("bob") = true * StringUtils.isNotEmpty("bob") = true
* StringUtils.isNotEmpty(" bob ") = true * StringUtils.isNotEmpty(" bob ") = true
* </pre> * </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 * @param str the String to check, may be null
* @return <code>true</code> if the String is not <code>null</code> and not empty * @return <code>true</code> if the String is not empty
*/ */
public static boolean isNotEmpty(String str) { 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); return (str != null && str.length() > 0);
} }
/** /**
* <p>Checks if a trimmed String is <code>null</code> or empty ("").</p> * <p>Checks if a trimmed String is empty ("").
* <code>null</code> returns <code>false</code></p>
*
* <p>The string is trimmed using {@link String#trim()}.
* Trim removes start and end characters &lt;= 32.</p>
* *
* <pre> * <pre>
* StringUtils.isNotEmpty(null) = true * StringUtils.isEmptyTrimmed(null) = false
* StringUtils.isNotEmpty("") = true * StringUtils.isEmptyTrimmed("") = true
* StringUtils.isNotEmpty(" ") = true * StringUtils.isEmptyTrimmed(" ") = true
* StringUtils.isNotEmpty("bob") = false * StringUtils.isEmptyTrimmed("bob") = false
* StringUtils.isNotEmpty(" bob ") = false * StringUtils.isEmptyTrimmed(" 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()
*/
public static boolean isEmptyTrimmed(String str) {
return (str != null && str.trim().length() == 0);
}
/**
* <p>Checks if a trimmed String is not empty ("").</p>
* <code>null</code> returns <code>true</code></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
* </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()
*/
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> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -320,19 +428,22 @@ public class StringUtils {
* @return <code>true</code> if the String is <code>null</code> * @return <code>true</code> if the String is <code>null</code>
* or empty after trim() * or empty after trim()
*/ */
public static boolean isEmptyTrimmed(String str) { public static boolean isEmptyTrimmedOrNull(String str) {
return (str == null || str.trim().length() == 0); return (str == null || str.trim().length() == 0);
} }
/** /**
* <p>Checks if a trimmed String is not <code>null</code> and not empty ("").</p> * <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> * <pre>
* StringUtils.isNotEmpty(null) = false * StringUtils.isNotEmptyTrimmedOrNull(null) = false
* StringUtils.isNotEmpty("") = false * StringUtils.isNotEmptyTrimmedOrNull("") = false
* StringUtils.isNotEmpty(" ") = false * StringUtils.isNotEmptyTrimmedOrNull(" ") = false
* StringUtils.isNotEmpty("bob") = true * StringUtils.isNotEmptyTrimmedOrNull("bob") = true
* StringUtils.isNotEmpty(" bob ") = true * StringUtils.isNotEmptyTrimmedOrNull(" bob ") = true
* </pre> * </pre>
* *
* @see java.lang.String#trim() * @see java.lang.String#trim()
@ -340,7 +451,7 @@ public class StringUtils {
* @return <code>true</code> if the String is not <code>null</code> * @return <code>true</code> if the String is not <code>null</code>
* and not empty after trim() * and not empty after trim()
*/ */
public static boolean isNotEmptyTrimmed(String str) { public static boolean isNotEmptyTrimmedOrNull(String str) {
return (str != null && str.trim().length() > 0); return (str != null && str.trim().length() > 0);
} }
@ -375,7 +486,7 @@ public class StringUtils {
* <p>Compares two Strings, returning <code>true</code> if they are equal ignoring * <p>Compares two Strings, returning <code>true</code> if they are equal ignoring
* the case.</p> * the case.</p>
* *
* <p><code>Nulls</code> are handled without exceptions. Two <code>null</code> * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
* references are considered equal. Comparison is case insensitive.</p> * references are considered equal. Comparison is case insensitive.</p>
* *
* <pre> * <pre>
@ -401,6 +512,16 @@ public class StringUtils {
* *
* <p><code>null</code> String will return <code>-1</code>.</p> * <p><code>null</code> String will return <code>-1</code>.</p>
* *
* <pre>
* StringUtils.indexOfAny(null, null) = -1
* StringUtils.indexOfAny(null, ["ab","cd"]) = -1
* StringUtils.indexOfAny("zzabyycdxx", null) = -1
* StringUtils.indexOfAny("zzabyycdxx", []) = -1
* StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"]) = 2
* StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"]) = 2
* StringUtils.indexOfAny("zzabyycdxx", ["mn","op"]) = -1
* </pre>
*
* @param str the String to check, may be null * @param str the String to check, may be null
* @param searchStrs the Strings to search for, may be null * @param searchStrs the Strings to search for, may be null
* @return the first index of any of the searchStrs in str, -1 if no match * @return the first index of any of the searchStrs in str, -1 if no match
@ -435,6 +556,16 @@ public class StringUtils {
* *
* <p><code>null</code> string will return <code>-1</code>.</p> * <p><code>null</code> string will return <code>-1</code>.</p>
* *
* <pre>
* StringUtils.lastIndexOfAny(null, null) = -1
* StringUtils.lastIndexOfAny(null, ["ab","cd"]) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", null) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", []) = -1
* StringUtils.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6
* StringUtils.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6
* StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
* </pre>
*
* @param str the String to check, may be null * @param str the String to check, may be null
* @param searchStrs the Strings to search for, may be null * @param searchStrs the Strings to search for, may be null
* @return the last index of any of the Strings, -1 if no match * @return the last index of any of the Strings, -1 if no match

View File

@ -201,7 +201,7 @@ import org.apache.commons.lang.StringUtils;
* @author Chris Webb * @author Chris Webb
* @author Mike Bowler * @author Mike Bowler
* @since 1.0 * @since 1.0
* @version $Id: Enum.java,v 1.12 2003/07/14 22:58:31 scolebourne Exp $ * @version $Id: Enum.java,v 1.13 2003/07/16 21:19:21 scolebourne Exp $
*/ */
public abstract class Enum implements Comparable, Serializable { public abstract class Enum implements Comparable, Serializable {
// After discussion, the default size for HashMaps is used, as the // After discussion, the default size for HashMaps is used, as the
@ -243,14 +243,15 @@ public abstract class Enum implements Comparable, Serializable {
/** /**
* <p>Constructor to add a new named item to the enumeration.</p> * <p>Constructor to add a new named item to the enumeration.</p>
* *
* @param name the name of the enum object * @param name the name of the enum object,
* must not be empty or <code>null</code>
* @throws IllegalArgumentException if the name is <code>null</code> * @throws IllegalArgumentException if the name is <code>null</code>
* or a blank string * or an empty string
*/ */
protected Enum(String name) { protected Enum(String name) {
super(); super();
if (StringUtils.isEmpty(name)) { if (StringUtils.isEmptyOrNull(name)) {
throw new IllegalArgumentException("The Enum name must not be empty or null"); throw new IllegalArgumentException("The Enum name must not be empty or null");
} }
iName = name; iName = name;

View File

@ -65,7 +65,7 @@ import org.apache.commons.lang.StringUtils;
* reflection.</p> * reflection.</p>
* *
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @version $Id: ReflectionUtils.java,v 1.7 2003/07/14 22:28:48 bayard Exp $ * @version $Id: ReflectionUtils.java,v 1.8 2003/07/16 21:19:22 scolebourne Exp $
*/ */
public class ReflectionUtils { public class ReflectionUtils {
@ -167,13 +167,14 @@ public class ReflectionUtils {
/** /**
* <p>Gets a class object for the specified string.</p> * <p>Gets a class object for the specified string.</p>
* *
* @param className fully qualified class name to find, must not be empty * @param className fully qualified class name to find,
* must not be empty or <code>null</code>
* @return Class object for class * @return Class object for class
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class name is empty * @throws IllegalArgumentException if the class name is empty
*/ */
public static Class getClass(String className) throws ReflectionException { public static Class getClass(String className) throws ReflectionException {
if (StringUtils.isEmpty(className)) { if (StringUtils.isEmptyOrNull(className)) {
throw new IllegalArgumentException("The class name must not be null"); throw new IllegalArgumentException("The class name must not be null");
} }
try { 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:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a> * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.8 2003/07/14 22:26:22 scolebourne Exp $ * @version $Id: StringUtilsTrimEmptyTest.java,v 1.9 2003/07/16 21:19:22 scolebourne Exp $
*/ */
public class StringUtilsTrimEmptyTest extends TestCase { public class StringUtilsTrimEmptyTest extends TestCase {
private static final String FOO = "foo"; private static final String FOO = "foo";
@ -137,7 +137,7 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(false, StringUtils.isEmpty(" foo ")); assertEquals(false, StringUtils.isEmpty(" foo "));
assertEquals(false, StringUtils.isEmpty(" ")); assertEquals(false, StringUtils.isEmpty(" "));
assertEquals(true, StringUtils.isEmpty("")); assertEquals(true, StringUtils.isEmpty(""));
assertEquals(true, StringUtils.isEmpty(null)); assertEquals(false, StringUtils.isEmpty(null));
} }
public void testIsNotEmpty() { public void testIsNotEmpty() {
@ -145,7 +145,23 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(true, StringUtils.isNotEmpty(" foo ")); assertEquals(true, StringUtils.isNotEmpty(" foo "));
assertEquals(true, StringUtils.isNotEmpty(" ")); assertEquals(true, StringUtils.isNotEmpty(" "));
assertEquals(false, StringUtils.isNotEmpty("")); assertEquals(false, StringUtils.isNotEmpty(""));
assertEquals(false, StringUtils.isNotEmpty(null)); 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 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() { public void testIsEmptyTrimmed() {
@ -153,7 +169,7 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(false, StringUtils.isEmptyTrimmed(" foo ")); assertEquals(false, StringUtils.isEmptyTrimmed(" foo "));
assertEquals(true, StringUtils.isEmptyTrimmed(" ")); assertEquals(true, StringUtils.isEmptyTrimmed(" "));
assertEquals(true, StringUtils.isEmptyTrimmed("")); assertEquals(true, StringUtils.isEmptyTrimmed(""));
assertEquals(true, StringUtils.isEmptyTrimmed(null)); assertEquals(false, StringUtils.isEmptyTrimmed(null));
} }
public void testIsNotEmptyTrimmed() { public void testIsNotEmptyTrimmed() {
@ -161,7 +177,23 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(true, StringUtils.isNotEmptyTrimmed(" foo ")); assertEquals(true, StringUtils.isNotEmptyTrimmed(" foo "));
assertEquals(false, StringUtils.isNotEmptyTrimmed(" ")); assertEquals(false, StringUtils.isNotEmptyTrimmed(" "));
assertEquals(false, StringUtils.isNotEmptyTrimmed("")); assertEquals(false, StringUtils.isNotEmptyTrimmed(""));
assertEquals(false, StringUtils.isNotEmptyTrimmed(null)); 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 testDeleteSpace() { public void testDeleteSpace() {