Class Preconditions


  • public final class Preconditions
    extends java.lang.Object
    Preconditions which can be used to validate the argument passed to methods. This functionality is already implemented with guava, but importing the complete guava library for just the preconditions seems like a bit of an overkill.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void check​(boolean expression)
      Checks whether the given expression evaluates to true.
      static void check​(boolean expression, java.lang.String message)
      Checks whether the given expression evaluates to true.
      static void check​(boolean expression, java.util.function.Supplier<java.lang.String> messageSupplier)
      Checks whether the given expression evaluates to true.
      static <T> java.util.Collection<T> doesNotContainNull​(java.util.Collection<T> collection)
      Checks if the given Collection contains any null element.
      static <T> java.util.Collection<T> doesNotContainNull​(java.util.Collection<T> collection, java.lang.String message)
      Checks if the given Collection contains any null element.
      static <T> java.util.Collection<T> doesNotContainNull​(java.util.Collection<T> collection, java.util.function.Supplier<java.lang.String> messageSupplier)
      Checks if the given Collection contains any null element.
      static <T> T notNull​(T object)
      Checks if the given object is null.
      static <T> T notNull​(T object, java.lang.String message)
      Checks if the given object if null.
      static <T> T notNull​(T object, java.util.function.Supplier<java.lang.String> messageSupplier)
      Checks if the given object is null.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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:
        java.lang.NullPointerException - if the passed object is null
      • notNull

        public static <T> T notNull​(T object,
                                    java.lang.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:
        java.lang.NullPointerException - if the passed object is null
      • notNull

        public static <T> T notNull​(T object,
                                    java.util.function.Supplier<java.lang.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:
        java.lang.NullPointerException - if the passed object is null
      • doesNotContainNull

        public static <T> java.util.Collection<T> doesNotContainNull​(java.util.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 initally given to this method
        Throws:
        java.lang.IllegalArgumentException - if collection contains null
        java.lang.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> java.util.Collection<T> doesNotContainNull​(java.util.Collection<T> collection,
                                                                     java.lang.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 initally given to this method
        Throws:
        java.lang.IllegalArgumentException - if collection contains null
        java.lang.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> java.util.Collection<T> doesNotContainNull​(java.util.Collection<T> collection,
                                                                     java.util.function.Supplier<java.lang.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 initally given to this method
        Throws:
        java.lang.IllegalArgumentException - if collection contains null
        java.lang.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:
        java.lang.IllegalArgumentException - if the expression evaluates to false
      • check

        public static void check​(boolean expression,
                                 java.lang.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:
        java.lang.IllegalArgumentException - if the expression evaluates to false
      • check

        public static void check​(boolean expression,
                                 java.util.function.Supplier<java.lang.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:
        java.lang.IllegalArgumentException - if the expression evaluates to false