Skip to content

Instantly share code, notes, and snippets.

@asv
Created July 24, 2015 13:00
Show Gist options
  • Save asv/7f2e7773f34d637d1c7d to your computer and use it in GitHub Desktop.
Save asv/7f2e7773f34d637d1c7d to your computer and use it in GitHub Desktop.
package helpers;
import com.google.maps.PendingResult;
import java.util.concurrent.CompletableFuture;
/**
* Helper methods for Google Maps API.
*/
public final class GoogleMapsApiHelper {
/**
* Private constructor for utility class to prevent instantiation.
*/
private GoogleMapsApiHelper() {
}
/**
* Wrap the {@link PendingResult} to {@link CompletableFuture}.
*/
public static <T> CompletableFuture<T> toCompletable(PendingResult<T> pendingResult) {
final CompletableFuture<T> future = new CompletableFuture<>();
pendingResult.setCallback(new CompletableCallback<>(future));
return future;
}
private static class CompletableCallback<T> implements PendingResult.Callback<T> {
private final CompletableFuture<T> future;
public CompletableCallback(final CompletableFuture<T> future) {
this.future = future;
}
@Override
public void onResult(final T result) {
this.future.complete(result);
}
@Override
public void onFailure(final Throwable e) {
this.future.completeExceptionally(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment