Class AssertUtils


  • public final class AssertUtils
    extends java.lang.Object
    Utility class used to examine function parameters. All the methods throw IllegalArgumentException if the argument doesn't fulfil constraints.
    Author:
    rstein
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areEqual​(double v1, double v2)
      The method returns true if both values area equal.
      static void assertType​(java.lang.Object obj, java.lang.Class<?> type)
      Asserts if the specified object is an instance of the specified type.
      static void belongsToEnum​(java.lang.String name, int[] allowedElements, int value)  
      static void checkArrayDimension​(java.lang.String name, boolean[] array, int defaultLength)  
      static void checkArrayDimension​(java.lang.String name, double[] array, int defaultLength)  
      static void checkArrayDimension​(java.lang.String name, int[] array, int defaultLength)  
      static <T> void equalArrays​(T[] array1, T[] array2)
      Asserts that the specified arrays have the same length.
      static void equalDoubleArrays​(double[] array1, double[] array2)
      Asserts that the specified arrays have the same length.
      static void equalDoubleArrays​(double[] array1, double[] array2, int nMinSize)
      Asserts that the specified arrays have the same length or are at least min size.
      static void equalFloatArrays​(float[] array1, float[] array2)
      Asserts that the specified arrays have the same length.
      static void equalFloatArrays​(float[] array1, float[] array2, int nMinSize)
      Asserts that the specified arrays have the same length or are at least min size.
      static void gtEqThanZero​(java.lang.String name, double value)
      Checks if the int value is >= 0
      static void gtEqThanZero​(java.lang.String name, int value)
      Checks if the int value is >= 0
      static <T extends java.lang.Number>
      void
      gtEqThanZero​(java.lang.String name, T value)
      Checks if the value is >= 0
      static void gtThanZero​(java.lang.String name, int value)
      Checks if the int value is >= 0
      static <T extends java.lang.Number>
      void
      gtThanZero​(java.lang.String name, T value)
      Checks if the value is >= 0
      static void indexInBounds​(int index, int bounds)
      Checks if the index is >= 0 and < bounds
      static void indexInBounds​(int index, int bounds, java.lang.String message)
      Checks if the index is >= 0 and < bounds
      static void indexOrder​(int index1, int index2, java.lang.String msg)
      Checks if the index1 <= index2
      static void indexOrder​(int index1, java.lang.String name1, int index2, java.lang.String name2)
      Checks if the index1 <= index2
      static void nonEmptyArray​(java.lang.String name, boolean[] array)  
      static void nonEmptyArray​(java.lang.String name, double[] array)  
      static void nonEmptyArray​(java.lang.String name, int[] array)  
      static void nonEmptyArray​(java.lang.String name, java.lang.Object[] array)  
      static <T> void notNull​(java.lang.String name, T obj)
      Checks if the 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> void notNull​(java.lang.String name,
                                       T obj)
        Checks if the object is not null.
        Type Parameters:
        T - generics object to be checked
        Parameters:
        name - name to be included in exception message.
        obj - object to be checked
      • indexInBounds

        public static void indexInBounds​(int index,
                                         int bounds)
        Checks if the index is >= 0 and < bounds
        Parameters:
        index - index to be checked
        bounds - maximum bound
      • indexInBounds

        public static void indexInBounds​(int index,
                                         int bounds,
                                         java.lang.String message)
        Checks if the index is >= 0 and < bounds
        Parameters:
        index - index to be checked
        bounds - maximum bound
        message - exception message
      • indexOrder

        public static void indexOrder​(int index1,
                                      java.lang.String name1,
                                      int index2,
                                      java.lang.String name2)
        Checks if the index1 <= index2
        Parameters:
        index1 - index1 to be checked
        name1 - name of index1
        index2 - index1 to be checked
        name2 - name of index2
      • indexOrder

        public static void indexOrder​(int index1,
                                      int index2,
                                      java.lang.String msg)
        Checks if the index1 <= index2
        Parameters:
        index1 - index1 to be checked
        index2 - index1 to be checked
        msg - exception message
      • gtThanZero

        public static <T extends java.lang.Number> void gtThanZero​(java.lang.String name,
                                                                   T value)
        Checks if the value is >= 0
        Type Parameters:
        T - generics object to be checked
        Parameters:
        name - name to be included in the exception message
        value - to be checked
      • gtEqThanZero

        public static <T extends java.lang.Number> void gtEqThanZero​(java.lang.String name,
                                                                     T value)
        Checks if the value is >= 0
        Type Parameters:
        T - generics object to be checked
        Parameters:
        name - name to be included in the exception message
        value - to be checked
      • equalDoubleArrays

        public static void equalDoubleArrays​(double[] array1,
                                             double[] array2)
        Asserts that the specified arrays have the same length.
        Parameters:
        array1 - to be checked
        array2 - to be checked
      • equalDoubleArrays

        public static void equalDoubleArrays​(double[] array1,
                                             double[] array2,
                                             int nMinSize)
        Asserts that the specified arrays have the same length or are at least min size.
        Parameters:
        array1 - to be checked
        array2 - to be checked
        nMinSize - minimum required size
      • equalArrays

        public static <T> void equalArrays​(T[] array1,
                                           T[] array2)
        Asserts that the specified arrays have the same length.
        Type Parameters:
        T - generics object to be checked
        Parameters:
        array1 - to be checked
        array2 - to be checked
      • equalFloatArrays

        public static void equalFloatArrays​(float[] array1,
                                            float[] array2)
        Asserts that the specified arrays have the same length.
        Parameters:
        array1 - to be checked
        array2 - to be checked
      • equalFloatArrays

        public static void equalFloatArrays​(float[] array1,
                                            float[] array2,
                                            int nMinSize)
        Asserts that the specified arrays have the same length or are at least min size.
        Parameters:
        array1 - to be checked
        array2 - to be checked
        nMinSize - minimum required size
      • nonEmptyArray

        public static void nonEmptyArray​(java.lang.String name,
                                         java.lang.Object[] array)
      • nonEmptyArray

        public static void nonEmptyArray​(java.lang.String name,
                                         double[] array)
      • nonEmptyArray

        public static void nonEmptyArray​(java.lang.String name,
                                         int[] array)
      • nonEmptyArray

        public static void nonEmptyArray​(java.lang.String name,
                                         boolean[] array)
      • belongsToEnum

        public static void belongsToEnum​(java.lang.String name,
                                         int[] allowedElements,
                                         int value)
      • assertType

        public static void assertType​(java.lang.Object obj,
                                      java.lang.Class<?> type)
        Asserts if the specified object is an instance of the specified type.
        Parameters:
        obj - to be checked
        type - required class type
        Throws:
        java.lang.IllegalArgumentException - in case of problems
      • areEqual

        public static boolean areEqual​(double v1,
                                       double v2)
        The method returns true if both values area equal. The method differs from simple == compare because it takes into account that both values can be Double.NaN, in which case == operator returns false.
        Parameters:
        v1 - to be checked
        v2 - to be checked
        Returns:
        true if v1 and v2 are Double.NaN or v1 == v2.
      • gtThanZero

        public static void gtThanZero​(java.lang.String name,
                                      int value)
        Checks if the int value is >= 0
        Parameters:
        name - name to be included in the exception message
        value - to be checked
      • gtEqThanZero

        public static void gtEqThanZero​(java.lang.String name,
                                        int value)
        Checks if the int value is >= 0
        Parameters:
        name - name to be included in the exception message
        value - to be checked
      • gtEqThanZero

        public static void gtEqThanZero​(java.lang.String name,
                                        double value)
        Checks if the int value is >= 0
        Parameters:
        name - name to be included in the exception message
        value - to be checked
      • checkArrayDimension

        public static void checkArrayDimension​(java.lang.String name,
                                               double[] array,
                                               int defaultLength)
      • checkArrayDimension

        public static void checkArrayDimension​(java.lang.String name,
                                               int[] array,
                                               int defaultLength)
      • checkArrayDimension

        public static void checkArrayDimension​(java.lang.String name,
                                               boolean[] array,
                                               int defaultLength)