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 final
class constant of typeFunction<String, Exception>
. This function can be used as the first argument to theCheck.on(...)
methods of theCheck
class. 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>
. TheSupplier
will pass the string to the exception's constructor when requested to supply the exception. TheSupplier
can 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>
. TheSupplier
will 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
Modifier 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 aSupplier
of aDuplicateValueException
.static Supplier
<DuplicateValueException> duplicateElement
(Object element) Returns aSupplier
of aDuplicateValueException
.static Supplier
<DuplicateValueException> Returns aSupplier
of aDuplicateValueException
.static Supplier
<DuplicateValueException> duplicateKey
(Object key) Returns aSupplier
of aDuplicateValueException
.static Supplier
<DuplicateValueException> duplicateValue
(String message) Returns aSupplier
of aDuplicateValueException
.static Supplier
<FileNotFoundException> fileNotFound
(File f) Returns aSupplier
of aFileNotFoundException
.static Supplier
<FileNotFoundException> fileNotFound
(String message) Returns aSupplier
of aFileNotFoundException
.static Supplier
<IllegalArgumentException> Returns aSupplier
of anIllegalArgumentException
.static Supplier
<IllegalArgumentException> illegalArgument
(String message) Returns aSupplier
of anIllegalArgumentException
.static Supplier
<IllegalStateException> Returns aSupplier
of anIllegalStateException
.static Supplier
<IllegalStateException> illegalState
(String message) Returns aSupplier
of anIllegalStateException
.static Supplier
<IndexOutOfBoundsException> indexOutOfBounds
(int index) Returns aSupplier
of anIndexOutOfBoundsException
.static Supplier
<IndexOutOfBoundsException> indexOutOfBounds
(String message) Returns aSupplier
of anIndexOutOfBoundsException
.static Supplier
<IOException> Returns aSupplier
of anIOException
.static Supplier
<IOException> ioException
(String message) Returns aSupplier
of anIOException
.static Supplier
<NoSuchElementException> Returns aSupplier
of aNoSuchElementException
.static Supplier
<NoSuchElementException> noSuchElement
(String message) Returns aSupplier
of aNoSuchElementException
.static Supplier
<NullPointerException> npe()
Returns aSupplier
of aNullPointerException
.static Supplier
<NullPointerException> Returns aSupplier
of 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.IllegalArgumentException
already is the exception that is thrown by default if a value fails to pass a test.
-
-
Method Details
-
illegalState
Returns aSupplier
of anIllegalStateException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of anIllegalStateException
-
illegalState
Returns aSupplier
of anIllegalStateException
. The supplier will call the no-arg constructor ofIllegalStateException
.- Returns:
- a
Supplier
of anIllegalStateException
-
illegalArgument
Returns aSupplier
of anIllegalArgumentException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of anIllegalArgumentException
-
illegalArgument
Returns aSupplier
of anIllegalArgumentException
. The supplier will call the no-arg constructor ofIllegalArgumentException
.- Returns:
- a
Supplier
of anIllegalArgumentException
-
indexOutOfBounds
Returns aSupplier
of anIndexOutOfBoundsException
.- Parameters:
index
- the out-of-bounds index- Returns:
- a
Supplier
of anIndexOutOfBoundsException
- See Also:
-
indexOutOfBounds
Returns aSupplier
of anIndexOutOfBoundsException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of anIndexOutOfBoundsException
- See Also:
-
ioException
Returns aSupplier
of anIOException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of anIOException
-
ioException
- Returns:
- a
Supplier
of anIOException
-
fileNotFound
Returns aSupplier
of aFileNotFoundException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of aFileNotFoundException
-
fileNotFound
Returns aSupplier
of aFileNotFoundException
.- Parameters:
f
- theFile
object corresponding to the non-existent file- Returns:
- a
Supplier
of aFileNotFoundException
-
npe
Returns aSupplier
of aNullPointerException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of aNullPointerException
-
npe
Returns aSupplier
of aNullPointerException
. The supplier will call the no-arg constructor ofNullPointerException
.- Returns:
- a
Supplier
of aNullPointerException
-
noSuchElement
Returns aSupplier
of aNoSuchElementException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of aNoSuchElementException
-
noSuchElement
Returns aSupplier
of aNoSuchElementException
. The supplier will call the no-arg constructor ofNoSuchElementException
.- Returns:
- a
Supplier
of aNoSuchElementException
-
duplicateKey
Returns aSupplier
of aDuplicateValueException
.- Returns:
- a
Supplier
of aDuplicateValueException
-
duplicateKey
Returns aSupplier
of aDuplicateValueException
.- Parameters:
key
- the key found to be a duplicate- Returns:
- a
Supplier
of aDuplicateValueException
-
duplicateElement
Returns aSupplier
of aDuplicateValueException
.- Parameters:
element
- the element found to be a duplicate- Returns:
- a
Supplier
of aDuplicateValueException
-
duplicateElement
Returns aSupplier
of aDuplicateValueException
.- Returns:
- a
Supplier
of aDuplicateValueException
-
duplicateValue
Returns aSupplier
of aDuplicateValueException
.- Parameters:
message
- the exception message- Returns:
- a
Supplier
of aDuplicateValueException
-