1/*
2 * Copyright (C) 2015 Google, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package dagger.producers.internal;
17
18import com.google.common.base.Function;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.util.concurrent.Futures;
21import com.google.common.util.concurrent.ListenableFuture;
22import dagger.producers.Producer;
23import dagger.producers.monitoring.ProducerMonitor;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Set;
27
28/**
29 * A {@link Producer} implementation used to implement {@link Set} bindings. This producer returns
30 * a future {@link Set} whose elements are populated by subsequent calls to the delegate
31 * {@link Producer#get} methods.
32 *
33 * @author Jesse Beder
34 * @since 2.0
35 */
36public final class SetProducer<T> extends AbstractProducer<Set<T>> {
37  /**
38   * Returns a new producer that creates {@link Set} futures from the union of the given
39   * {@link Producer} instances.
40   */
41  @SafeVarargs
42  public static <T> Producer<Set<T>> create(Producer<Set<T>>... producers) {
43    return new SetProducer<T>(ImmutableSet.copyOf(producers));
44  }
45
46  private final Set<Producer<Set<T>>> contributingProducers;
47
48  private SetProducer(Set<Producer<Set<T>>> contributingProducers) {
49    super();
50    this.contributingProducers = contributingProducers;
51  }
52
53  /**
54   * Returns a future {@link Set} whose iteration order is that of the elements given by each of the
55   * producers, which are invoked in the order given at creation.
56   *
57   * <p>If any of the delegate sets, or any elements therein, are null, then this future will fail
58   * with a NullPointerException.
59   *
60   * <p>Canceling this future will attempt to cancel all of the component futures, and if any of the
61   * delegate futures fails or is canceled, this one is, too.
62   *
63   * @throws NullPointerException if any of the delegate producers return null
64   */
65  @Override
66  public ListenableFuture<Set<T>> compute(ProducerMonitor unusedMonitor) {
67    List<ListenableFuture<Set<T>>> futureSets =
68        new ArrayList<ListenableFuture<Set<T>>>(contributingProducers.size());
69    for (Producer<Set<T>> producer : contributingProducers) {
70      ListenableFuture<Set<T>> futureSet = producer.get();
71      if (futureSet == null) {
72        throw new NullPointerException(producer + " returned null");
73      }
74      futureSets.add(futureSet);
75    }
76    return Futures.transform(Futures.allAsList(futureSets), new Function<List<Set<T>>, Set<T>>() {
77      @Override public Set<T> apply(List<Set<T>> sets) {
78        ImmutableSet.Builder<T> builder = ImmutableSet.builder();
79        for (Set<T> set : sets) {
80          builder.addAll(set);
81        }
82        return builder.build();
83      }
84    });
85  }
86}
87