All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flowthings.client.api.FlowthingsFuture Maven / Gradle / Ivy

There is a newer version: 0.9.3.2
Show newest version
package com.flowthings.client.api;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.flowthings.client.exception.AsyncException;
import com.flowthings.client.exception.FlowthingsException;
import com.google.common.util.concurrent.Futures;

/**
 * Created by matt on 7/5/16.
 */
public class FlowthingsFuture implements Future {

  private final Future inner;

  public FlowthingsFuture(Future inner){
    this.inner = inner;
  }

  public static  FlowthingsFuture fromResult(S result){
    return new FlowthingsFuture<>(Futures.immediateFuture(result));
  }
  public static  FlowthingsFuture fromException(FlowthingsException e){
    return new FlowthingsFuture<>(Futures.immediateFailedFuture(e));
  }

  @Override
  public boolean cancel(boolean mayInterruptIfRunning) {
    return inner.cancel(mayInterruptIfRunning);
  }

  @Override
  public boolean isCancelled() {
    return inner.isCancelled();
  }

  @Override
  public boolean isDone() {
    return inner.isDone();
  }

  @Override
  public T get() throws InterruptedException, ExecutionException {
    return inner.get();
  }

  @Override
  public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    return inner.get(timeout, unit);
  }

  /**
   * Works like "get", but returns a sane exception type
   */
  public T grab() throws FlowthingsException {
    try {
      return this.get();
    } catch (InterruptedException e) {
      throw new AsyncException(e);
    } catch (ExecutionException e) {
      Throwable cause = e.getCause();
      if (cause instanceof FlowthingsException){
        throw (FlowthingsException) cause;
      } else {
        throw new FlowthingsException(cause);
      }
    }
  }
}