/*
 * Copyright (C) 2015 Google, Inc.
 *
 * 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 com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import dagger.producers.Producer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * A {@link Producer} implementation used to implement {@link Set} bindings. This producer returns
 * a future {@link Set} whose elements are populated by subsequent calls to the delegate
 * {@link Producer#get} methods.
 *
 * @author Jesse Beder
 * @since 2.0
 */
public final class SetProducer<T> extends AbstractProducer<Set<T>> {
  /**
   * Returns a new producer that creates {@link Set} futures from the union of the given
   * {@link Producer} instances.
   */
  public static <T> Producer<Set<T>> create(
      @SuppressWarnings("unchecked") Producer<Set<T>>... producers) {
    return new SetProducer<T>(ImmutableSet.copyOf(producers));
  }

  private final Set<Producer<Set<T>>> contributingProducers;

  private SetProducer(Set<Producer<Set<T>>> contributingProducers) {
    this.contributingProducers = contributingProducers;
  }

  /**
   * Returns a future {@link Set} whose iteration order is that of the elements given by each of the
   * producers, which are invoked in the order given at creation.
   *
   * <p>If any of the delegate sets, or any elements therein, are null, then this future will fail
   * with a NullPointerException.
   *
   * <p>Canceling this future will attempt to cancel all of the component futures, and if any of the
   * delegate futures fails or is canceled, this one is, too.
   *
   * @throws NullPointerException if any of the delegate producers return null
   */
  @Override
  public ListenableFuture<Set<T>> compute() {
    List<ListenableFuture<Set<T>>> futureSets = new ArrayList<>(contributingProducers.size());
    for (Producer<Set<T>> producer : contributingProducers) {
      ListenableFuture<Set<T>> futureSet = producer.get();
      if (futureSet == null) {
        throw new NullPointerException(producer + " returned null");
      }
      futureSets.add(futureSet);
    }
    return Futures.transform(Futures.allAsList(futureSets), new Function<List<Set<T>>, Set<T>>() {
      @Override public Set<T> apply(List<Set<T>> sets) {
        ImmutableSet.Builder<T> builder = new ImmutableSet.Builder<>();
        for (Set<T> set : sets) {
          builder.addAll(set);
        }
        return builder.build();
      }
    });
  }
}

@@ -2450,16 +2450,24 @@
 reSets =
+%0A       
  new Arr
@@ -2473,16 +2473,40 @@
 rayList%3C
+ListenableFuture%3CSet%3CT%3E%3E
 %3E(contri
@@ -2995,20 +2995,16 @@
 uilder =
- new
  Immutab
@@ -3013,17 +3013,15 @@
 Set.
-B
+b
 uilder
-%3C%3E
 ();%0A

Fix a discovered java7ism in the producers' runtime lib which may be depended on by android targets who use dagger producers. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=88283966