package com.wizzardo.tools.misc;

import java.util.concurrent.Callable;

/**
 * @author: wizzardo
 * Date: 08.12.14
 */
public class Unchecked {

    @SuppressWarnings("unchecked")
    private static <T extends Exception> void throwsUnchecked(Exception toThrow) throws T {
        throw (T) toThrow;
    }

    public static RuntimeException rethrow(Exception ex) {
        Unchecked.<RuntimeException>throwsUnchecked(ex);

        return new IllegalStateException("unreachable statement");
    }

    public static void run(UncheckedRunnable runnable) {
        run(runnable, null);
    }

    public static <T> T call(Callable<T> callable) {
        return call(callable, null);
    }

    public static void run(UncheckedRunnable runnable, Runnable finaly) {
        try {
            runnable.run();
        } catch (Exception e) {
            throw rethrow(e);
        } finally {
            if (finaly != null)
                finaly.run();
        }
    }

    public static <T> T call(Callable<T> callable, Runnable finaly) {
        try {
            return callable.call();
        } catch (Exception e) {
            throw rethrow(e);
        } finally {
            if (finaly != null)
                finaly.run();
        }
    }

    public interface UncheckedRunnable {
        void run() throws Exception;
    }
}

@@ -27,16 +27,70 @@
 .misc;%0A%0A
+import java.io.Closeable;%0Aimport java.io.IOException;%0A
 import j
@@ -1308,55 +1308,497 @@
 lic 
-interface UncheckedRunnable %7B%0A        void run(
+static %3CT, C extends Closeable%3E T call(C closeable, Consumer%3CC, T%3E consumer) %7B%0A        try %7B%0A            return consumer.call(closeable);%0A        %7D catch (Exception e) %7B%0A            throw rethrow(e);%0A        %7D finally %7B%0A            try %7B%0A                closeable.close();%0A            %7D catch (IOException ignored) %7B%0A            %7D%0A        %7D%0A    %7D%0A%0A    public interface UncheckedRunnable %7B%0A        void run() throws Exception;%0A    %7D%0A%0A    public interface Consumer%3CT, R%3E %7B%0A        R call(T t
 ) th

implement call for Closeable