Package dev.nm.misc

Class ArgumentAssertion


  • public final class ArgumentAssertion
    extends Object
    Utility class for checking numerical arguments. Arguments of same primitive type of double, int, long, short, float and byte can be used as input arguments. If input arguments violate the assertion, an IllegalArgumentException with appropriate error message will be thrown.

    For example:

     
     ArgumentAssertion.assertRangeLeftOpen(1.5, 1.0, 2.0); // double
     ArgumentAssertion.assertRangeLeftOpen(2, 1, 3); // int
     ArgumentAssertion.assertRangeLeftOpen(1.5f, 1.0f, 2.0f); // float
     
     
    • Method Detail

      • assertRange

        public static <T extends Number & Comparable<? super T>> void assertRange​(T x,
                                                                                  T low,
                                                                                  T high,
                                                                                  String variableName)
        Test whether the specified Number occurs within the range [low, high] (both inclusive). Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        low - lower bound of the range, inclusive
        high - upper bound of the range, inclusive
        variableName - the display name of variable x
      • assertRangeOpen

        public static <T extends Number & Comparable<? super T>> void assertRangeOpen​(T x,
                                                                                      T low,
                                                                                      T high,
                                                                                      String variableName)
        Test whether the specified Number occurs within the range (low, high) (both exclusive). Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        low - lower bound of the range, exclusive
        high - upper bound of the range, exclusive
        variableName - the display name of variable x
      • assertRangeLeftOpen

        public static <T extends Number & Comparable<? super T>> void assertRangeLeftOpen​(T x,
                                                                                          T low,
                                                                                          T high,
                                                                                          String variableName)
        Test whether the specified Number occurs within the range (low, high] (left exclusive, right inclusive). Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        low - lower bound of the range, exclusive
        high - upper bound of the range, inclusive
        variableName - the display name of variable x
      • assertRangeRightOpen

        public static <T extends Number & Comparable<? super T>> void assertRangeRightOpen​(T x,
                                                                                           T low,
                                                                                           T high,
                                                                                           String variableName)
        Test whether the specified Number occurs within the range [low, high) (left inclusive, right exclusive). Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        low - lower bound of the range, inclusive
        high - upper bound of the range, exclusive
        variableName - the display name of variable x
      • assertGreaterThan

        public static <T extends Number & Comparable<? super T>> void assertGreaterThan​(T x,
                                                                                        T bound,
                                                                                        String variableName)
        Test if Number x is greater than bound. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        bound - the number to see if x is greater than
        variableName - the display name of variable x
      • assertNotLessThan

        public static <T extends Number & Comparable<? super T>> void assertNotLessThan​(T x,
                                                                                        T bound,
                                                                                        String variableName)
        Test if Number x is not less than bound. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        bound - the number to see if x is not less than
        variableName - the display name of variable x
      • assertLessThan

        public static <T extends Number & Comparable<? super T>> void assertLessThan​(T x,
                                                                                     T bound,
                                                                                     String variableName)
        Test if Number x is less than bound. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        bound - the number to see if x is less than
        variableName - the display name of variable x
      • assertNotGreaterThan

        public static <T extends Number & Comparable<? super T>> void assertNotGreaterThan​(T x,
                                                                                           T bound,
                                                                                           String variableName)
        Test if Number x is not greater than bound. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        bound - the number to see if x is not greater than
        variableName - the display name of variable x
      • assertPositive

        public static <T extends Number & Comparable<? super T>> void assertPositive​(T x,
                                                                                     String variableName)
        Test if Number x is positive. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        variableName - the display name of variable x
      • assertNonNegative

        public static <T extends Number & Comparable<? super T>> void assertNonNegative​(T x,
                                                                                        String variableName)
        Test if Number x is non-negative. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        variableName - the display name of variable x
      • assertNegative

        public static <T extends Number & Comparable<? super T>> void assertNegative​(T x,
                                                                                     String variableName)
        Test if Number x is negative. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        variableName - the display name of variable x
      • assertNonPositive

        public static <T extends Number & Comparable<? super T>> void assertNonPositive​(T x,
                                                                                        String variableName)
        Test if Number x is non-positive. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        variableName - the display name of variable x
      • assertEqual

        public static <T extends Number & Comparable<? super T>> void assertEqual​(T x,
                                                                                  T bound,
                                                                                  String variableName)
        Test if Number x equal to bound. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x - the number to test
        bound - the number to see if x is equal to
        variableName - the display name of variable x
      • assertEqual

        public static <T extends Number & Comparable<? super T>> void assertEqual​(T x1,
                                                                                  T x2,
                                                                                  String variableName1,
                                                                                  String variableName2)
        Test if two Numbers x1 and x2 are equal. Throw IllegalArgumentException if not.
        Type Parameters:
        T - subclass of Number which implements Comparable
        Parameters:
        x1 - the number x1
        x2 - the number x2
        variableName1 - the display name of variable x1
        variableName2 - the display name of variable x2
      • assertTrue

        public static void assertTrue​(boolean condition,
                                      String errorMessage,
                                      Object... args)
        Check if an argument condition is true. Throw IllegalArgumentException if it is false.
        Parameters:
        condition - the argument condition to be checked
        errorMessage - the error message if the condition is not true (format string can be used with args, see String.format(java.lang.String, java.lang.Object[]))
        args - the arguments for the error message
      • assertFalse

        public static void assertFalse​(boolean condition,
                                       String errorMessage,
                                       Object... args)
        Check if an argument condition is false. Throw IllegalArgumentException if it is true.
        Parameters:
        condition - the argument condition to be checked
        errorMessage - the error message if the condition is not false (format string can be used with args, see String.format(java.lang.String, java.lang.Object[]))
        args - the arguments for the error message
      • assertNotNull

        public static void assertNotNull​(Object obj,
                                         String variableName)
        Check if obj is not null. Throw IllegalArgumentException if it is null.
        Parameters:
        obj - variable to be checked
        variableName - the display name of variable obj
      • assertNull

        public static void assertNull​(Object obj,
                                      String variableName)
        Check if obj is null. Throw IllegalArgumentException if it is not null.
        Parameters:
        obj - variable to be checked
        variableName - the display name of variable obj
      • assertNotNaN

        public static void assertNotNaN​(double d,
                                        String variableName)
        Check if an argument is NOT a Double.NaN. Throw IllegalArgumentException if it is a NaN.
        Parameters:
        d - the double variable
        variableName - the display name of the variable
      • assertNormalDouble

        public static void assertNormalDouble​(double d,
                                              String variableName)
        Check if an argument is a normal double value (that is, NOT Double.NaN nor infinity). Throw IllegalArgumentException if it is not a normal double value.
        Parameters:
        d - the double variable
        variableName - the display name of the variable
      • assertNotNaN

        public static void assertNotNaN​(float f,
                                        String variableName)
        Check if an argument is NOT a Float.NaN. Throw IllegalArgumentException if it is a NaN.
        Parameters:
        f - the float variable
        variableName - the display name of the variable
      • assertNormalFloat

        public static void assertNormalFloat​(float f,
                                             String variableName)
        Check if an argument is a normal float value (that is, NOT Float.NaN nor infinity). Throw IllegalArgumentException if it is not a normal float value.
        Parameters:
        f - the float variable
        variableName - the display name of the variable