Last active
April 23, 2020 14:17
-
-
Save wytrem/34f2ac134f9aa6b54600f9a643aa2408 to your computer and use it in GitHub Desktop.
Java: callback (from bungeecord) to callable.
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
public static <V> Callable<V> createCallable(Consumer<Callback<V>> callbackConsumer) { | |
return new Callable<V>() { | |
private final CountDownLatch countDownLatch = new CountDownLatch(1); | |
private V result; | |
private Throwable error; | |
public void callbackDone(V result, Throwable error) { | |
this.result = result; | |
this.error = error; | |
countDownLatch.countDown(); | |
} | |
@Override | |
public V call() throws Exception { | |
callbackConsumer.accept(this::callbackDone); | |
countDownLatch.await(); | |
if (this.error != null) { | |
throw new RuntimeException(this.error); | |
} | |
else { | |
return this.result; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment