/**
 * 
 */
package com.github.phantomthief.util;

import com.google.common.base.Throwables;

/**
 * @author w.vela
 */
public class MoreFunctions {

    public interface FunctionWithThrowable<T, R, X extends Throwable> {

        R apply(T t) throws X;
    }

    public static final <T, R> R catching(FunctionWithThrowable<T, R, Throwable> function, T t) {
        return catching(function, t, (Class<Throwable>) null);
    }

    @SafeVarargs
    public static final <T, R> R catching(FunctionWithThrowable<T, R, Throwable> function, T t,
            Class<? extends Throwable>... catchThrowables) {
        try {
            return function.apply(t);
        } catch (Throwable e) {
            if (catchThrowables != null) {
                for (Class<? extends Throwable> throwable : catchThrowables) {
                    if (throwable != null) {
                        if (throwable.isInstance(e)) {
                            continue;
                        } else {
                            throw Throwables.propagate(e);
                        }
                    }
                }
            }
            return null;
        }
    }
}

@@ -44,16 +44,55 @@
 .util;%0A%0A
+import java.util.concurrent.Callable;%0A%0A
 import c
@@ -289,24 +289,849 @@
 s X;%0A    %7D%0A%0A
+    public static final %3CR%3E R catching(Callable%3CR%3E callable) %7B%0A        return catching(callable, (Class%3CThrowable%3E) null);%0A    %7D%0A%0A    @SafeVarargs%0A    public static final %3CR%3E R catching(Callable%3CR%3E callable,%0A            Class%3C? extends Throwable%3E... catchThrowables) %7B%0A        try %7B%0A            return callable.call();%0A        %7D catch (Throwable e) %7B%0A            if (catchThrowables != null) %7B%0A                for (Class%3C? extends Throwable%3E throwable : catchThrowables) %7B%0A                    if (throwable != null) %7B%0A                        if (throwable.isInstance(e)) %7B%0A                            continue;%0A                        %7D else %7B%0A                            throw Throwables.propagate(e);%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A
     public s

add catching callable.