public final class ArgumentUtils extends Object
Public and protected methods should throw an exception if invalid arguments are passed to them. This class has convenience methods for commonly-used checks.
Example Usage:
public void doSomething(String name) {
ArgumentUtils.checkNotNullNorBlank(name, "name");
...
}
| Modifier and Type | Method and Description |
|---|---|
static void |
checkMaxLength(String value,
int maxLen,
String name)
Check that the length of a string does not exceed a max length.
|
static void |
checkNotNull(Object value,
String name)
Throws an exception if an argument value is null.
|
static void |
checkNotNullNorBlank(String value,
String name)
Throws an exception if a string argument is null, is an empty string or is
made of whitespace characters only.
|
static void |
checkNotNullNorEmpty(Collection<?> collection,
String name)
Throws an exception if a collection argument is null or has no items.
|
static void |
checkNullOrBlank(String value,
String name)
Check that an argument is blank (null or empty string).
|
static boolean |
isEqual(Object value1,
Object value2)
Checks two arguments for equality, handling nulls.
|
static boolean |
isNullOrBlank(String value)
Checks if a string argument is null or has no non-whitespace characters.
|
static boolean |
isNullOrEmpty(Collection<?> collection)
Checks if a collection argument is null or has no items.
|
public static void checkNotNull(Object value, String name)
value - Value of the argument to check.name - Name of the argument.IllegalArgumentException - If the argument is null.public static void checkNotNullNorEmpty(Collection<?> collection, String name)
collection - A collection to check.name - Name of the collection argument.NullPointerException - If the collection argument is null.IllegalArgumentException - If the collection argument does not contain at least one item.public static void checkNotNullNorBlank(String value, String name)
value - Value of the string argument to check.name - Name of the string argument.NullPointerException - If the string argument is null.IllegalArgumentException - If the string argument is an empty string or is made of
whitespace characters only.public static void checkNullOrBlank(String value, String name)
value - Value of the string argument to check.name - Name of the string argument.IllegalArgumentException - If the value argument is not null or an empty string.public static void checkMaxLength(String value, int maxLen, String name)
value - Value of the string argument to check.maxLen - Maximum length of the string argument.name - Name of the string argument.IllegalArgumentException - Exception to notify of max length being exceeded.public static boolean isNullOrEmpty(Collection<?> collection)
collection - collection to check.public static boolean isNullOrBlank(String value)
value - string value to check.Copyright © 2021. All rights reserved.