Interface ThrowingFunction4<A,B,C,D,R>

Type Parameters:
A - The type of the first argument to the function.
B - The type of the second argument to the function.
C - The type of the third argument to the function.
D - The type of the fourth argument to the function.
R - The type of the result of the function.
All Superinterfaces:
Function4<A,B,C,D,R>
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 ThrowingFunction4<A,B,C,D,R> extends Function4<A,B,C,D,R>
A functional interface representing a function that accepts four arguments, produces a result, and may throw a checked exception.

This interface extends Function4 to allow the functional method to throw checked exceptions. The default apply(Object, Object, Object, Object) method wraps any checked exceptions in a RuntimeException, making it compatible with standard Function4 usage while still allowing the implementation to throw checked exceptions via applyThrows(Object, Object, Object, Object).

Features:
Use Cases:
  • Four-argument transformations that may throw I/O exceptions
  • Parsing operations that may throw validation exceptions
  • Data conversion that may fail with checked exceptions
  • Operations that need to propagate checked exceptions
Usage:

// Lambda with exception ThrowingFunction4<String,String,String,String,File> fileParser = (dir, subdir1, subdir2, name) -> { Path path = Paths.get(dir, subdir1, subdir2, name); if (! Files.exists(path)) { throw new FileNotFoundException("File not found: " + path); } return path.toFile(); }; // Using applyThrows to get checked exception try { File file = fileParser.applyThrows("/tmp", "data", "2024", "file.txt"); } catch (FileNotFoundException e) { // Handle checked exception } // Using apply wraps exception in RuntimeException File file = fileParser.apply("/tmp", "data", "2024", "file.txt"); // May throw RuntimeException

Exception Handling:
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default <V> ThrowingFunction4<A,B,C,D,V>
    andThen(Function<? super R,? extends V> after)
    Returns a composed function that first applies this function to its input, and then applies the after function to the result.
    default R
    apply(A a, B b, C c, D d)
    Applies this function to the given arguments, wrapping any checked exceptions in a RuntimeException.
    applyThrows(A a, B b, C c, D d)
    Applies this function to the given arguments, potentially throwing a checked exception.
  • Method Details

    • apply

      default R apply(A a, B b, C c, D d)
      Applies this function to the given arguments, wrapping any checked exceptions in a RuntimeException.

      This is the default implementation that makes this interface compatible with Function4. It calls applyThrows(Object, Object, Object, Object) and wraps any checked exceptions.

      Specified by:
      apply in interface Function4<A,B,C,D,R>
      Parameters:
      a - The first function argument.
      b - The second function argument.
      c - The third function argument.
      d - The fourth function argument.
      Returns:
      The function result.
      Throws:
      RuntimeException - if applyThrows(Object, Object, Object, Object) throws a checked exception.
    • andThen

      default <V> ThrowingFunction4<A,B,C,D,V> andThen(Function<? super R,? extends V> after)
      Returns a composed function that first applies this function to its input, and then applies the after function to the result.

      This method enables function composition, allowing you to chain multiple transformations together. The composed function will throw exceptions from this function, but the after function is a standard Function that does not throw checked exceptions.

      Specified by:
      andThen in interface Function4<A,B,C,D,R>
      Type Parameters:
      V - The type of output of the after function, and of the composed function.
      Parameters:
      after - The function to apply after this function is applied. Must not be null.
      Returns:
      A composed ThrowingFunction4 that first applies this function and then applies the after function.
      Throws:
      NullPointerException - if after is null.
    • applyThrows

      R applyThrows(A a, B b, C c, D d) throws Exception
      Applies this function to the given arguments, potentially throwing a checked exception.

      This is the functional method that implementations must provide. It allows checked exceptions to be thrown directly, unlike the standard Function4.apply(Object, Object, Object, Object) method.

      Example:

      ThrowingFunction4<String,Integer,Boolean,Double,Integer> parser = (s, base, signed, multiplier) -> { try { int value = Integer.parseInt(s, base); value = signed ? value : Math.abs(value); return (int)(value * multiplier); } catch (NumberFormatException e) { throw new ParseException("Invalid number: " + s + " in base " + base, 0); } }; try { int value = parser.applyThrows("123", 10, true, 2.0); } catch (ParseException e) { // Handle checked exception }

      Parameters:
      a - The first function argument.
      b - The second function argument.
      c - The third function argument.
      d - The fourth function argument.
      Returns:
      The function result.
      Throws:
      Exception - If an error occurs during function execution.