Class Preconditions



  • @API(status=INTERNAL,
         since="1.0")
    public final class Preconditions
    extends java.lang.Object
    Collection of utilities for asserting preconditions for method and constructor arguments.

    Each method in this class throws a PreconditionViolationException if the precondition is violated.

    DISCLAIMER

    These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!

    Since:
    1.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void condition​(boolean predicate, java.lang.String message)
      Assert that the supplied predicate is true.
      static void condition​(boolean predicate, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied predicate is true.
      static <T> T[] containsNoNullElements​(T[] array, java.lang.String message)
      Assert that the supplied array contains no null elements.
      static <T> T[] containsNoNullElements​(T[] array, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied array contains no null elements.
      static <T extends java.util.Collection<?>>
      T
      containsNoNullElements​(T collection, java.lang.String message)
      Assert that the supplied collection contains no null elements.
      static <T extends java.util.Collection<?>>
      T
      containsNoNullElements​(T collection, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied collection contains no null elements.
      static java.lang.String notBlank​(java.lang.String str, java.lang.String message)
      Assert that the supplied String is not blank.
      static java.lang.String notBlank​(java.lang.String str, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied String is not blank.
      static <T> T[] notEmpty​(T[] array, java.lang.String message)
      Assert that the supplied array is neither null nor empty.
      static <T> T[] notEmpty​(T[] array, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied array is neither null nor empty.
      static <T extends java.util.Collection<?>>
      T
      notEmpty​(T collection, java.lang.String message)
      Assert that the supplied Collection is neither null nor empty.
      static <T extends java.util.Collection<?>>
      T
      notEmpty​(T collection, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied Collection is neither null nor empty.
      static <T> T notNull​(T object, java.lang.String message)
      Assert that the supplied Object is not null.
      static <T> T notNull​(T object, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that the supplied Object is not 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,
                                    java.util.function.Supplier<java.lang.String> messageSupplier)
                             throws PreconditionViolationException
        Assert that the supplied Object is not null.
        Parameters:
        object - the object to check
        messageSupplier - precondition violation message supplier
        Returns:
        the supplied object as a convenience
        Throws:
        PreconditionViolationException - if the supplied object is null
        See Also:
        condition(boolean, Supplier)
      • notEmpty

        public static <T extends java.util.Collection<?>> T notEmpty​(T collection,
                                                                     java.util.function.Supplier<java.lang.String> messageSupplier)
                                                              throws PreconditionViolationException
        Assert that the supplied Collection is neither null nor empty.

        WARNING: this method does NOT check if the supplied collection contains any null elements.

        Parameters:
        collection - the collection to check
        messageSupplier - precondition violation message supplier
        Returns:
        the supplied collection as a convenience
        Throws:
        PreconditionViolationException - if the supplied collection is null or empty
        See Also:
        containsNoNullElements(Collection, String), condition(boolean, String)
      • containsNoNullElements

        public static <T> T[] containsNoNullElements​(T[] array,
                                                     java.lang.String message)
                                              throws PreconditionViolationException
        Assert that the supplied array contains no null elements.

        WARNING: this method does NOT check if the supplied array is null or empty.

        Parameters:
        array - the array to check
        message - precondition violation message
        Returns:
        the supplied array as a convenience
        Throws:
        PreconditionViolationException - if the supplied array contains any null elements
        See Also:
        notNull(Object, String)
      • containsNoNullElements

        public static <T> T[] containsNoNullElements​(T[] array,
                                                     java.util.function.Supplier<java.lang.String> messageSupplier)
                                              throws PreconditionViolationException
        Assert that the supplied array contains no null elements.

        WARNING: this method does NOT check if the supplied array is null or empty.

        Parameters:
        array - the array to check
        messageSupplier - precondition violation message supplier
        Returns:
        the supplied array as a convenience
        Throws:
        PreconditionViolationException - if the supplied array contains any null elements
        See Also:
        notNull(Object, String)
      • containsNoNullElements

        public static <T extends java.util.Collection<?>> T containsNoNullElements​(T collection,
                                                                                   java.lang.String message)
                                                                            throws PreconditionViolationException
        Assert that the supplied collection contains no null elements.

        WARNING: this method does NOT check if the supplied collection is null or empty.

        Parameters:
        collection - the collection to check
        message - precondition violation message
        Returns:
        the supplied collection as a convenience
        Throws:
        PreconditionViolationException - if the supplied collection contains any null elements
        See Also:
        notNull(Object, String)
      • containsNoNullElements

        public static <T extends java.util.Collection<?>> T containsNoNullElements​(T collection,
                                                                                   java.util.function.Supplier<java.lang.String> messageSupplier)
                                                                            throws PreconditionViolationException
        Assert that the supplied collection contains no null elements.

        WARNING: this method does NOT check if the supplied collection is null or empty.

        Parameters:
        collection - the collection to check
        messageSupplier - precondition violation message supplier
        Returns:
        the supplied collection as a convenience
        Throws:
        PreconditionViolationException - if the supplied collection contains any null elements
        See Also:
        notNull(Object, String)
      • notBlank

        public static java.lang.String notBlank​(java.lang.String str,
                                                java.lang.String message)
                                         throws PreconditionViolationException
        Assert that the supplied String is not blank.

        A String is blank if it is null or consists only of whitespace characters.

        Parameters:
        str - the string to check
        message - precondition violation message
        Returns:
        the supplied string as a convenience
        Throws:
        PreconditionViolationException - if the supplied string is blank
        See Also:
        notBlank(String, Supplier)
      • condition

        public static void condition​(boolean predicate,
                                     java.util.function.Supplier<java.lang.String> messageSupplier)
                              throws PreconditionViolationException
        Assert that the supplied predicate is true.
        Parameters:
        predicate - the predicate to check
        messageSupplier - precondition violation message supplier
        Throws:
        PreconditionViolationException - if the predicate is false