Class Assert


  • public final class Assert
    extends java.lang.Object
    A set of assertions and checks.
    Author:
    David M. Lloyd
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean assertFalse​(boolean expr)
      Assert that the given expression is always false.
      static <T> T assertHoldsLock​(T monitor)
      Assert that the given monitor is held by the current thread.
      static <T> T assertNotHoldsLock​(T monitor)
      Assert that the given monitor is not held by the current thread.
      static <T> T assertNotNull​(T value)
      Assert that the value is not null.
      static boolean assertTrue​(boolean expr)
      Assert that the given expression is always true.
      static void checkArrayBounds​(byte[] array, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array.
      static void checkArrayBounds​(char[] array, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array.
      static void checkArrayBounds​(int[] array, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array.
      static void checkArrayBounds​(int arrayLength, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array length.
      static void checkArrayBounds​(long[] array, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array.
      static void checkArrayBounds​(java.lang.Object[] array, int offs, int len)
      Check that the given offset and length fall completely within the bounds of the given array.
      static void checkMaximumParameter​(java.lang.String name, double max, double actual)
      Check that the named parameter is less than or equal to max.
      static void checkMaximumParameter​(java.lang.String name, float max, float actual)
      Check that the named parameter is less than or equal to max.
      static void checkMaximumParameter​(java.lang.String name, int max, int actual)
      Check that the named parameter is less than or equal to max.
      static void checkMaximumParameter​(java.lang.String name, long max, long actual)
      Check that the named parameter is less than or equal to max.
      static void checkMinimumParameter​(java.lang.String name, double min, double actual)
      Check that the named parameter is greater than or equal to min.
      static void checkMinimumParameter​(java.lang.String name, float min, float actual)
      Check that the named parameter is greater than or equal to min.
      static void checkMinimumParameter​(java.lang.String name, int min, int actual)
      Check that the named parameter is greater than or equal to min.
      static void checkMinimumParameter​(java.lang.String name, long min, long actual)
      Check that the named parameter is greater than or equal to min.
      static boolean[] checkNotEmptyParam​(java.lang.String name, boolean[] value)
      Check that the named parameter is not empty.
      static byte[] checkNotEmptyParam​(java.lang.String name, byte[] value)
      Check that the named parameter is not empty.
      static double[] checkNotEmptyParam​(java.lang.String name, double[] value)
      Check that the named parameter is not empty.
      static float[] checkNotEmptyParam​(java.lang.String name, float[] value)
      Check that the named parameter is not empty.
      static int[] checkNotEmptyParam​(java.lang.String name, int[] value)
      Check that the named parameter is not empty.
      static long[] checkNotEmptyParam​(java.lang.String name, long[] value)
      Check that the named parameter is not empty.
      static short[] checkNotEmptyParam​(java.lang.String name, short[] value)
      Check that the named parameter is not empty.
      static java.lang.CharSequence checkNotEmptyParam​(java.lang.String name, java.lang.CharSequence value)
      Check that the named parameter is not empty.
      static java.lang.String checkNotEmptyParam​(java.lang.String name, java.lang.String value)
      Check that the named parameter is not empty.
      static <E,​T extends java.util.Collection<E>>
      T
      checkNotEmptyParam​(java.lang.String name, T value)
      Check that the named parameter is not empty.
      static <T> T[] checkNotEmptyParam​(java.lang.String name, T[] value)
      Check that the named parameter is not empty.
      static <T> T checkNotNullArrayParam​(java.lang.String name, int index, T value)
      Check that a value within the named array parameter is not null.
      static <T> T checkNotNullParam​(java.lang.String name, T value)
      Check that the named parameter is not null.
      static <T> T checkNotNullParamWithNullPointerException​(java.lang.String name, T value)
      Check that the named parameter is not null, using a NullPointerException as some specifications require.
      static java.lang.IllegalStateException impossibleSwitchCase​(int val)
      Return an exception indicating that the current switch case was intended to be unreachable.
      static java.lang.IllegalStateException impossibleSwitchCase​(long val)
      Return an exception indicating that the current switch case was intended to be unreachable.
      static java.lang.IllegalStateException impossibleSwitchCase​(java.lang.Object obj)
      Return an exception indicating that the current switch case was intended to be unreachable.
      static java.lang.IllegalStateException unreachableCode()
      Return an exception indicating that the current code was intended to be unreachable.
      static java.lang.UnsupportedOperationException unsupported()
      Return an exception explaining that the caller's method is not supported.
      • Methods inherited from class java.lang.Object

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

      • checkNotNullParam

        @NotNull
        public static <T> T checkNotNullParam​(java.lang.String name,
                                              T value)
                                       throws java.lang.IllegalArgumentException
        Check that the named parameter is not null. Use a standard exception message if it is.
        Type Parameters:
        T - the value type
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is null
      • checkNotNullParamWithNullPointerException

        @NotNull
        public static <T> T checkNotNullParamWithNullPointerException​(java.lang.String name,
                                                                      T value)
                                                               throws java.lang.NullPointerException
        Check that the named parameter is not null, using a NullPointerException as some specifications require. Use a standard exception message if it is.
        Type Parameters:
        T - the value type
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.NullPointerException - if the value is null
      • checkNotNullArrayParam

        @NotNull
        public static <T> T checkNotNullArrayParam​(java.lang.String name,
                                                   int index,
                                                   T value)
                                            throws java.lang.IllegalArgumentException
        Check that a value within the named array parameter is not null. Use a standard exception message if it is.
        Type Parameters:
        T - the element value type
        Parameters:
        name - the parameter name
        index - the array index
        value - the array element value
        Returns:
        the array element value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is null
      • checkNotEmptyParam

        @NotNull
        public static java.lang.String checkNotEmptyParam​(java.lang.String name,
                                                          java.lang.String value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static java.lang.CharSequence checkNotEmptyParam​(java.lang.String name,
                                                                java.lang.CharSequence value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static <E,​T extends java.util.Collection<E>> T checkNotEmptyParam​(java.lang.String name,
                                                                                       T value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static <K,​V,​T extends java.util.Map<K,​V>> T checkNotEmptyParam​(java.lang.String name,
                                                                                                T value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static <T> T[] checkNotEmptyParam​(java.lang.String name,
                                                 T[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static boolean[] checkNotEmptyParam​(java.lang.String name,
                                                   boolean[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static byte[] checkNotEmptyParam​(java.lang.String name,
                                                byte[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static short[] checkNotEmptyParam​(java.lang.String name,
                                                 short[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static int[] checkNotEmptyParam​(java.lang.String name,
                                               int[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static long[] checkNotEmptyParam​(java.lang.String name,
                                                long[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static float[] checkNotEmptyParam​(java.lang.String name,
                                                 float[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkNotEmptyParam

        @NotNull
        public static double[] checkNotEmptyParam​(java.lang.String name,
                                                  double[] value)
        Check that the named parameter is not empty. Use a standard exception message if it is.
        Parameters:
        name - the parameter name
        value - the parameter value
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the value is empty
      • checkMinimumParameter

        public static void checkMinimumParameter​(java.lang.String name,
                                                 int min,
                                                 int actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is greater than or equal to min.
        Parameters:
        name - the parameter name
        min - the minimum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is less than the minimum value
      • checkMinimumParameter

        public static void checkMinimumParameter​(java.lang.String name,
                                                 long min,
                                                 long actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is greater than or equal to min.
        Parameters:
        name - the parameter name
        min - the minimum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is less than the minimum value
      • checkMinimumParameter

        public static void checkMinimumParameter​(java.lang.String name,
                                                 float min,
                                                 float actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is greater than or equal to min.
        Parameters:
        name - the parameter name
        min - the minimum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is less than the minimum value
      • checkMinimumParameter

        public static void checkMinimumParameter​(java.lang.String name,
                                                 double min,
                                                 double actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is greater than or equal to min.
        Parameters:
        name - the parameter name
        min - the minimum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is less than the minimum value
      • checkMaximumParameter

        public static void checkMaximumParameter​(java.lang.String name,
                                                 int max,
                                                 int actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is less than or equal to max.
        Parameters:
        name - the parameter name
        max - the maximum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is greater than the minimum value
      • checkMaximumParameter

        public static void checkMaximumParameter​(java.lang.String name,
                                                 long max,
                                                 long actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is less than or equal to max.
        Parameters:
        name - the parameter name
        max - the maximum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is greater than the minimum value
      • checkMaximumParameter

        public static void checkMaximumParameter​(java.lang.String name,
                                                 float max,
                                                 float actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is less than or equal to max.
        Parameters:
        name - the parameter name
        max - the maximum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is greater than the minimum value
      • checkMaximumParameter

        public static void checkMaximumParameter​(java.lang.String name,
                                                 double max,
                                                 double actual)
                                          throws java.lang.IllegalArgumentException
        Check that the named parameter is less than or equal to max.
        Parameters:
        name - the parameter name
        max - the maximum value
        actual - the actual parameter value
        Throws:
        java.lang.IllegalArgumentException - if the actual value is greater than the minimum value
      • checkArrayBounds

        public static void checkArrayBounds​(java.lang.Object[] array,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array.
        Parameters:
        array - the array to check
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • checkArrayBounds

        public static void checkArrayBounds​(byte[] array,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array.
        Parameters:
        array - the array to check
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • checkArrayBounds

        public static void checkArrayBounds​(char[] array,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array.
        Parameters:
        array - the array to check
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • checkArrayBounds

        public static void checkArrayBounds​(int[] array,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array.
        Parameters:
        array - the array to check
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • checkArrayBounds

        public static void checkArrayBounds​(long[] array,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array.
        Parameters:
        array - the array to check
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • checkArrayBounds

        public static void checkArrayBounds​(int arrayLength,
                                            int offs,
                                            int len)
                                     throws java.lang.ArrayIndexOutOfBoundsException
        Check that the given offset and length fall completely within the bounds of the given array length.
        Parameters:
        arrayLength - the array length to check against
        offs - the array offset
        len - the array length
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the range of the offset and length do not fall within the array bounds
      • assertNotNull

        @NotNull
        public static <T> T assertNotNull​(T value)
        Assert that the value is not null. Use a standard assertion failure message if it is. Only runs if assert is enabled.
        Type Parameters:
        T - the value type
        Parameters:
        value - the not-null value
        Returns:
        the value that was passed in
      • assertHoldsLock

        @NotNull
        public static <T> T assertHoldsLock​(@NotNull
                                            T monitor)
        Assert that the given monitor is held by the current thread. Use a standard assertion failure message if it is not. Only runs if assert is enabled.
        Type Parameters:
        T - the monitor's type
        Parameters:
        monitor - the monitor object
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the monitor is null
      • assertNotHoldsLock

        @NotNull
        public static <T> T assertNotHoldsLock​(@NotNull
                                               T monitor)
        Assert that the given monitor is not held by the current thread. Use a standard assertion failure message if it is. Only runs if assert is enabled.
        Type Parameters:
        T - the monitor's type
        Parameters:
        monitor - the monitor object
        Returns:
        the value that was passed in
        Throws:
        java.lang.IllegalArgumentException - if the monitor is null
      • assertTrue

        public static boolean assertTrue​(boolean expr)
        Assert that the given expression is always true.
        Parameters:
        expr - the boolean expression
        Returns:
        the boolean expression
      • assertFalse

        public static boolean assertFalse​(boolean expr)
        Assert that the given expression is always false.
        Parameters:
        expr - the boolean expression
        Returns:
        the boolean expression
      • unreachableCode

        public static java.lang.IllegalStateException unreachableCode()
        Return an exception indicating that the current code was intended to be unreachable.
        Returns:
        the exception which may be immediately thrown
      • impossibleSwitchCase

        @NotNull
        public static java.lang.IllegalStateException impossibleSwitchCase​(@NotNull
                                                                           java.lang.Object obj)
        Return an exception indicating that the current switch case was intended to be unreachable.
        Parameters:
        obj - the switch case value
        Returns:
        the exception which may be immediately thrown
      • impossibleSwitchCase

        @NotNull
        public static java.lang.IllegalStateException impossibleSwitchCase​(int val)
        Return an exception indicating that the current switch case was intended to be unreachable.
        Parameters:
        val - the switch case value
        Returns:
        the exception which may be immediately thrown
      • impossibleSwitchCase

        @NotNull
        public static java.lang.IllegalStateException impossibleSwitchCase​(long val)
        Return an exception indicating that the current switch case was intended to be unreachable.
        Parameters:
        val - the switch case value
        Returns:
        the exception which may be immediately thrown
      • unsupported

        @NotNull
        public static java.lang.UnsupportedOperationException unsupported()
        Return an exception explaining that the caller's method is not supported.
        Returns:
        the exception