Class Validators

java.lang.Object
com.tnsai.utils.Validators

public final class Validators extends Object
Validation utility methods for null and empty checks.

Provides two styles of validation:

  • Boolean checks - Return true/false (e.g., isNullOrEmpty())
  • Require methods - Throw IllegalArgumentException with 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 Details

    • isNullOrEmpty

      public static boolean isNullOrEmpty(String str)
      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

      public static boolean isNullOrBlank(String str)
      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

      public static String requireNonEmpty(String str, String message)
      Validates that a string is not null or empty.
      Parameters:
      str - The string to validate
      message - The exception message if validation fails
      Returns:
      The validated string (for chaining)
      Throws:
      IllegalArgumentException - if str is null or empty
    • requireNonBlank

      public static String requireNonBlank(String str, String message)
      Validates that a string is not null, empty, or whitespace-only.
      Parameters:
      str - The string to validate
      message - The exception message if validation fails
      Returns:
      The validated string (for chaining)
      Throws:
      IllegalArgumentException - if str is null, empty, or whitespace-only
    • isNullOrEmpty

      public static <T> boolean isNullOrEmpty(Collection<T> collection)
      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

      public static <T> Collection<T> requireNonEmpty(Collection<T> collection, String message)
      Validates that a collection is not null or empty.
      Type Parameters:
      T - The type of elements in the collection
      Parameters:
      collection - The collection to validate
      message - The exception message if validation fails
      Returns:
      The validated collection (for chaining)
      Throws:
      IllegalArgumentException - if collection is null or empty
    • isNullOrEmpty

      public static <K, V> boolean isNullOrEmpty(Map<K,V> map)
      Checks if a map is null or empty.
      Type Parameters:
      K - The type of keys in the map
      V - 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

      public static <K, V> Map<K,V> requireNonEmpty(Map<K,V> map, String message)
      Validates that a map is not null or empty.
      Type Parameters:
      K - The type of keys in the map
      V - The type of values in the map
      Parameters:
      map - The map to validate
      message - The exception message if validation fails
      Returns:
      The validated map (for chaining)
      Throws:
      IllegalArgumentException - if map is null or empty
    • requireNonNull

      public static <T> T requireNonNull(T obj, String message)
      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 validate
      message - The exception message if validation fails
      Returns:
      The validated object (for chaining)
      Throws:
      IllegalArgumentException - if obj is null