Class InvariantChecks


  • public final class InvariantChecks
    extends java.lang.Object
    The InvariantChecks class provides static methods for checking different kinds of invariants. If a check fails a corresponding exception is thrown.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkBounds​(int index, int length)
      Checks the invariant "0 <= index < length" and throws an exception if it is violated.
      static void checkBoundsInclusive​(int index, int length)
      Checks the invariant "0 <= index <= length" and throws an exception if it is violated.
      static void checkFalse​(boolean condition)
      Checks the invariant "Condition is false" and throws an exception if it is violated.
      static void checkFalse​(boolean condition, java.lang.String message)
      Checks the invariant "Condition is false" and throws an exception if it is violated.
      static <T extends java.lang.Number & java.lang.Comparable<T>>
      void
      checkGreaterOrEq​(T first, T second)
      Checks the invariant "Numeric value first is greater than or equal to second" and throws an exception if it is violated.
      static void checkGreaterOrEqZero​(int num)
      Checks the invariant "Integer value is greater or equal 0" and throws an exception if it is violated.
      static <T extends java.lang.Number & java.lang.Comparable<T>>
      void
      checkGreaterThan​(T first, T second)
      Checks the invariant "Numeric value first is greater than second" and throws an exception if it is violated.
      static void checkGreaterThanZero​(int num)
      Checks the invariant "Integer value is greater than 0" and throws an exception if it is violated.
      static <T> void checkNotEmpty​(java.util.Collection<T> collection)
      Checks the invariants "Object reference is not null" and "Collection is not empty" throws an exception if they are violated.
      static <T> void checkNotEmpty​(T[] array)
      Checks the invariants "Object reference is not null" and "Array is not empty" throws an exception if they are violated.
      static <T> void checkNotNull​(T objType)
      Checks the invariant "Object reference is not null" and throws an exception if it is violated.
      static <T> void checkNotNull​(T obj, java.lang.String message)
      Checks the invariant "Object reference is not null" and throws an exception if it is violated.
      static void checkTrue​(boolean condition)
      Checks the invariant "Condition is true" and throws an exception if it is violated.
      static void checkTrue​(boolean condition, java.lang.String message)
      Checks the invariant "Condition is true" and throws an exception if it is violated.
      • Methods inherited from class java.lang.Object

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

      • checkTrue

        public static void checkTrue​(boolean condition)
        Checks the invariant "Condition is true" and throws an exception if it is violated.
        Parameters:
        condition - Condition to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (condition is false).
      • checkTrue

        public static void checkTrue​(boolean condition,
                                     java.lang.String message)
        Checks the invariant "Condition is true" and throws an exception if it is violated.
        Parameters:
        condition - Condition to be checked.
        message - Exception message.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (condition is false).
      • checkFalse

        public static void checkFalse​(boolean condition)
        Checks the invariant "Condition is false" and throws an exception if it is violated.
        Parameters:
        condition - Condition to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (condition is true).
      • checkFalse

        public static void checkFalse​(boolean condition,
                                      java.lang.String message)
        Checks the invariant "Condition is false" and throws an exception if it is violated.
        Parameters:
        condition - Condition to be checked.
        message - Exception message.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (condition is true).
      • checkNotNull

        public static <T> void checkNotNull​(T objType)
        Checks the invariant "Object reference is not null" and throws an exception if it is violated.
        Type Parameters:
        T - Object type.
        Parameters:
        objType - Object reference to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (o is null).
      • checkNotNull

        public static <T> void checkNotNull​(T obj,
                                            java.lang.String message)
        Checks the invariant "Object reference is not null" and throws an exception if it is violated.
        Type Parameters:
        T - Object type.
        Parameters:
        obj - Object reference to be checked.
        message - Exception message.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (o is null).
      • checkNotEmpty

        public static <T> void checkNotEmpty​(java.util.Collection<T> collection)
        Checks the invariants "Object reference is not null" and "Collection is not empty" throws an exception if they are violated.
        Type Parameters:
        T - Collection item type.
        Parameters:
        collection - Collection to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (o is null); if the invariant is violated (o.isEmpty).
      • checkNotEmpty

        public static <T> void checkNotEmpty​(T[] array)
        Checks the invariants "Object reference is not null" and "Array is not empty" throws an exception if they are violated.
        Type Parameters:
        T - Array element type.
        Parameters:
        array - Array to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (o is null); if the invariant is violated (o.length is 0).
      • checkGreaterThanZero

        public static void checkGreaterThanZero​(int num)
        Checks the invariant "Integer value is greater than 0" and throws an exception if it is violated.
        Parameters:
        num - Integer value to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (n <= 0).
      • checkGreaterOrEqZero

        public static void checkGreaterOrEqZero​(int num)
        Checks the invariant "Integer value is greater or equal 0" and throws an exception if it is violated.
        Parameters:
        num - Integer value to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (n < 0).
      • checkBounds

        public static void checkBounds​(int index,
                                       int length)
        Checks the invariant "0 <= index < length" and throws an exception if it is violated.
        Parameters:
        index - Index to be checked.
        length - Length of the allowed range.
        Throws:
        java.lang.IndexOutOfBoundsException - if the invariant is violated (index is not within range [0..length)).
      • checkBoundsInclusive

        public static void checkBoundsInclusive​(int index,
                                                int length)
        Checks the invariant "0 <= index <= length" and throws an exception if it is violated.
        Parameters:
        index - Index to be checked.
        length - Length of the allowed range.
        Throws:
        java.lang.IndexOutOfBoundsException - if the invariant is violated (index is not within range [0..length]).
      • checkGreaterThan

        public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterThan​(T first,
                                                                                                   T second)
        Checks the invariant "Numeric value first is greater than second" and throws an exception if it is violated.
        Type Parameters:
        T - Value type.
        Parameters:
        first - Numeric value to be checked.
        second - Numeric value to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (a <= b).
      • checkGreaterOrEq

        public static <T extends java.lang.Number & java.lang.Comparable<T>> void checkGreaterOrEq​(T first,
                                                                                                   T second)
        Checks the invariant "Numeric value first is greater than or equal to second" and throws an exception if it is violated.
        Type Parameters:
        T - Value type.
        Parameters:
        first - Numeric value to be checked.
        second - Numeric value to be checked.
        Throws:
        java.lang.IllegalArgumentException - if the invariant is violated (a < b).