lambda exception handling for bi-functions
*/
package org.wso2.carbon.identity.core.util;
-import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.Supplier;
+import java.util.function.*;
/**
* A Utility which provides a way to throw checked exceptions from the lambda expressions.
@@ -91,6 +88,19 @@ public final class LambdaExceptionUtils {
R apply(T t) throws E;
}
+ /**
+ * Represents a {@code BiFunction} interface which can throw exceptions.
+ *
+ * @param <T> the type of the input to the function
+ * @param <U> the type of the input to the function
+ * @param <R> the type of the result of the function
+ * @param <E> the type of Exception
+ */
+ @FunctionalInterface
+ public interface BiFunctionWithExceptions<T, U, R, E extends Exception> {
+
+ R apply(T t, U u) throws E;
+ }
/**
* Represents a {@code Supplier} interface which can throw exceptions.
@@ -177,6 +187,29 @@ public final class LambdaExceptionUtils {
};
}
+ /**
+ * This method allows a BiFunction which throws exceptions to be used in places which expects a BiFunction.
+ *
+ * @param <T> Any Object.
+ * @param <U> Any Object.
+ * @param <R> Any Object.
+ * @param <E> Any Exception.
+ * @param biFunction BiFunction to apply for given arguments.
+ * @return Any Object that result from the bi-function passed.
+ */
+ public static <T, U, R, E extends Exception> BiFunction<T, U, R> rethrowFunction(
+ BiFunctionWithExceptions<T, U, R, E> biFunction) {
+
+ return (t,u) -> {
+ try {
+ return biFunction.apply(t,u);
+ } catch (Exception exception) {
+ throwAsUnchecked(exception);
+ return null;
+ }
+ };
+ }
+
/**
* This method allows a Supplier which throws exceptions to be used in places which expects a Supplier.
*
