Class LambdaUnchecker


  • public final class LambdaUnchecker
    extends Object
    Utility class to hide checked exceptions. Thanks to:
    • https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams#27668305
    • http://www.philandstuff.com/2012/04/28/sneakily-throwing-checked-exceptions.html http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html
    • https://softwareengineering.stackexchange.com/questions/225931/workaround-for-java-checked-exceptions?newreg=ddf0dd15e8174af8ba52e091cf85688e
    • https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java-8-streams
    Since:
    1.1.0
    Author:
    not sure
    • Constructor Detail

      • LambdaUnchecker

        public LambdaUnchecker()
    • Method Detail

      • uncheckedConsumer

        public static <T,​E extends ExceptionConsumer<T> uncheckedConsumer​(LambdaUnchecker.ConsumerWithExceptions<T,​E> _consumer)
        Wraps consumer lambda returning the consumer but hacking the compiler to avoid compilation check. Example: .forEach(uncheckedConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(uncheckedConsumer(ClassNameUtil::println));
        Type Parameters:
        T - consumer input type
        E - checked exception
        Parameters:
        _consumer - consumer to wrap
        Returns:
        wrapped consumer that throws the exceptions produced without compilation check
        See Also:
        LambdaUnchecker.ConsumerWithExceptions
      • silencedConsumer

        public static <T,​E extends ExceptionConsumer<T> silencedConsumer​(LambdaUnchecker.ConsumerWithExceptions<T,​E> _consumer)
        Wraps consumer lambda returning the same one but silencing the exception Example: .forEach(silencedConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(silencedConsumer(ClassNameUtil::println));
        Type Parameters:
        T - consumer input type
        E - checked exception
        Parameters:
        _consumer - consumer to wrap
        Returns:
        wrapped consumer that silence the exceptions produced without compilation check
        See Also:
        LambdaUnchecker.ConsumerWithExceptions
      • uncheckedAccept

        public static <T,​E extends Exception> void uncheckedAccept​(LambdaUnchecker.ConsumerWithExceptions<T,​E> _consumer,
                                                                         T _input)
        Accept consumer input but hacking the compiler to avoid compilation check. Example: uncheckedAccept(name -> System.out.println(Class.forName(name)),"java.util.List");
        Type Parameters:
        T - consumer input type
        E - checked exception
        Parameters:
        _consumer - consumer to wrap
        _input - consumer input
        See Also:
        LambdaUnchecker.ConsumerWithExceptions
      • silencedAccept

        public static <T,​E extends Exception> void silencedAccept​(LambdaUnchecker.ConsumerWithExceptions<T,​E> _consumer,
                                                                        T _input)
        Accept consumer input but silencing the exception Example: uncheckedAccept(name -> System.out.println(Class.forName(name)),"java.util.List");
        Type Parameters:
        T - consumer input type
        E - checked exception
        Parameters:
        _consumer - consumer to wrap
        _input - consumer input
        See Also:
        LambdaUnchecker.ConsumerWithExceptions
      • uncheckedBiConsumer

        public static <T,​U,​E extends ExceptionBiConsumer<T,​U> uncheckedBiConsumer​(LambdaUnchecker.BiConsumerWithExceptions<T,​U,​E> _biConsumer)
        Wraps biconsumer lambda returning the same one but hacking the compiler to avoid compilation check. Example: .forEach(uncheckedBiConsumer((name,value) -> System.out.println(Class.forName(name)+value)));
        Type Parameters:
        T - biconsumer first input type
        U - biconsumer second input type
        E - checked exception
        Parameters:
        _biConsumer - biconsumer to wrap
        Returns:
        wrapped biconsumer that throws the exceptions produced without compilation check
        See Also:
        BiConsumer, LambdaUnchecker.BiConsumerWithExceptions
      • silencedBiConsumer

        public static <T,​U,​E extends ExceptionBiConsumer<T,​U> silencedBiConsumer​(LambdaUnchecker.BiConsumerWithExceptions<T,​U,​E> _biConsumer)
        Wraps biconsumer lambda returning the same one but returning null when exception and silencing the exception Example: .forEach(silencedBiConsumer((name,value) -> System.out.println(Class.forName(name)+value)));
        Type Parameters:
        T - biconsumer first input type
        U - biconsumer second input type
        E - checked exception
        Parameters:
        _biConsumer - biconsumer to wrap
        Returns:
        wrapped biconsumer that silence the exceptions produced without compilation check
        See Also:
        BiConsumer, LambdaUnchecker.BiConsumerWithExceptions
      • uncheckedAccept

        public static <T,​U,​E extends Exception> void uncheckedAccept​(LambdaUnchecker.BiConsumerWithExceptions<T,​U,​E> _biConsumer,
                                                                                 T _firstInput,
                                                                                 U _secondInput)
        Accept consumer input but hacking the compiler to avoid compilation check. Example: uncheckedAccept((name,value) -> System.out.println(Class.forName(name)+value));
        Type Parameters:
        T - biconsumer first input type
        U - biconsumer second input type
        E - checked exception
        Parameters:
        _biConsumer - biconsumer to wrap
        _firstInput - biconsumer first input
        _secondInput - biconsumer second input
        See Also:
        BiConsumer, LambdaUnchecker.BiConsumerWithExceptions
      • silencedAccept

        public static <T,​U,​E extends Exception> void silencedAccept​(LambdaUnchecker.BiConsumerWithExceptions<T,​U,​E> _biConsumer,
                                                                                T _firstInput,
                                                                                U _secondInput)
        Accept consumer input but silencing the exception Example: silencedAccept((name,value) -> System.out.println(Class.forName(name)+value));
        Type Parameters:
        T - biconsumer first input type
        U - biconsumer second input type
        E - checked exception
        Parameters:
        _biConsumer - biconsumer to wrap
        _firstInput - biconsumer first input
        _secondInput - biconsumer second input
        See Also:
        BiConsumer, LambdaUnchecker.BiConsumerWithExceptions
      • uncheckedFunction

        public static <T,​R,​E extends ExceptionFunction<T,​R> uncheckedFunction​(LambdaUnchecker.FunctionWithExceptions<T,​R,​E> _function)
        Wraps function lambda returning the same one but hacking the compiler to avoid compilation check. Example: .map(uncheckedFunction(name -> Class.forName(name))) or .map(uncheckedFunction(Class::forName))
        Type Parameters:
        T - function input type
        R - function result type
        E - checked exception
        Parameters:
        _function - biconsumer to wrap
        Returns:
        wrapped function that throws the exceptions produced without compilation check
        See Also:
        Function, LambdaUnchecker.FunctionWithExceptions
      • silencedFunction

        public static <T,​R,​E extends ExceptionFunction<T,​R> silencedFunction​(LambdaUnchecker.FunctionWithExceptions<T,​R,​E> _function)
        Wraps function lambda returning the same one but returning null when exception and silencing the exception Example: .map(silencedFunction(name -> Class.forName(name))) or .map(silencedFunction(Class::forName))
        Type Parameters:
        T - function input type
        R - function result type
        E - checked exception
        Parameters:
        _function - biconsumer to wrap
        Returns:
        wrapped function that silence the exceptions produced without compilation check
        See Also:
        Function, LambdaUnchecker.FunctionWithExceptions
      • uncheckedApply

        public static <T,​R,​E extends Exception> R uncheckedApply​(LambdaUnchecker.FunctionWithExceptions<T,​R,​E> _function,
                                                                             T _input)
        Apply function returning the result but hacking the compiler to avoid compilation check. Example: uncheckedFunction(name -> Class.forName(name)) or uncheckedFunction(Class::forName)
        Type Parameters:
        T - function input type
        R - function result type
        E - checked exception
        Parameters:
        _function - biconsumer to wrap
        _input - function input
        Returns:
        function result
        See Also:
        Function, LambdaUnchecker.FunctionWithExceptions
      • silencedApply

        public static <T,​R,​E extends Exception> R silencedApply​(LambdaUnchecker.FunctionWithExceptions<T,​R,​E> _function,
                                                                            T _input)
        Apply function returning the result but returning null when exception and silencing the exception and in this case returning null Example: silencedFunction(name -> Class.forName(name)) or silencedFunction(Class::forName)
        Type Parameters:
        T - function input type
        R - function result type
        E - checked exception
        Parameters:
        _function - biconsumer to wrap
        _input - function input
        Returns:
        function result or null if exception
        See Also:
        Function, LambdaUnchecker.FunctionWithExceptions
      • uncheckedSupplier

        public static <T,​E extends ExceptionSupplier<T> uncheckedSupplier​(LambdaUnchecker.SupplierWithExceptions<T,​E> _supplier)
        Wraps supplier lambda returning the same one but hacking the compiler to avoid compilation check. Example: uncheckedSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),
        Type Parameters:
        T - supplier result
        E - checked exception
        Parameters:
        _supplier - supplier to wrap
        Returns:
        wrapped function that throws the exceptions produced without compilation check
        See Also:
        Supplier, LambdaUnchecker.SupplierWithExceptions
      • silencedSupplier

        public static <T,​E extends ExceptionSupplier<T> silencedSupplier​(LambdaUnchecker.SupplierWithExceptions<T,​E> _supplier)
        Wraps supplier lambda returning the same one but returning null when exception and silencing the exception Example: silencedSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),
        Type Parameters:
        T - supplier result
        E - checked exception
        Parameters:
        _supplier - supplier to wrap
        Returns:
        wrapped function that silence the exceptions produced without compilation check
        See Also:
        Supplier, LambdaUnchecker.SupplierWithExceptions
      • uncheckedGet

        public static <T,​E extends Exception> T uncheckedGet​(LambdaUnchecker.SupplierWithExceptions<T,​E> _supplier)
        Get supplier returning the supplier result but hacking the compiler to avoid compilation check. Example: uncheckedGet(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8")))
        Type Parameters:
        T - supplier result
        E - checked exception
        Parameters:
        _supplier - supplier to wrap
        Returns:
        supplier result
        See Also:
        Supplier, LambdaUnchecker.SupplierWithExceptions
      • silencedGet

        public static <T,​E extends Exception> T silencedGet​(LambdaUnchecker.SupplierWithExceptions<T,​E> _supplier)
        Get supplier returning the supplier result or null if exception Example: silencedGet(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8")))
        Type Parameters:
        T - supplier result
        E - checked exception
        Parameters:
        _supplier - supplier to wrap
        Returns:
        supplier result or get if exception
        See Also:
        Supplier, LambdaUnchecker.SupplierWithExceptions
      • uncheckedRunnable

        public static Runnable uncheckedRunnable​(LambdaUnchecker.RunnableWithExceptions _runnable)
        Wraps Runnable lambda returning the same one but hacking the compiler to avoid compilation check. Example: uncheckedSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),
        Parameters:
        _runnable - runnable to wrap
        Returns:
        wrapped runnable that throws the exceptions produced without compilation check
        See Also:
        Runnable, LambdaUnchecker.RunnableWithExceptions
      • silencedRunnable

        public static Runnable silencedRunnable​(LambdaUnchecker.RunnableWithExceptions _runnable)
        Wraps Runnable lambda returning the same one but returning null when exception and silencing the exception Example: silencedSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))),
        Parameters:
        _runnable - runnable to wrap
        Returns:
        wrapped runnable that silence the exceptions produced without compilation check
        See Also:
        Runnable, LambdaUnchecker.RunnableWithExceptions