Package io.blt.util

Class Ex

java.lang.Object
io.blt.util.Ex

public final class Ex extends Object
Static utility methods centred around Exception and Throwable.

Includes raising and handling exceptions.

  • Method Details

    • transformExceptions

      public static <R, E extends Throwable> R transformExceptions(ThrowingSupplier<R,? extends Exception> supplier, Function<? super Exception,E> transformer) throws E
      Executes a supplier, transforming any thrown Exception using a specified function. A thrown RuntimeException will bubble up unaltered.

      e.g., a custom high-level XmlProcessingException that 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 supplier
      E - The type of transformed exception
      Parameters:
      supplier - The supplier that may throw an exception
      transformer - 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 E
      Executes a runnable, transforming any thrown Exception using a specified function. A thrown RuntimeException will 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 exception
      transformer - 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 specified throwable if the given value satisfies the provided predicate. For convenience, value is 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 value
      E - the type of the throwable
      Parameters:
      value - the value to be checked
      predicate - the predicate to be evaluated
      throwable - the supplier for the throwable to be thrown
      Returns:
      value
      Throws:
      E - if the given value satisfies the provided predicate
      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 specified throwable if the given value does not satisfy the provided predicate. For convenience, value is returned. e.g.,
      
       throwUnless(properties, p -> p.containsKey("host"),
               () -> new IllegalStateException("Properties must contain a host"));
       
      Type Parameters:
      T - the type of the value
      E - the type of the throwable
      Parameters:
      value - the value to be checked
      predicate - the predicate to be evaluated
      throwable - the supplier for the throwable to be thrown
      Returns:
      value
      Throws:
      E - if the given value does not satisfy the provided predicate
      See Also: