/*
 * Copyright 2016 The Bazel Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.idea.blaze.base.async;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.scope.Scope;
import com.google.idea.blaze.base.scope.output.IssueOutput;
import com.google.idea.blaze.base.scope.output.StatusOutput;
import com.google.idea.blaze.base.scope.scopes.TimingScope;
import com.google.idea.blaze.base.scope.scopes.TimingScope.EventType;
import com.intellij.openapi.diagnostic.Logger;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;

/** Utilities operating on futures. */
public class FutureUtil {
  /** The result of the future operation */
  public static class FutureResult<T> {
    @Nullable private final T result;
    private final boolean success;

    FutureResult(T result) {
      this.result = result;
      this.success = true;
    }

    FutureResult() {
      this.result = null;
      this.success = false;
    }

    @Nullable
    public T result() {
      return result;
    }

    public boolean success() {
      return success;
    }
  }

  /** Builder for the future */
  public static class Builder<T> {
    private static final Logger logger = Logger.getInstance(FutureUtil.class);
    private final BlazeContext context;
    private final ListenableFuture<T> future;
    private String timingCategory;
    private EventType eventType;
    private String errorMessage;
    private String progressMessage;

    Builder(BlazeContext context, ListenableFuture<T> future) {
      this.context = context;
      this.future = future;
    }

    public Builder<T> timed(String timingCategory, EventType eventType) {
      this.timingCategory = timingCategory;
      this.eventType = eventType;
      return this;
    }

    public Builder<T> withProgressMessage(String message) {
      this.progressMessage = message;
      return this;
    }

    public Builder<T> onError(String errorMessage) {
      this.errorMessage = errorMessage;
      return this;
    }

    public FutureResult<T> run() {
      return Scope.push(
          context,
          (childContext) -> {
            if (timingCategory != null) {
              childContext.push(new TimingScope(timingCategory, eventType));
            }
            if (progressMessage != null) {
              childContext.output(new StatusOutput(progressMessage));
            }
            try {
              return new FutureResult<>(future.get());
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
              context.setCancelled();
            } catch (ExecutionException e) {
              logger.error(e);
              if (errorMessage != null) {
                IssueOutput.error(errorMessage).submit(childContext);
              }
              context.setHasError();
            }
            return new FutureResult<>();
          });
    }
  }

  public static <T> Builder<T> waitForFuture(BlazeContext context, ListenableFuture<T> future) {
    return new Builder<>(context, future);
  }
}

@@ -662,67 +662,8 @@
 c;%0A%0A
-import com.google.common.util.concurrent.ListenableFuture;%0A
 impo
@@ -712,16 +712,16 @@
 ontext;%0A
+
 import c
@@ -1105,16 +1105,52 @@
 eption;%0A
+import java.util.concurrent.Future;%0A
 import j
@@ -1906,26 +1906,16 @@
 e final 
-Listenable
 Future%3CT
@@ -2092,34 +2092,24 @@
 xt context, 
-Listenable
 Future%3CT%3E fu
@@ -3500,16 +3500,16 @@
  %7D%0A  %7D%0A%0A
+
   public
@@ -3571,18 +3571,8 @@
 xt, 
-Listenable
 Futu

Convert FutureUtil to use Future instead of ListenableFuture.