Class Invariants


  • public final class Invariants
    extends java.lang.Object
    Functions to check invariants.
    • Method Detail

      • checkInvariants

        @SafeVarargs
        public static <T> T checkInvariants​(T value,
                                            ContractConditionType<T>... conditions)
                                     throws InvariantViolationException

        Evaluate all of the given conditions using value as input.

        All of the conditions are evaluated and the function throws InvariantViolationException if any of the conditions are false, or raise an exception that is not of type Error. Exceptions of type Error are propagated immediately, without any further contract checking.

        Type Parameters:
        T - The type of values
        Parameters:
        value - The value
        conditions - The set of conditions
        Returns:
        value
        Throws:
        InvariantViolationException - If any of the conditions are false
      • checkInvariant

        public static <T> T checkInvariant​(T value,
                                           java.util.function.Predicate<T> predicate,
                                           java.util.function.Function<T,​java.lang.String> describer)

        Evaluate the given predicate using value as input.

        The function throws InvariantViolationException if the predicate is false.

        Type Parameters:
        T - The type of values
        Parameters:
        value - The value
        predicate - The predicate
        describer - A describer for the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariant

        public static <T> T checkInvariant​(T value,
                                           boolean condition,
                                           java.util.function.Function<T,​java.lang.String> describer)

        Evaluate the given predicate using value as input.

        The function throws InvariantViolationException if the predicate is false.

        Type Parameters:
        T - The type of values
        Parameters:
        value - The value
        condition - The predicate
        describer - A describer for the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantV

        public static <T> T checkInvariantV​(T value,
                                            boolean condition,
                                            java.lang.String format,
                                            java.lang.Object... objects)

        A version of checkInvariant(boolean, String) that constructs a description message from the given format string and arguments.

        Note that the use of variadic arguments may entail allocating memory on virtual machines that fail to eliminate the allocations with escape analysis.

        Type Parameters:
        T - The precise type of values
        Parameters:
        value - The value
        condition - The predicate
        format - The format string
        objects - The format string arguments
        Returns:
        value
        Since:
        1.1.0
      • checkInvariantV

        public static void checkInvariantV​(boolean condition,
                                           java.lang.String format,
                                           java.lang.Object... objects)

        A version of checkInvariant(boolean, String) that constructs a description message from the given format string and arguments.

        Note that the use of variadic arguments may entail allocating memory on virtual machines that fail to eliminate the allocations with escape analysis.

        Parameters:
        condition - The predicate
        format - The format string
        objects - The format string arguments
        Since:
        1.1.0
      • checkInvariantI

        public static int checkInvariantI​(int value,
                                          java.util.function.IntPredicate predicate,
                                          java.util.function.IntFunction<java.lang.String> describer)
        An int specialized version of checkInvariant(Object, ContractConditionType).
        Parameters:
        value - The value
        predicate - The predicate
        describer - The describer for the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantI

        public static int checkInvariantI​(int value,
                                          boolean condition,
                                          java.util.function.IntFunction<java.lang.String> describer)
        An int specialized version of checkInvariant(Object, boolean, Function).
        Parameters:
        value - The value
        condition - The predicate
        describer - The describer for the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantL

        public static long checkInvariantL​(long value,
                                           java.util.function.LongPredicate predicate,
                                           java.util.function.LongFunction<java.lang.String> describer)
        A long specialized version of checkInvariant(Object, Predicate, Function)
        Parameters:
        value - The value
        predicate - The predicate
        describer - The describer of the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantL

        public static long checkInvariantL​(long value,
                                           boolean condition,
                                           java.util.function.LongFunction<java.lang.String> describer)
        A long specialized version of checkInvariant(Object, Predicate, Function)
        Parameters:
        condition - The predicate
        value - The value
        describer - The describer of the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantD

        public static double checkInvariantD​(double value,
                                             java.util.function.DoublePredicate predicate,
                                             java.util.function.DoubleFunction<java.lang.String> describer)
        A double specialized version of checkInvariant(Object, Predicate, Function)
        Parameters:
        value - The value
        predicate - The predicate
        describer - The describer of the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false
      • checkInvariantD

        public static double checkInvariantD​(double value,
                                             boolean condition,
                                             java.util.function.DoubleFunction<java.lang.String> describer)
        A double specialized version of checkInvariant(Object, boolean, Function)
        Parameters:
        value - The value
        condition - The predicate
        describer - The describer of the predicate
        Returns:
        value
        Throws:
        InvariantViolationException - If the predicate is false