// Copyright 2017 Google LLC
//
// 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.api.graphql.execution;

import com.google.common.util.concurrent.ListenableFuture;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.NoOpInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.schema.DataFetcher;
import net.javacrumbs.futureconverter.java8guava.FutureConverter;

public final class GuavaListenableFutureSupport {
  private GuavaListenableFutureSupport() {}

  /**
   * Converts a {@link ListenableFuture} to a Java8 {@link java.util.concurrent.CompletableFuture}.
   *
   * <p>{@see CompletableFuture} is supported by the provided {@link
   * graphql.execution.AsyncExecutionStrategy}.
   *
   * <p>Note: This should be installed before any other instrumentation.
   */
  public static Instrumentation listenableFutureInstrumentation() {
    return new NoOpInstrumentation() {
      @Override
      public DataFetcher<?> instrumentDataFetcher(
          DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
        return (DataFetcher<Object>)
            dataFetchingEnvironment -> {
              Object data = dataFetcher.get(dataFetchingEnvironment);
              if (data instanceof ListenableFuture) {
                return FutureConverter.<Object>toCompletableFuture((ListenableFuture<Object>) data);
              }
              return data;
            };
      }
    };
  }
}

@@ -666,32 +666,195 @@
 current.
-ListenableFuture
+FutureCallback;%0Aimport com.google.common.util.concurrent.Futures;%0Aimport com.google.common.util.concurrent.ListenableFuture;%0Aimport com.google.common.util.concurrent.MoreExecutors
 ;%0Aimport
@@ -1102,68 +1102,89 @@
 ort 
-net.javacrumbs.futureconverter.java8guava.FutureConverter
+java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.Executors
 ;%0A%0A
+%0A
 publ
@@ -2070,84 +2070,253 @@
     
-return FutureConverter.%3CObject%3EtoCompletableFuture((ListenableFuture
+ListenableFuture%3CObject%3E listenableFuture = (ListenableFuture%3CObject%3E) data;%0A                CompletableFuture%3CObject%3E completableFuture= new CompletableFuture%3C%3E();%0A                Futures.addCallback(listenableFuture, new FutureCallback
 %3CObject%3E
 ) da
@@ -2315,15 +2315,434 @@
 ect%3E
+(
 ) 
-data)
+%7B%0A                  @Override%0A                  public void onSuccess(Object result) %7B%0A                    completableFuture.complete(result);%0A                  %7D%0A%0A                  @Override%0A                  public void onFailure(Throwable t) %7B%0A                    completableFuture.completeExceptionally(t);%0A                  %7D%0A                %7D, MoreExecutors.directExecutor());%0A                return completableFuture
 ;%0A  

Remove dependency on futureconverter library