Package com.tnsai.utils
Class Validators
java.lang.Object
com.tnsai.utils.Validators
Validation utility methods for null and empty checks.
Provides two styles of validation:
- Boolean checks - Return true/false (e.g.,
isNullOrEmpty()) - Require methods - Throw
IllegalArgumentExceptionwith custom message
Eliminates duplicate validation code across the framework by centralizing
common patterns like if (x == null || x.isEmpty()).
Usage Examples:
// Boolean checks
if (Validators.isNullOrEmpty(topic)) {
logger.warn("Topic is missing");
return;
}
// Throwing validation
Validators.requireNonEmpty(endpoint, "Endpoint cannot be null or empty");
// Whitespace-aware validation
Validators.requireNonBlank(name, "Name cannot be blank");
Thread-safe: All methods are static and stateless.
- Since:
- Phase 3, Issue #4
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisNullOrBlank(String str) Checks if a string is null, empty, or contains only whitespace.static booleanisNullOrEmpty(String str) Checks if a string is null or empty.static <T> booleanisNullOrEmpty(Collection<T> collection) Checks if a collection is null or empty.static <K,V> boolean isNullOrEmpty(Map<K, V> map) Checks if a map is null or empty.static StringrequireNonBlank(String str, String message) Validates that a string is not null, empty, or whitespace-only.static StringrequireNonEmpty(String str, String message) Validates that a string is not null or empty.static <T> Collection<T> requireNonEmpty(Collection<T> collection, String message) Validates that a collection is not null or empty.static <K,V> Map <K, V> requireNonEmpty(Map<K, V> map, String message) Validates that a map is not null or empty.static <T> TrequireNonNull(T obj, String message) Validates that an object is not null.
-
Method Details
-
isNullOrEmpty
Checks if a string is null or empty.- Parameters:
str- The string to check- Returns:
- true if the string is null or empty (""), false otherwise
-
isNullOrBlank
Checks if a string is null, empty, or contains only whitespace.Uses
String.trim()to detect whitespace-only strings.- Parameters:
str- The string to check- Returns:
- true if the string is null, empty, or whitespace-only
-
requireNonEmpty
Validates that a string is not null or empty.- Parameters:
str- The string to validatemessage- The exception message if validation fails- Returns:
- The validated string (for chaining)
- Throws:
IllegalArgumentException- if str is null or empty
-
requireNonBlank
Validates that a string is not null, empty, or whitespace-only.- Parameters:
str- The string to validatemessage- The exception message if validation fails- Returns:
- The validated string (for chaining)
- Throws:
IllegalArgumentException- if str is null, empty, or whitespace-only
-
isNullOrEmpty
Checks if a collection is null or empty.- Type Parameters:
T- The type of elements in the collection- Parameters:
collection- The collection to check- Returns:
- true if the collection is null or has no elements, false otherwise
-
requireNonEmpty
Validates that a collection is not null or empty.- Type Parameters:
T- The type of elements in the collection- Parameters:
collection- The collection to validatemessage- The exception message if validation fails- Returns:
- The validated collection (for chaining)
- Throws:
IllegalArgumentException- if collection is null or empty
-
isNullOrEmpty
Checks if a map is null or empty.- Type Parameters:
K- The type of keys in the mapV- The type of values in the map- Parameters:
map- The map to check- Returns:
- true if the map is null or has no entries, false otherwise
-
requireNonEmpty
Validates that a map is not null or empty.- Type Parameters:
K- The type of keys in the mapV- The type of values in the map- Parameters:
map- The map to validatemessage- The exception message if validation fails- Returns:
- The validated map (for chaining)
- Throws:
IllegalArgumentException- if map is null or empty
-
requireNonNull
Validates that an object is not null.Similar to
Objects.requireNonNull(Object, String)but returns the object for chaining.- Type Parameters:
T- The type of the object- Parameters:
obj- The object to validatemessage- The exception message if validation fails- Returns:
- The validated object (for chaining)
- Throws:
IllegalArgumentException- if obj is null
-