Class Preconditions


  • public final class Preconditions
    extends Object
    Preconditions which can be used to validate the argument passed to methods.
    • Method Detail

      • notNull

        public static <T> T notNull​(T object)
        Checks if the given object is null. If this is the case, a NullPointerException is thrown.
        Type Parameters:
        T - the type of the object. This is needed to return the correct type again
        Parameters:
        object - the object to be checked
        Returns:
        the object which was passed if it is not null
        Throws:
        NullPointerException - if the passed object is null
      • notNull

        public static <T> T notNull​(T object,
                                    String message)
        Checks if the given object if null. If this is the case, a NullPointerException with the given message is thrown.
        Type Parameters:
        T - the type of the object. This is needed to returned the correct type again
        Parameters:
        object - the object to be checked
        message - the message for the exception if the object is null
        Returns:
        the object which was passed if it is not null
        Throws:
        NullPointerException - if the passed object is null
      • notNull

        public static <T> T notNull​(T object,
                                    Supplier<String> messageSupplier)
        Checks if the given object is null. If this is the case, a NullPointerException with the message provided by the Supplier is thrown.
        Type Parameters:
        T - the type of the object. This is needed to returned the correct * type again
        Parameters:
        object - the object to be checked
        messageSupplier - a supplier for the message of the exception which will be thrown if object is null
        Returns:
        the object which was passed if it is not null
        Throws:
        NullPointerException - if the passed object is null
      • doesNotContainNull

        public static <T> Collection<T> doesNotContainNull​(Collection<T> collection)
        Checks if the given Collection contains any null element. If this is the case, a IllegalArgumentException is thrown.
        Type Parameters:
        T - the type of the collection
        Parameters:
        collection - to be examined for a null element. Must not be null
        Returns:
        the same collection which was initially given to this method
        Throws:
        IllegalArgumentException - if collection contains null
        NullPointerException - if the collection it was null itself. Note that some Collection implementations may throw a NullPointerException during the check if they do not permit the null type
      • doesNotContainNull

        public static <T> Collection<T> doesNotContainNull​(Collection<T> collection,
                                                           String message)
        Checks if the given Collection contains any null element. If this is the case, a IllegalArgumentException with the given message is thrown.
        Type Parameters:
        T - the type of the collection
        Parameters:
        collection - to be examined for a null element. Must not be null
        message - for the exception if it is thrown
        Returns:
        the same collection which was initially given to this method
        Throws:
        IllegalArgumentException - if collection contains null
        NullPointerException - if the collection it was null itself. Note that some Collection implementations may throw a NullPointerException during the check if they do not permit the null type
      • doesNotContainNull

        public static <T> Collection<T> doesNotContainNull​(Collection<T> collection,
                                                           Supplier<String> messageSupplier)
        Checks if the given Collection contains any null element. If this is the case, a IllegalArgumentException with the message given by the supplier.
        Type Parameters:
        T - the type of the collection
        Parameters:
        collection - to be examined for a null element. Must not be null
        messageSupplier - for the exception message if it is thrown
        Returns:
        the same collection which was initially given to this method
        Throws:
        IllegalArgumentException - if collection contains null
        NullPointerException - if the collection it was null itself. Note that some Collection implementations may throw a NullPointerException during the check if they do not permit the null type
      • check

        public static void check​(boolean expression)
        Checks whether the given expression evaluates to true. If this is not the case, an IllegalArgumentException is thrown.
        Parameters:
        expression - the expression to be evaluated
        Throws:
        IllegalArgumentException - if the expression evaluates to false
      • check

        public static void check​(boolean expression,
                                 String message)
        Checks whether the given expression evaluates to true. If this is not the case, an IllegalArgumentException with the given message is thrown.
        Parameters:
        expression - the expression to be evaluated
        message - the message for the exception
        Throws:
        IllegalArgumentException - if the expression evaluates to false
      • check

        public static void check​(boolean expression,
                                 Supplier<String> messageSupplier)
        Checks whether the given expression evaluates to true. If this is not the case, an IllegalArgumentException with the message given by the Supplier is thrown.
        Parameters:
        expression - the expression to be evaluated
        messageSupplier - the supplier for the exception message
        Throws:
        IllegalArgumentException - if the expression evaluates to false