Class ExceptionalFunctionUtils


  • public abstract class ExceptionalFunctionUtils
    extends Object
    Utility class for functional extensions
    Author:
    Peter Kurfer
    • Method Detail

      • tryWithResources

        public static <T,​R extends AutoCloseable> T tryWithResources​(ExceptionalResourceSupplier<R> resourceSupplier,
                                                                           ExceptionalFunction<R,​T,​Exception> consumer,
                                                                           Supplier<T> fallbackSupplier)
        Functional try-with-resources
        Type Parameters:
        T - Type of the result
        R - Type of the AutoCloseable resource
        Parameters:
        resourceSupplier - supplier for the AutoCloseable resource
        consumer - function that consumes the resource and returns a value
        fallbackSupplier - supplier for a fallback value
        Returns:
        instance of T supplied by the consumer or fallback value if exception occurred
      • tryWithResources

        public static <T,​R extends AutoCloseable> T tryWithResources​(ExceptionalResourceSupplier<R> resourceSupplier,
                                                                           ExceptionalFunction<R,​T,​Exception> consumer,
                                                                           RuntimeExceptionFunction runtimeExceptionFunction)
        Functional try-with-resources with exception translator function
        Type Parameters:
        T - Type of the result
        R - Type of the AutoCloseable resource
        Parameters:
        resourceSupplier - supplier for the AutoCloseable resource
        consumer - function that consumes the resource and returns a value
        runtimeExceptionFunction - function that converts Exception to RuntimeException
        Returns:
        instance of T supplied by the consumer
      • tryWithResources

        public static <T,​R extends AutoCloseable> T tryWithResources​(ExceptionalResourceSupplier<R> resourceSupplier,
                                                                           ExceptionalFunction<R,​T,​Exception> consumer)
        Functional try-with-resources with default exception translator function
        Type Parameters:
        T - Type of the result
        R - Type of the AutoCloseable resource
        Parameters:
        resourceSupplier - supplier for the AutoCloseable resource
        consumer - function that consumes the resource and returns a value
        Returns:
        instance of T supplied by the consumer