/*
* Copyright 2015 Petr Bouda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joyrest.utils;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public final class ExceptionUtils {
public static <T> Consumer<T> rethrowConsumer(Consumer_WithExceptions<T> consumer) {
return t -> {
try {
consumer.accept(t);
} catch (Exception exception) {
throwAsUnchecked(exception);
}
};
}
public static <T, R> Function<T, R> rethrowFuction(Function_WithExceptions<T, R> function) {
return t -> {
try {
return function.apply(t);
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
};
}
public static <T> Supplier<T> rethrow(Supplier_WithExceptions<T> function) {
return () -> {
try {
return function.get();
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
};
}
public static void uncheck(Runnable_WithExceptions t) {
try {
t.accept();
} catch (Exception exception) {
throwAsUnchecked(exception);
}
}
public static <R> R uncheck(Supplier_WithExceptions<R> supplier) {
try {
return supplier.get();
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
}
public static <T, R> R uncheck(Function_WithExceptions<T, R> function, T t) {
try {
return function.apply(t);
} catch (Exception exception) {
throwAsUnchecked(exception);
return null;
}
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAsUnchecked(Exception exception) throws E {
throw (E) exception;
}
@FunctionalInterface
public interface Consumer_WithExceptions<T> {
void accept(T t) throws Exception;
}
@FunctionalInterface
public interface Function_WithExceptions<T, R> {
R apply(T t) throws Exception;
}
@FunctionalInterface
public interface Supplier_WithExceptions<T> {
T get() throws Exception;
}
@FunctionalInterface
public interface Runnable_WithExceptions {
void accept() throws Exception;
}
}