DEFENSIVE PROGRAMMING WITH KLOJANG CHECK

Predicates and Relations

You may have noticed that some of the checks from the CommonChecks class don't really look like a Predicate or IntPredicate, the two functional interfaces most commonly used to test a value. Indeed, the checks you can pass to the is(...) and isNot(...) methods broadly fall apart in two categories: Predicate and IntPredicate on the one hand, and, on the other, Relation, IntRelation and IntObjRelation. The latter are not part of the JDK. They reside inside the Klojang Check module itself.

A Relation can be thought of as a "BiPredicate" (which neither exists in the JDK). It takes two arguments and returns a boolean. If the two arguments have a certain relationship with each other, the relation is said to exist and the function returns true. Within the context of Klojang Check, the first argument is always the value to be validated. The second argument is the value that it is to be validated against.

Here is an example for each of the five functional interfaces mentioned above:

    // Predicate:
    Check.that(file).is(writable());

    // IntPredicate:
    Check.that(numChairs).is(even());

    // Relation:
    Check.that(vehicle).is(instanceOf(), Car.class);

    // IntRelation:
    Check.that(toIndex).is(gte(), fromIndex);

    // IntObjRelation:
    Check.that(i).is(indexOf(), list);