javastrava.api.v3.rest.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
/**
*
*/
package javastrava.api.v3.rest.async;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javastrava.api.v3.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;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy