Skip to content

Instantly share code, notes, and snippets.

@kevints
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevints/9c746e2583a04dfe9bb5 to your computer and use it in GitHub Desktop.
Save kevints/9c746e2583a04dfe9bb5 to your computer and use it in GitHub Desktop.
Finagle version of guava's Futures
package com.github.kevints.finaglefutures;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.google.common.base.Throwables;
import com.twitter.util.Await;
import com.twitter.util.Duration;
import com.twitter.util.Future;
public final class FinagleFutures {
public static <V, X extends Exception> V get(Future<V> future, Class<X> exceptionClass) throws X {
try {
return Await.result(future);
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e, exceptionClass);
throw Throwables.propagate(e);
}
}
public static <V, X extends Exception> V get(
Future<V> future,
long timeout,
TimeUnit unit,
Class<X> exceptionClass) throws TimeoutException, InterruptedException, X {
Await.ready(future, Duration.apply(timeout, unit));
try {
return Await.result(future);
} catch (Exception e) {
Throwables.propagateIfPossible(e, exceptionClass);
throw Throwables.propagate(e);
}
}
public static <V> V getUnchecked(Future<V> future) {
try {
return Await.result(future);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment