DEFENSIVE PROGRAMMING WITH KLOJANG CHECK

Tagging Values

You can optionally tag — or name — the value you are testing. If, for example, the value is a method argument, you might want to specify the corresponding parameter name. It could, however, just as well be something more descriptive. Value tagging is useful when validating multiple values within the same method as it makes it easier to trace an exception back to the value that caused it.

import static org.klojang.check.CommonChecks.gte;
import static org.klojang.check.CommonChecks.lte;

// ...

  public void processMessages(List<String> messages, int from, int to) {
    Check.notNull(messages, "messages");
    Check.that(from, "from-index").is(gte(), 0);
    Check.that(to, "to-index").is(gte(), from).is(lte(), messages.size());
    // process the messages ...
  }

As an aside, this particular sequence of checks is so common, it has been given its own method:

public void processMessages(List<String> messages, int from, int to) {
  Check.fromTo(messages, from, to);
}