For example, if the contract of a public method states it does not
* allow null
arguments, Assert can be used to validate that
* contract. Doing this clearly indicates a contract violation when it
* occurs and protects the class's invariants.
*
*
Typically used to validate method arguments rather than configuration * properties, to check for cases that are usually programmer errors rather than * configuration errors. In contrast to config initialization code, there is * usally no point in falling back to defaults in such methods. * *
This class is similar to JUnit's assertion library. If an argument value is * deemed invalid, an {@link IllegalArgumentException} is thrown (typically). * For example: * *
* Assert.notNull(clazz, "The class must not be null"); * Assert.isTrue(i > 0, "The value must be greater than zero");* * Mainly for internal use within the framework; consider Jakarta's Commons Lang * >= 2.0 for a more comprehensive suite of assertion utilities. * * @author Keith Donald * @author Juergen Hoeller * @author Colin Sampaleanu * @author Rob Harrop * @since 1.1.2 */ public abstract class Assert { /** * Assert a boolean expression, throwing
IllegalArgumentException
* if the test result is false
.
* Assert.isTrue(i > 0, "The value must be greater than zero");* @param expression a boolean expression * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if expression is
false
*/
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
/**
* Assert a boolean expression, throwing IllegalArgumentException
* if the test result is false
.
* Assert.isTrue(i > 0);* @param expression a boolean expression * @throws IllegalArgumentException if expression is
false
*/
public static void isTrue(boolean expression) {
isTrue(expression, "[Assertion failed] - this expression must be true");
}
/**
* Assert a boolean expression, throwing IllegalArgumentException
* if the test result is true
.
* Assert.isFalse(i > 0);* @param expression a boolean expression * @throws IllegalArgumentException if expression is
true
*/
public static void isFalse(boolean expression) {
isTrue(!expression, "[Assertion failed] - this expression must be false");
}
/**
* Assert a boolean expression, throwing IllegalArgumentException
* if the test result is true
.
* Assert.isFalse(i <= 0, "The value must be greater than zero");* @param expression a boolean expression * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if expression is
true
*/
public static void isFalse(boolean expression, String message) {
isTrue(!expression, message);
}
/**
* Assert that an object is null
.
* Assert.isNull(value, "The value must be null");* @param object the object to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the object is not
null
*/
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
/**
* Assert that an object is null
.
* Assert.isNull(value);* @param object the object to check * @throws IllegalArgumentException if the object is not
null
*/
public static void isNull(Object object) {
isNull(object, "[Assertion failed] - the object argument must be null");
}
/**
* Assert that an object is not null
.
* Assert.notNull(clazz, "The class must not be null");* @param object the object to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the object is
null
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/**
* Assert that an object is not null
.
* Assert.notNull(clazz);* @param object the object to check * @throws IllegalArgumentException if the object is
null
*/
public static void notNull(Object object) {
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
/**
* Assert that the given String is not empty; that is,
* it must not be null
and not the empty String.
* Assert.hasLength(name, "Name must not be empty");* @param text the String to check * @param message the exception message to use if the assertion fails * @see StringUtils#hasLength */ public static void hasLength(String text, String message) { if (!StringUtils.hasLength(text)) { throw new IllegalArgumentException(message); } } /** * Assert that the given String is not empty; that is, * it must not be
null
and not the empty String.
* Assert.hasLength(name);* @param text the String to check * @see StringUtils#hasLength */ public static void hasLength(String text) { hasLength(text, "[Assertion failed] - this String argument must have length; it must not be
null
or empty");
}
/**
* Assert that the given String has valid text content; that is, it must not
* be null
and must contain at least one non-whitespace character.
* Assert.hasText(name, "'name' must not be empty");* @param text the String to check * @param message the exception message to use if the assertion fails * @see StringUtils#hasText */ public static void hasText(String text, String message) { if (!StringUtils.hasText(text)) { throw new IllegalArgumentException(message); } } /** * Assert that the given String has valid text content; that is, it must not * be
null
and must contain at least one non-whitespace character.
* Assert.hasText(name, "'name' must not be empty");* @param text the String to check * @see StringUtils#hasText */ public static void hasText(String text) { hasText(text, "[Assertion failed] - this String argument must have text; it must not be
null
, empty, or blank");
}
/**
* Assert that the given text does not contain the given substring.
* Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");* @param textToSearch the text to search * @param substring the substring to find within the text * @param message the exception message to use if the assertion fails */ public static void doesNotContain(String textToSearch, String substring, String message) { if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.indexOf(substring) != -1) { throw new IllegalArgumentException(message); } } /** * Assert that the given text does not contain the given substring. *
Assert.doesNotContain(name, "rod");* @param textToSearch the text to search * @param substring the substring to find within the text */ public static void doesNotContain(String textToSearch, String substring) { doesNotContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); } } /** * Miscellaneous {@link String} utility methods. * *
Mainly for internal use within the framework; consider * Jakarta's Commons Lang * for a more comprehensive suite of * {@link org.apache.commons.lang.StringUtils String utilities}. * *
This class delivers some simple functionality that should really
* be provided by the core Java String
and {@link StringBuffer}
* classes, such as the ability to {@link #replace} all occurrences of a given
* substring in a target string. It also provides easy-to-use methods to
* convert between delimited strings, such as CSV strings, and collections and
* arrays.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Keith Donald
* @author Rob Harrop
* @author Rick Evans
* @author Jacky.Song
* @since 16 April 2001
*/
abstract class StringUtils {
/**
* Check that the given String is neither null
nor of length 0.
* Note: Will return true
for a String that purely consists of whitespace.
*
* StringUtils.hasLength(null) = false * StringUtils.hasLength("") = false * StringUtils.hasLength(" ") = true * StringUtils.hasLength("Hello") = true ** @param str the String to check (may be
null
)
* @return true
if the String is not null and has length
* @see #hasText(String)
*/
public static boolean hasLength(String str) {
return (str != null && str.length() > 0);
}
/**
* Check whether the given String has actual text.
* More specifically, returns true
if the string not null
,
* its length is greater than 0, and it contains at least one non-whitespace character.
* * StringUtils.hasText(null) = false * StringUtils.hasText("") = false * StringUtils.hasText(" ") = false * StringUtils.hasText("12345") = true * StringUtils.hasText(" 12345 ") = true ** @param str the String to check (may be
null
)
* @return true
if the String is not null
, its length is
* greater than 0, and it does not contain whitespace only
* @see java.lang.Character#isWhitespace
*/
public static boolean hasText(String str) {
if (!hasLength(str)) {
return false;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
/**
* Check whether the given String contains any whitespace characters.
* @param str the String to check (may be null
)
* @return true
if the String is not empty and
* contains at least 1 whitespace character
* @see java.lang.Character#isWhitespace
*/
public static boolean containsWhitespace(String str) {
if (!hasLength(str)) {
return false;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
/**
* Capitalize a String
, changing the first letter to
* upper case as per {@link Character#toUpperCase(char)}.
* No other letters are changed.
* @param str the String to capitalize, may be null
* @return the capitalized String, null
if null
*/
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}
/**
* Uncapitalize a String
, changing the first letter to
* lower case as per {@link Character#toLowerCase(char)}.
* No other letters are changed.
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, null
if null
*/
public static String uncapitalize(String str) {
return changeFirstCharacterCase(str, false);
}
private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer buf = new StringBuffer(str.length());
if (capitalize) {
buf.append(Character.toUpperCase(str.charAt(0)));
}
else {
buf.append(Character.toLowerCase(str.charAt(0)));
}
buf.append(str.substring(1));
return buf.toString();
}
}