/*
* Copyright (C) 2014 The Dagger Authors.
*
* 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 dagger.producers.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.Futures.catchingAsync;
import static com.google.common.util.concurrent.Futures.transform;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import dagger.producers.Produced;
import dagger.producers.Producer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Provider;
/**
* Utility methods for use in generated producer code.
*/
public final class Producers {
/**
* Returns a future of {@link Produced} that represents the completion (either success or failure)
* of the given future. If the input future succeeds, then the resulting future also succeeds with
* a successful {@code Produced}; if the input future fails, then the resulting future succeeds
* with a failing {@code Produced}.
*
*
Cancelling the resulting future will propagate the cancellation to the input future; but
* cancelling the input future will trigger the resulting future to succeed with a failing
* {@code Produced}.
*/
// TODO(beder): Document what happens with an InterruptedException after you figure out how to
// trigger one in a test.
public static ListenableFuture> createFutureProduced(ListenableFuture future) {
return catchingAsync(
transform(future, Producers.resultToProduced(), directExecutor()),
Throwable.class,
Producers.futureFallbackForProduced(),
directExecutor());
}
private static final Function