Class Assert


  • @ThreadSafe
    public class Assert
    extends java.lang.Object
    Basic assertions. The static methods in this class provide a convenient way to test method arguments. All of the methods in this class throw IllegalArgumentException if the method arguments are invalid.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void isAssignableTo​(java.lang.String argumentName, java.lang.Object argumentObject, java.lang.Class<?> targetClass)
      Tests if an argument is not null and can be cast to a specified class.
      static void isInstanceOf​(java.lang.String argumentName, java.lang.Object argumentObject, java.lang.Class<?> targetClass)
      Tests if an argument is not null and is an instance of a specified class.
      static void isInstanceOf​(java.lang.String argumentName, java.lang.Object argumentObject, java.lang.Class<?>... targetClasses)
      Tests if an argument is not null and is an instance of one of the specified classes.
      static void isNotInstanceOf​(java.lang.String argumentName, java.lang.Object argumentObject, java.lang.Class<?> targetClass)
      Tests if an argument is not null and is not an instance of a specified class.
      static void isNotInstanceOf​(java.lang.String argumentName, java.lang.Object argumentObject, java.lang.Class<?>... targetClasses)
      Tests if an argument is not null and is not an instance of one of the specified classes.
      static void notEmpty​(java.lang.String argumentName, java.lang.String argumentObject)
      Tests if an argument is not null and is not empty.
      static <T extends java.util.Map<?,​?>>
      void
      notEmpty​(java.lang.String argumentName, T argumentObject)
      Tests if an argument is not null and is not empty.
      static <T> void notEmpty​(java.lang.String argumentName, T[] argumentObject)
      Tests if an argument is not null and is not empty.
      static void notNull​(java.lang.Object... arguments)
      Tests a list of arguments for null.
      static void notNull​(java.lang.String argumentName, java.lang.Object objectToTest)
      Tests an argument for null.
      • Methods inherited from class java.lang.Object

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

      • isAssignableTo

        public static void isAssignableTo​(java.lang.String argumentName,
                                          java.lang.Object argumentObject,
                                          java.lang.Class<?> targetClass)
        Tests if an argument is not null and can be cast to a specified class.

        Assert.isAssignableTo("foo", foo, Foo.class);

        Parameters:
        argumentName -
        argumentObject -
        targetClass -
        Throws:
        java.lang.IllegalArgumentException
      • isInstanceOf

        public static void isInstanceOf​(java.lang.String argumentName,
                                        java.lang.Object argumentObject,
                                        java.lang.Class<?> targetClass)
        Tests if an argument is not null and is an instance of a specified class.

        Assert.isInstanceOf("foo", foo, Foo.class);

        Parameters:
        argumentName -
        argumentObject -
        targetClass -
        Throws:
        java.lang.IllegalArgumentException
      • isInstanceOf

        public static void isInstanceOf​(java.lang.String argumentName,
                                        java.lang.Object argumentObject,
                                        java.lang.Class<?>... targetClasses)
        Tests if an argument is not null and is an instance of one of the specified classes.

        Assert.isInstanceOf("foo", foo, Foo.class, Bar.class, ...);

        Parameters:
        argumentName -
        argumentObject -
        targetClasses -
        Throws:
        java.lang.IllegalArgumentException
      • isNotInstanceOf

        public static void isNotInstanceOf​(java.lang.String argumentName,
                                           java.lang.Object argumentObject,
                                           java.lang.Class<?> targetClass)
        Tests if an argument is not null and is not an instance of a specified class.

        Assert.isNotInstanceOf("foo", foo, Foo.class);

        Parameters:
        argumentName -
        argumentObject -
        targetClass -
        Throws:
        java.lang.IllegalArgumentException
      • isNotInstanceOf

        public static void isNotInstanceOf​(java.lang.String argumentName,
                                           java.lang.Object argumentObject,
                                           java.lang.Class<?>... targetClasses)
        Tests if an argument is not null and is not an instance of one of the specified classes.

        Assert.isNotInstanceOf("foo", foo, Foo.class, Bar.class, ...);

        Parameters:
        argumentName -
        argumentObject -
        targetClasses -
        Throws:
        java.lang.IllegalArgumentException
      • notEmpty

        public static void notEmpty​(java.lang.String argumentName,
                                    java.lang.String argumentObject)
        Tests if an argument is not null and is not empty.

        Assert.notEmpty("foo", foo);

        Parameters:
        argumentName -
        argumentObject -
        Throws:
        java.lang.IllegalArgumentException
      • notEmpty

        public static <T extends java.util.Map<?,​?>> void notEmpty​(java.lang.String argumentName,
                                                                         T argumentObject)
        Tests if an argument is not null and is not empty.

        Assert.notEmpty("foo", foo);

        Parameters:
        argumentName -
        argumentObject -
        Throws:
        java.lang.IllegalArgumentException
      • notEmpty

        public static <T extends java.util.Collection<?>> void notEmpty​(java.lang.String argumentName,
                                                                        T argumentObject)
        Tests if an argument is not null and is not empty.

        Assert.notEmpty("foo", foo);

        Parameters:
        argumentName -
        argumentObject -
        Throws:
        java.lang.IllegalArgumentException
      • notEmpty

        public static <T> void notEmpty​(java.lang.String argumentName,
                                        T[] argumentObject)
        Tests if an argument is not null and is not empty.

        Assert.notEmpty("foo", foo);

        Parameters:
        argumentName -
        argumentObject -
        Throws:
        java.lang.IllegalArgumentException
      • notNull

        public static void notNull​(java.lang.Object... arguments)
        Tests a list of arguments for null.

        Assert.notNull("foo", foo, "bar", bar, ...);

        Parameters:
        arguments -
        Throws:
        java.lang.IllegalArgumentException
      • notNull

        public static void notNull​(java.lang.String argumentName,
                                   java.lang.Object objectToTest)
        Tests an argument for null.

        Assert.notNull("foo", foo);

        Parameters:
        argumentName -
        objectToTest -
        Throws:
        java.lang.IllegalArgumentException