Last active
August 29, 2015 14:09
-
-
Save kevints/9c746e2583a04dfe9bb5 to your computer and use it in GitHub Desktop.
Finagle version of guava's Futures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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