Created
July 24, 2015 13:00
-
-
Save asv/7f2e7773f34d637d1c7d to your computer and use it in GitHub Desktop.
This file contains 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 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