Invariants.java

1
package com.renomad.minum.utils;
2
3
/**
4
 * Utilities for asserting invariants within the code.
5
 * <br>
6
 * The purpose here is to make firm statements about the code.  This is
7
 * to help document the code, (e.g. at this point, x is the sum of the ...),
8
 * to include testing mindset in the code, and to guard against adding
9
 * bugs during maintenance.
10
 */
11
public final class Invariants {
12
13
    private Invariants() {
14
        // cannot construct
15
    }
16
17
    /**
18
     * Specify something which must be true.
19
     * <p>
20
     * Throws an {@link InvariantException} if false
21
     * @param predicate the boolean expression that must be true at this point
22
     * @param message a message that will be included in the exception if this is false
23
     */
24
    public static void mustBeTrue(boolean predicate, String message) {
25 1 1. mustBeTrue : negated conditional → KILLED
        if (!predicate) {
26
            throw new InvariantException(message);
27
        }
28
    }
29
30
    /**
31
     * Specify something which must be false
32
     * <p>
33
     * Throws an {@link InvariantException} if true
34
     * @param predicate the boolean expression that must be false at this point
35
     * @param message a message that will be included in the exception if this is true
36
     */
37
    public static void mustBeFalse(boolean predicate, String message) {
38 1 1. mustBeFalse : negated conditional → KILLED
        if (predicate) {
39
            throw new InvariantException(message);
40
        }
41
    }
42
43
    /**
44
     * specifies that the parameter must be not null.
45
     * <p>
46
     * Throws an {@link InvariantException} if null.
47
     * @return the object if not null
48
     */
49
    public static <T> T mustNotBeNull(T object) {
50 1 1. mustNotBeNull : negated conditional → KILLED
        if (object == null) {
51
            throw new InvariantException("value must not be null");
52
        } else {
53 1 1. mustNotBeNull : replaced return value with null for com/renomad/minum/utils/Invariants::mustNotBeNull → TIMED_OUT
            return object;
54
        }
55
    }
56
}

Mutations

25

1.1
Location : mustBeTrue
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustBeTrue(com.renomad.minum.utils.InvariantsTests)
negated conditional → KILLED

38

1.1
Location : mustBeFalse
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustBeFalse(com.renomad.minum.utils.InvariantsTests)
negated conditional → KILLED

50

1.1
Location : mustNotBeNull
Killed by : com.renomad.minum.utils.InvariantsTests.test_MustNotBeNull(com.renomad.minum.utils.InvariantsTests)
negated conditional → KILLED

53

1.1
Location : mustNotBeNull
Killed by : none
replaced return value with null for com/renomad/minum/utils/Invariants::mustNotBeNull → TIMED_OUT

Active mutators

Tests examined


Report generated by PIT 1.17.0