Interface ThrowableSupplier<T>

  • Type Parameters:
    T - the type of result supplied by this supplier
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface ThrowableSupplier<T>
    A functional interface similar to Supplier, but allows the get() method to throw a Throwable. This is useful for functional constructs where operations may throw checked exceptions that need to be handled or rethrown.

    Example Usage

    
     // Using ThrowableSupplier to read a file content
     ThrowableSupplier<String> fileReader = () -> {
         Path path = Paths.get("example.txt");
         return Files.readString(path);
     };
    
     // Execute with default exception handling (converts to RuntimeException)
     String content = fileReader.execute();
    
     // Execute with custom exception handling
     String contentWithHandler = fileReader.execute(ex -> {
         System.err.println("Error reading file: " + ex.getMessage());
         return "default content";
     });
     

    This interface provides convenience methods to execute the supplier and handle exceptions using a custom handler, making it easier to work with functional patterns in environments where exceptions must be managed.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Supplier, Throwable
    • Method Detail

      • get

        T get()
        throws java.lang.Throwable
        Applies this function to the given argument.
        Returns:
        the supplied result
        Throws:
        java.lang.Throwable - if met with any error
      • execute

        default T execute​(java.util.function.Function<java.lang.Throwable,​T> exceptionHandler)
        Execute get() with the customized exception handling
        Parameters:
        exceptionHandler - the handler to handle any exception that the get() method throws
        Returns:
        the supplied result
        See Also:
        execute()
      • handleException

        default T handleException​(java.lang.Throwable failure)
        Handle any exception that the get() method throws
        Parameters:
        failure - the instance of Throwable
        Returns:
        the result after the exception handling
      • execute

        static <T> T execute​(ThrowableSupplier<T> supplier)
                      throws java.lang.NullPointerException
        Executes the given ThrowableSupplier with the default exception handling.

        This method is useful when you want to invoke a supplier that may throw checked exceptions without explicitly handling them. Any exception thrown during the execution of the supplier will be passed to the default exception handler, which wraps it into a RuntimeException.

        Example Usage

        
         // Using the static execute method with default exception handling
         String content = ThrowableSupplier.execute(() -> {
             Path path = Paths.get("example.txt");
             return Files.readString(path);
         });
         
        Type Parameters:
        T - The type of result supplied by the supplier.
        Parameters:
        supplier - The supplier to execute.
        Returns:
        The result of the supplier after successful execution.
        Throws:
        java.lang.NullPointerException - if the given supplier is null.
      • execute

        static <T> T execute​(ThrowableSupplier<T> supplier,
                             java.util.function.Function<java.lang.Throwable,​T> exceptionHandler)
                      throws java.lang.NullPointerException
        Executes the given ThrowableSupplier with a custom exception handler.

        This method allows for flexible exception handling when executing a supplier that may throw checked exceptions. If the supplier or exception handler is null, a NullPointerException will be thrown.

        Example Usage

        
         // Example 1: Using a custom exception handler to return a default value
         String content = execute(
             () -> Files.readString(Paths.get("example.txt")),
             ex -> "Default Content"
         );
        
         // Example 2: Logging and rethrowing as a custom exception
         String content = execute(
             () -> Files.readString(Paths.get("example.txt")),
             ex -> {
                 System.err.println("Failed to read file: " + ex.getMessage());
                 throw new RuntimeException(ex);
             }
         );
         
        Type Parameters:
        T - The type of result supplied by the supplier.
        Parameters:
        supplier - The supplier to execute. Must not be null.
        exceptionHandler - The handler to manage any exception thrown by the supplier. Must not be null.
        Returns:
        The result of the supplier after execution or after handling an exception.
        Throws:
        java.lang.NullPointerException - if either the supplier or the exception handler is null.