DEFENSIVE PROGRAMMING WITH KLOJANG CHECK

Testing Against a Set of Values

Klojang Check lets you specify that a value must have a certain relation not just to one other value, but to a set of other values — a value domain in the vocabulary of predicate logic:

import static org.klojang.check.CommonChecks.notEmpty;
import static org.klojang.check.CommonChecks.contains;
import static org.klojang.check.relation.Quantifier.noneOf;

// ...

  public void applyColors(List<Color> colors) {
    Check.that(colors).is(notEmpty().and(contains(), noneOf(), RED, BLUE, PINK);
    // apply the colors
  }

The noneOf() method returns an instance of the Quantifier enum, which represents a logical quantifier. This is what the Quantifier enum basically looks like:

public enum Quantifier {

  ALL, ANY, NONE;

  public static Quantifier allOf() { return ALL; }

  public static Quantifier anyOf() { return ANY; }

  public static Quantifier noneOf() { return NONE; }
}

Notice that this time the and() method is not called on the ObjectCheck instance created by Check.that(). It is called on the notEmpty() check. This is explained separately in the chapter on Composing Tests.

Strictly speaking it would have been more correct to model the value domain (in this case  RED,  BLUE  and  PINK) as a Set. For convenience sake, however, it has been modeled as a varargs array.