Class PreConditions


  • public final class PreConditions
    extends Object
    Commons static pre-conditions utilities.
    • Method Detail

      • notNull

        public static <T> T notNull​(T object,
                                    String message)
        Check that a given object is not null. If object is null, a NullPointerException is thrown with given message. If object is not null, it is automatically returns.
        Type Parameters:
        T - Object Generic Type.
        Parameters:
        object - Object to check.
        message - Exception message.
        Returns:
        Non null object.
        Throws:
        NullPointerException - if object is null.
      • notEmpty

        public static <T> T[] notEmpty​(T[] array,
                                       String message)
        Check if an array is not null and not empty. If array is null or empty, an IllegalArgumentException is thrown. If array is not null and not empty, it will be automatically returned.
        Type Parameters:
        T - Type of array elements.
        Parameters:
        array - Array to check.
        message - Exception message.
        Returns:
        Original array.
      • notEmpty

        public static <T> Collection<T> notEmpty​(Collection<T> array,
                                                 String message)
        Check if a collection is not null and not empty. If collection is null or empty, an IllegalArgumentException is thrown. If collection is not null and not empty, it will be automatically returned.
        Type Parameters:
        T - Type of array elements.
        Parameters:
        array - Collection to check.
        message - Exception message.
        Returns:
        Original collection.
      • hasText

        public static String hasText​(String value,
                                     String message)
        Check that a given string value is not blank (i.e. not null, not empty and does not contain only whitespaces). If value is blank, an IllegalArgumentException is thrown. If value is not blank, original string value is automatically returned.
        Parameters:
        value - String value.
        message - Error message given to exception.
        Returns:
        Original string value.
        Throws:
        IllegalArgumentException - if value is blank.