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) {
        try {
            runnable.run();
        } catch (Exception e) {
            throw rethrow(e);
        }
    }

    public static <T> T call(Callable<T> callable) {
        try {
            return callable.call();
        } catch (Exception e) {
            throw rethrow(e);
        }
    }

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

@@ -540,32 +540,239 @@
 ble runnable) %7B%0A
+        run(runnable, null);%0A    %7D%0A%0A    public static %3CT%3E T call(Callable%3CT%3E callable) %7B%0A        return call(callable, null);%0A    %7D%0A%0A    public static void run(UncheckedRunnable runnable, Runnable finaly) %7B%0A
         try %7B%0A  
@@ -851,32 +851,114 @@
 row rethrow(e);%0A
+        %7D finally %7B%0A            if (finaly != null)%0A                finaly.run();%0A
         %7D%0A    %7D%0A
@@ -999,32 +999,49 @@
 able%3CT%3E callable
+, Runnable finaly
 ) %7B%0A        try 
@@ -1132,32 +1132,114 @@
 row rethrow(e);%0A
+        %7D finally %7B%0A            if (finaly != null)%0A                finaly.run();%0A
         %7D%0A    %7D%0A

add finally block