Class CommonExceptions

java.lang.Object
org.klojang.check.CommonExceptions

public final class CommonExceptions extends Object
Provides factories for some commonly thrown exceptions. Typically (but not always), for each type of exception, three exception factories are provided:
  1. A public static final class constant of type Function<String, Exception>. This function can be used as the first argument to the Check.on(...) methods of the Check class. It sets the default exception, to be thrown if the value fails to pass any of the subsequent tests.
  2. A method that takes a String (the error message) and returns a Supplier<Exception>. The Supplier will pass the string to the exception's constructor when requested to supply the exception. The Supplier can be used as the last argument to checks that allow you to specify an alternative exception.
  3. A method that takes no arguments and returns a Supplier<Exception>. The Supplier will instantiate the exception using its no-arg constructor. This Supplier, too, can be used as the last argument to checks that allow you to specify an alternative exception.

The following examples make this more concrete:


 Check.on(STATE, file, "file").is(writable());
 // is shortcut for:
 Check.on(IllegalStateException::new, file, "file").is(writable());

 Check.on(STATE, file).is(writable(), "file not writable");
 // is shortcut for:
 Check.on(IllegalStateException::new, file).is(writable(), "file not writable");

 Check.that(file).is(writable(), illegalState("file not writable"));
 // is shortcut for:
 Check.that(file).is(writable(), () -> new IllegalStateException("file not writable"));

 Check.that(file).is(writable(), illegalState());
 // is shortcut for:
 Check.that(file).is(writable(), () -> new IllegalStateException());
 
Author:
Ayco Holleman