Class CommonExceptions
java.lang.Object
org.klojang.check.CommonExceptions
Provides factories for some commonly thrown exceptions. Typically (but not
always), for each type of exception, three exception factories are provided:
- A
public static finalclass constant of typeFunction<String, Exception>. This function can be used as the first argument to theCheck.on(...)methods of theCheckclass. It sets the default exception, to be thrown if the value fails to pass any of the subsequent tests. - A method that takes a
String(the error message) and returns aSupplier<Exception>. TheSupplierwill pass the string to the exception's constructor when requested to supply the exception. TheSuppliercan be used as the last argument to checks that allow you to specify an alternative exception. - A method that takes no arguments and returns a
Supplier<Exception>. TheSupplierwill instantiate the exception using its no-arg constructor. ThisSupplier, 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
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Function<String, IllegalArgumentException> Shortcut forIllegalArgumentException::new.static final Function<String, DuplicateValueException> Shortcut forDuplicateValueException::new.static final Function<String, NoSuchElementException> Shortcut forNoSuchElementException::new.static final Function<String, FileNotFoundException> Shortcut forFileNotFoundException::new.static final Function<String, IndexOutOfBoundsException> Shortcut forIndexOutOfBoundsException::new.static final Function<String, IOException> Shortcut forIOException::new.static final Function<String, NullPointerException> Shortcut forNullPointerException::new.static final Function<String, IllegalStateException> Shortcut forIllegalStateException::new. -
Method Summary
Modifier and TypeMethodDescriptionstatic Supplier<DuplicateValueException> Returns aSupplierof aDuplicateValueException.static Supplier<DuplicateValueException> duplicateElement(Object element) Returns aSupplierof aDuplicateValueException.static Supplier<DuplicateValueException> Returns aSupplierof aDuplicateValueException.static Supplier<DuplicateValueException> duplicateKey(Object key) Returns aSupplierof aDuplicateValueException.static Supplier<DuplicateValueException> duplicateValue(String message) Returns aSupplierof aDuplicateValueException.static Supplier<FileNotFoundException> fileNotFound(File f) Returns aSupplierof aFileNotFoundException.static Supplier<FileNotFoundException> fileNotFound(String message) Returns aSupplierof aFileNotFoundException.static Supplier<IllegalArgumentException> Returns aSupplierof anIllegalArgumentException.static Supplier<IllegalArgumentException> illegalArgument(String message) Returns aSupplierof anIllegalArgumentException.static Supplier<IllegalStateException> Returns aSupplierof anIllegalStateException.static Supplier<IllegalStateException> illegalState(String message) Returns aSupplierof anIllegalStateException.static Supplier<IndexOutOfBoundsException> indexOutOfBounds(int index) Returns aSupplierof anIndexOutOfBoundsException.static Supplier<IndexOutOfBoundsException> indexOutOfBounds(String message) Returns aSupplierof anIndexOutOfBoundsException.static Supplier<IOException> Returns aSupplierof anIOException.static Supplier<IOException> ioException(String message) Returns aSupplierof anIOException.static Supplier<NoSuchElementException> Returns aSupplierof aNoSuchElementException.static Supplier<NoSuchElementException> noSuchElement(String message) Returns aSupplierof aNoSuchElementException.static Supplier<NullPointerException> npe()Returns aSupplierof aNullPointerException.static Supplier<NullPointerException> Returns aSupplierof aNullPointerException.
-
Field Details
-
STATE
Shortcut forIllegalStateException::new. -
INDEX
Shortcut forIndexOutOfBoundsException::new. -
IO
Shortcut forIOException::new. -
FILE
Shortcut forFileNotFoundException::new. -
NULL
Shortcut forNullPointerException::new. -
ELEMENT
Shortcut forNoSuchElementException::new. -
DUPLICATE
Shortcut forDuplicateValueException::new. -
ARGUMENT
Shortcut forIllegalArgumentException::new. Included for completeness.IllegalArgumentExceptionalready is the exception that is thrown by default if a value fails to pass a test.
-
-
Method Details
-
illegalState
Returns aSupplierof anIllegalStateException.- Parameters:
message- the exception message- Returns:
- a
Supplierof anIllegalStateException
-
illegalState
Returns aSupplierof anIllegalStateException. The supplier will call the no-arg constructor ofIllegalStateException.- Returns:
- a
Supplierof anIllegalStateException
-
illegalArgument
Returns aSupplierof anIllegalArgumentException.- Parameters:
message- the exception message- Returns:
- a
Supplierof anIllegalArgumentException
-
illegalArgument
Returns aSupplierof anIllegalArgumentException. The supplier will call the no-arg constructor ofIllegalArgumentException.- Returns:
- a
Supplierof anIllegalArgumentException
-
indexOutOfBounds
Returns aSupplierof anIndexOutOfBoundsException.- Parameters:
index- the out-of-bounds index- Returns:
- a
Supplierof anIndexOutOfBoundsException - See Also:
-
indexOutOfBounds
Returns aSupplierof anIndexOutOfBoundsException.- Parameters:
message- the exception message- Returns:
- a
Supplierof anIndexOutOfBoundsException - See Also:
-
ioException
Returns aSupplierof anIOException.- Parameters:
message- the exception message- Returns:
- a
Supplierof anIOException
-
ioException
- Returns:
- a
Supplierof anIOException
-
fileNotFound
Returns aSupplierof aFileNotFoundException.- Parameters:
message- the exception message- Returns:
- a
Supplierof aFileNotFoundException
-
fileNotFound
Returns aSupplierof aFileNotFoundException.- Parameters:
f- theFileobject corresponding to the non-existent file- Returns:
- a
Supplierof aFileNotFoundException
-
npe
Returns aSupplierof aNullPointerException.- Parameters:
message- the exception message- Returns:
- a
Supplierof aNullPointerException
-
npe
Returns aSupplierof aNullPointerException. The supplier will call the no-arg constructor ofNullPointerException.- Returns:
- a
Supplierof aNullPointerException
-
noSuchElement
Returns aSupplierof aNoSuchElementException.- Parameters:
message- the exception message- Returns:
- a
Supplierof aNoSuchElementException
-
noSuchElement
Returns aSupplierof aNoSuchElementException. The supplier will call the no-arg constructor ofNoSuchElementException.- Returns:
- a
Supplierof aNoSuchElementException
-
duplicateKey
Returns aSupplierof aDuplicateValueException.- Returns:
- a
Supplierof aDuplicateValueException
-
duplicateKey
Returns aSupplierof aDuplicateValueException.- Parameters:
key- the key found to be a duplicate- Returns:
- a
Supplierof aDuplicateValueException
-
duplicateElement
Returns aSupplierof aDuplicateValueException.- Parameters:
element- the element found to be a duplicate- Returns:
- a
Supplierof aDuplicateValueException
-
duplicateElement
Returns aSupplierof aDuplicateValueException.- Returns:
- a
Supplierof aDuplicateValueException
-
duplicateValue
Returns aSupplierof aDuplicateValueException.- Parameters:
message- the exception message- Returns:
- a
Supplierof aDuplicateValueException
-