javastrava.api.async.StravaAPIFuture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javastrava-api Show documentation
Show all versions of javastrava-api Show documentation
Java implementation of the Strava API
The newest version!
package javastrava.api.async;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javastrava.service.exception.StravaUnknownAPIException;
/**
*
* Wrapper class for handling exceptions thrown by the API via a {@link CompletableFuture}
*
* @author Dan Shannon
* @param Class of object which will be returned by the future
*
*/
public class StravaAPIFuture {
/**
* Wrapped future
*/
private final CompletableFuture future;
/**
* No argument constructor provides the wrapped future
*/
public StravaAPIFuture() {
this.future = new CompletableFuture();
}
/**
* @param result The object to return via the future
*/
public void complete(final T result) {
this.future.complete(result);
}
/**
* @param cause Cause of the exceptional completion
*/
public void completeExceptionally(final Throwable cause) {
this.future.completeExceptionally(cause);
}
/**
* Wrapper for the {@link CompletableFuture#get()} method handles exceptions and maps to javastrava exceptions
* @return The object asked for
*/
public T get() {
T result = null;
try {
result = this.future.get();
} catch (final ExecutionException e) {
throw (RuntimeException) e.getCause();
} catch (final CancellationException e) {
throw new StravaUnknownAPIException(null, null, e);
} catch (InterruptedException e) {
throw new StravaUnknownAPIException(null, null, e);
}
return result;
}
}