Skip to content

Instantly share code, notes, and snippets.

@wytrem
Last active April 23, 2020 14:17
Show Gist options
  • Save wytrem/34f2ac134f9aa6b54600f9a643aa2408 to your computer and use it in GitHub Desktop.
Save wytrem/34f2ac134f9aa6b54600f9a643aa2408 to your computer and use it in GitHub Desktop.
Java: callback (from bungeecord) to callable.
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