Skip to content

Commit

Permalink
[hotfix] Add BiFunctionWithException#unchecked to convert into BiFunc…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
tillrohrmann committed Sep 14, 2018
1 parent 4942b95 commit 6feabbc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @param <E> type of the exception which can be thrown
*/
@FunctionalInterface
public interface BiFunctionWithException<T, U, R, E extends Throwable> extends BiFunction<T, U, R> {
public interface BiFunctionWithException<T, U, R, E extends Throwable> {

/**
* Apply the given values t and u to obtain the resulting value. The operation can
Expand All @@ -42,16 +42,25 @@ public interface BiFunctionWithException<T, U, R, E extends Throwable> extends B
* @return result value
* @throws E if the operation fails
*/
R applyWithException(T t, U u) throws E;
R apply(T t, U u) throws E;

default R apply(T t, U u) {
try {
return applyWithException(t, u);
} catch (Throwable e) {
ExceptionUtils.rethrow(e);
// we have to return a value to please the compiler
// but we will never reach the code here
return null;
}
/**
* Convert at {@link BiFunctionWithException} into a {@link BiFunction}.
*
* @param biFunctionWithException function with exception to convert into a function
* @param <A> input type
* @param <B> output type
* @return {@link BiFunction} which throws all checked exception as an unchecked exception.
*/
static <A, B, C> BiFunction<A, B, C> unchecked(BiFunctionWithException<A, B, C, ?> biFunctionWithException) {
return (A a, B b) -> {
try {
return biFunctionWithException.apply(a, b);
} catch (Throwable t) {
ExceptionUtils.rethrow(t);
// we need this to appease the compiler :-(
return null;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ public void grantLeadership(final UUID newLeaderSessionID) {

final CompletableFuture<Void> confirmationFuture = fencingTokenFuture.thenCombineAsync(
recoveredJobsFuture,
(BiFunctionWithException<Boolean, Collection<JobGraph>, Void, Exception>) (Boolean confirmLeadership, Collection<JobGraph> recoveredJobs) -> {
BiFunctionWithException.unchecked((Boolean confirmLeadership, Collection<JobGraph> recoveredJobs) -> {
if (confirmLeadership) {
leaderElectionService.confirmLeaderSessionID(newLeaderSessionID);
} else {
Expand All @@ -814,7 +814,7 @@ public void grantLeadership(final UUID newLeaderSessionID) {
}
}
return null;
},
}),
getRpcService().getExecutor());

confirmationFuture.whenComplete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public synchronized SubmittedJobGraph recoverJobGraph(JobID jobId) throws Except
verifyIsStarted();

if (recoverJobGraphFunction != null) {
return recoverJobGraphFunction.applyWithException(jobId, storedJobs);
return recoverJobGraphFunction.apply(jobId, storedJobs);
} else {
return requireNonNull(
storedJobs.get(jobId),
Expand Down

0 comments on commit 6feabbc

Please sign in to comment.