Class Assert


  • public abstract class Assert
    extends java.lang.Object
    The utility class for Assertion
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Constructor Description
      Assert()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertArrayIndex​(java.lang.Object array, int index)
      Assert the index is valid for the given array.
      static void assertArrayType​(java.lang.Object array)
      Assert the provided object is an array.
      static void assertFieldMatchType​(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> expectedType)
      Assert that the field with the specified name in the given object's class matches the expected type.
      static void assertNoNullElements​(java.lang.Iterable<?> elements, java.lang.String message)
      Assert that a elements contains no null elements.
      static void assertNoNullElements​(java.lang.Iterable<?> elements, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that a elements contains no null elements.
      static void assertNoNullElements​(java.lang.Object[] array, java.lang.String message)
      Assert that an array contains no null elements.
      static void assertNoNullElements​(java.lang.Object[] array, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that an array contains no null elements.
      static void assertNotBlank​(java.lang.String text, java.lang.String message)
      Assert that a string is not blank.
      static void assertNotBlank​(java.lang.String text, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that a string is not blank.
      static void assertNotEmpty​(java.lang.Object[] array, java.lang.String message)
      Assert that an array contains elements; that is, it must not be null and must contain at least one element.
      static void assertNotEmpty​(java.lang.Object[] array, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that an array contains elements; that is, it must not be null and must contain at least one element.
      static void assertNotEmpty​(java.lang.String text, java.lang.String message)
      Assert that a string is not empty ("").
      static void assertNotEmpty​(java.lang.String text, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that a string is not empty ("").
      static void assertNotEmpty​(java.util.Collection<?> collection, java.lang.String message)
      Assert that a collection contains elements; that is, it must not be null and must contain at least one element.
      static void assertNotEmpty​(java.util.Collection<?> collection, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that a collection contains elements; that is, it must not be null and must contain at least one element.
      static void assertNotEmpty​(java.util.Map<?,​?> map, java.lang.String message)
      Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.
      static void assertNotEmpty​(java.util.Map<?,​?> map, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.
      static void assertNotNull​(java.lang.Object object, java.lang.String message)
      Assert that an object is not null.
      static void assertNotNull​(java.lang.Object object, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that an object is not null.
      static void assertNull​(java.lang.Object object, java.lang.String message)
      Assert that an object is null.
      static void assertNull​(java.lang.Object object, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert that an object is null.
      static void assertTrue​(boolean expression, java.lang.String message)
      Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.
      static void assertTrue​(boolean expression, java.util.function.Supplier<java.lang.String> messageSupplier)
      Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Assert

        public Assert()
    • Method Detail

      • assertTrue

        public static void assertTrue​(boolean expression,
                                      java.lang.String message)
        Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.

        Example Usage

        
             assertTrue(i > 0, "The value must be greater than zero");
         
        Parameters:
        expression - a boolean expression
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if expression is false
      • assertTrue

        public static void assertTrue​(boolean expression,
                                      java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.

        Example Usage

        
         assertTrue(i > 0, () -> "The value '" + i + "' must be greater than zero");
         
        Parameters:
        expression - a boolean expression
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if expression is false
      • assertNull

        public static void assertNull​(@Nullable
                                      java.lang.Object object,
                                      java.lang.String message)
        Assert that an object is null.

        Example Usage

        
             assertNull(value, "The value must be null");
         
        Parameters:
        object - the object to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object is not null
      • assertNull

        public static void assertNull​(@Nullable
                                      java.lang.Object object,
                                      java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that an object is null.

        Example Usage

        
         assertNull(value, () -> "The value '" + value + "' must be null");
         
        Parameters:
        object - the object to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object is not null
      • assertNotNull

        public static void assertNotNull​(@Nullable
                                         java.lang.Object object,
                                         java.lang.String message)
        Assert that an object is not null.

        Example Usage

        
             assertNotNull(clazz, "The class must not be null");
         
        Parameters:
        object - the object to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object is null
      • assertNotNull

        public static void assertNotNull​(@Nullable
                                         java.lang.Object object,
                                         java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that an object is not null.

        Example Usage

        
         assertNotNull(entity.getId(), () -> "ID for entity " + entity.getName() + " must not be null");
         
        Parameters:
        object - the object to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object is null
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.lang.String text,
                                          java.lang.String message)
        Assert that a string is not empty ("").

        Example Usage

        
         assertNotEmpty(entity.getName(), "Name for entity " + entity.getName() + " must not be empty");
         
        Parameters:
        text - the text to check
        message - the exception message to use if the assertion fails
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.lang.String text,
                                          java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that a string is not empty ("").

        Example Usage

        
         assertNotEmpty(entity.getName(), () -> "Name for entity " + entity.getName() + " must not be empty");
         
        Parameters:
        text - the text to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
      • assertNotBlank

        public static void assertNotBlank​(@Nullable
                                          java.lang.String text,
                                          java.lang.String message)
        Assert that a string is not blank.

        Example Usage

        
         assertNotBlank(entity.getName(), "Name for entity " + entity.getName() + " must not be blank");
         
        Parameters:
        text - the text to check
        message - the exception message to use if the assertion fails
      • assertNotBlank

        public static void assertNotBlank​(@Nullable
                                          java.lang.String text,
                                          java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that a string is not blank.

        Example Usage

        
         assertNotBlank(entity.getName(), () -> "Name for entity " + entity.getName() + " must not be blank");
         
        Parameters:
        text - the text to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.lang.Object[] array,
                                          java.lang.String message)
        Assert that an array contains elements; that is, it must not be null and must contain at least one element.

        Example Usage

        
             assertNotEmpty(array, "The array must contain elements");
         
        Parameters:
        array - the array to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object array is null or contains no elements
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.lang.Object[] array,
                                          java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that an array contains elements; that is, it must not be null and must contain at least one element.

        Example Usage

        
         assertNotEmpty(array, () -> "The " + arrayType + " array must contain elements");
         
        Parameters:
        array - the array to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object array is null or contains no elements
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.util.Collection<?> collection,
                                          java.lang.String message)
        Assert that a collection contains elements; that is, it must not be null and must contain at least one element.

        Example Usage

        
             assertNotEmpty(collection, "Collection must contain elements");
         
        Parameters:
        collection - the collection to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the collection is null or contains no elements
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.util.Collection<?> collection,
                                          java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that a collection contains elements; that is, it must not be null and must contain at least one element.

        Example Usage

        
         assertNotEmpty(collection, () -> "The " + collectionType + " collection must contain elements");
         
        Parameters:
        collection - the collection to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the collection is null or contains no elements
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.util.Map<?,​?> map,
                                          java.lang.String message)
        Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.

        Example Usage

        
             assertNotEmpty(map, "Map must contain entries");
         
        Parameters:
        map - the map to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the map is null or contains no entries
      • assertNotEmpty

        public static void assertNotEmpty​(@Nullable
                                          java.util.Map<?,​?> map,
                                          java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.

        Example Usage

        
         assertNotEmpty(map, () -> "The " + mapType + " map must contain entries");
         
        Parameters:
        map - the map to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the map is null or contains no entries
      • assertNoNullElements

        public static void assertNoNullElements​(@Nullable
                                                java.lang.Object[] array,
                                                java.lang.String message)
        Assert that an array contains no null elements.

        Note: Does not complain if the array is empty!

        Example Usage

        
             assertNoNullElements(array, "The array must contain non-null elements");
         
        Parameters:
        array - the array to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object array contains a null element
      • assertNoNullElements

        public static void assertNoNullElements​(@Nullable
                                                java.lang.Object[] array,
                                                java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that an array contains no null elements.

        Note: Does not complain if the array is empty!

        Example Usage

        
         assertNoNullElements(array, () -> "The " + arrayType + " array must contain non-null elements");
         
        Parameters:
        array - the array to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the object array contains a null element
      • assertNoNullElements

        public static void assertNoNullElements​(@Nullable
                                                java.lang.Iterable<?> elements,
                                                java.lang.String message)
        Assert that a elements contains no null elements.

        Note: Does not complain if the elements is empty!

        Example Usage

        
             assertNoNullElements(elements, "Elements must contain non-null elements");
         
        Parameters:
        elements - the elements to check
        message - the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the elements contains a null element
      • assertNoNullElements

        public static void assertNoNullElements​(@Nullable
                                                java.lang.Iterable<?> elements,
                                                java.util.function.Supplier<java.lang.String> messageSupplier)
        Assert that a elements contains no null elements.

        Note: Does not complain if the elements is empty!

        Example Usage

        
         assertNoNullElements(elements, () -> "Collection " + collectionName + " must contain non-null elements");
         
        Parameters:
        elements - the elements to check
        messageSupplier - a supplier for the exception message to use if the assertion fails
        Throws:
        java.lang.IllegalArgumentException - if the elements contains a null element
      • assertArrayIndex

        public static void assertArrayIndex​(java.lang.Object array,
                                            int index)
                                     throws java.lang.IllegalArgumentException
        Assert the index is valid for the given array.

        Example Usage

        
         assertArrayIndex(array, index); // throws IllegalArgumentException if index is invalid
         
        Parameters:
        array - Object array to check
        index - Index value to validate
        Throws:
        java.lang.IllegalArgumentException - if the object is not an array
        java.lang.ArrayIndexOutOfBoundsException - if the index is negative or exceeds the array length
      • assertArrayType

        public static void assertArrayType​(java.lang.Object array)
                                    throws java.lang.IllegalArgumentException
        Assert the provided object is an array.

        Example Usage

        
         assertArrayType(new String[]{"one", "two"}); // passes
         assertArrayType("notAnArray");              // throws IllegalArgumentException
         
        Parameters:
        array - the object to check
        Throws:
        java.lang.IllegalArgumentException - if the object is not an array
      • assertFieldMatchType

        public static void assertFieldMatchType​(java.lang.Object object,
                                                java.lang.String fieldName,
                                                java.lang.Class<?> expectedType)
                                         throws java.lang.NullPointerException,
                                                java.lang.IllegalArgumentException
        Assert that the field with the specified name in the given object's class matches the expected type.

        Example Usage

        
         class Example {
             private String name;
         }
        
         Example example = new Example();
         assertFieldMatchType(example, "name", String.class);  // passes
         assertFieldMatchType(example, "name", Integer.class); // throws IllegalArgumentException
         
        Parameters:
        object - the object whose class is to be checked
        fieldName - the name of the field
        expectedType - the expected type of the field
        Throws:
        java.lang.NullPointerException - if the field cannot be found in the object's class
        java.lang.IllegalArgumentException - if the field's type does not match the expected type