Package io.blt.util
Class Ex
java.lang.Object
io.blt.util.Ex
Static utility methods centred around
Exception and Throwable.
Includes raising and handling exceptions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,E extends Throwable>
TThrows the specifiedthrowableif the givenvaluesatisfies the providedpredicate.static <T,E extends Throwable>
TthrowUnless(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) Throws the specifiedthrowableif the givenvaluedoes not satisfy the providedpredicate.static <E extends Throwable>
voidtransformExceptions(ThrowingRunnable<? extends Exception> runnable, Function<? super Exception, E> transformer) Executes a runnable, transforming any thrownExceptionusing a specified function.static <R,E extends Throwable>
RtransformExceptions(ThrowingSupplier<R, ? extends Exception> supplier, Function<? super Exception, E> transformer) Executes a supplier, transforming any thrownExceptionusing a specified function.
-
Method Details
-
transformExceptions
public static <R,E extends Throwable> R transformExceptions(ThrowingSupplier<R, ? extends Exception> supplier, Function<? super Exception, throws EE> transformer) Executes a supplier, transforming any thrownExceptionusing a specified function. A thrownRuntimeExceptionwill bubble up unaltered.e.g., a custom high-level
XmlProcessingExceptionthat we want to raise when parsing XML:public Document parseXml(String pathname) throws XmlProcessingException { return Ex.transformExceptions( () -> DocumentBuilderFactory .newInstance() .newDocumentBuilder() // throws ParserConfigurationException .parse(new File(pathname)), // throws SAXException, IOException XmlProcessingException::new); }- Type Parameters:
R- The result type of the supplierE- The type of transformed exception- Parameters:
supplier- The supplier that may throw an exceptiontransformer- Function to transform exceptions thrown by the supplier- Returns:
- The result of the supplier if successful
- Throws:
E- If the supplier throws an exception, transformed by the provided function
-
transformExceptions
public static <E extends Throwable> void transformExceptions(ThrowingRunnable<? extends Exception> runnable, Function<? super Exception, E> transformer) throws EExecutes a runnable, transforming any thrownExceptionusing a specified function. A thrownRuntimeExceptionwill bubble up unaltered.e.g.,
public void appendFormattedDateToFile(String date, String fileName) throws LoggingException { Ex.transformExceptions( () -> { var formatted = new SimpleDateFormat() .parse(date) // ParseException .toString(); Files.write( // IOException Paths.get(fileName), formatted.getBytes(), StandardOpenOption.APPEND); }, LoggingException::new ); }- Type Parameters:
E- The type of transformed exception- Parameters:
runnable- The runnable that may throw an exceptiontransformer- Function to transform exceptions thrown by the supplier- Throws:
E- If the supplier throws an exception, transformed by the provided function
-
throwIf
public static <T,E extends Throwable> T throwIf(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) throws E Throws the specifiedthrowableif the givenvaluesatisfies the providedpredicate. For convenience,valueis returned. e.g.,public Map<String, String> loadProperties() { return throwIf(Properties.loadFromJson(FILENAME), Map::isEmpty, () -> new IllegalStateException("Properties must not be empty")); }- Type Parameters:
T- the type of the valueE- the type of the throwable- Parameters:
value- the value to be checkedpredicate- the predicate to be evaluatedthrowable- the supplier for the throwable to be thrown- Returns:
value- Throws:
E- if the givenvaluesatisfies the providedpredicate- See Also:
-
throwUnless
public static <T,E extends Throwable> T throwUnless(T value, Predicate<? super T> predicate, Supplier<? extends E> throwable) throws E Throws the specifiedthrowableif the givenvaluedoes not satisfy the providedpredicate. For convenience,valueis returned. e.g.,throwUnless(properties, p -> p.containsKey("host"), () -> new IllegalStateException("Properties must contain a host"));- Type Parameters:
T- the type of the valueE- the type of the throwable- Parameters:
value- the value to be checkedpredicate- the predicate to be evaluatedthrowable- the supplier for the throwable to be thrown- Returns:
value- Throws:
E- if the givenvaluedoes not satisfy the providedpredicate- See Also:
-