Coverage Summary for Class: CollectionFuture (com.google.common.util.concurrent)

Class Method, % Line, %
CollectionFuture 0% (0/4) 0% (0/15)
CollectionFuture$ListFuture 0% (0/2) 0% (0/7)
CollectionFuture$Present 0% (0/1) 0% (0/2)
Total 0% (0/7) 0% (0/24)


1 /* 2  * Copyright (C) 2006 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.util.concurrent; 16  17 import static com.google.common.collect.Lists.newArrayListWithCapacity; 18 import static java.util.Collections.unmodifiableList; 19  20 import com.google.common.annotations.GwtCompatible; 21 import com.google.common.collect.ImmutableCollection; 22 import com.google.common.collect.Lists; 23 import java.util.Collections; 24 import java.util.List; 25 import javax.annotation.CheckForNull; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** Aggregate future that collects (stores) results of each future. */ 29 @GwtCompatible(emulated = true) 30 @ElementTypesAreNonnullByDefault 31 abstract class CollectionFuture<V extends @Nullable Object, C extends @Nullable Object> 32  extends AggregateFuture<V, C> { 33  /* 34  * We access this field racily but safely. For discussion of a similar situation, see the comments 35  * on the fields of TimeoutFuture. This field is slightly different than the fields discussed 36  * there: cancel() never reads this field, only writes to it. That makes the race here completely 37  * harmless, rather than just 99.99% harmless. 38  */ 39  @CheckForNull private List<@Nullable Present<V>> values; 40  41  CollectionFuture( 42  ImmutableCollection<? extends ListenableFuture<? extends V>> futures, 43  boolean allMustSucceed) { 44  super(futures, allMustSucceed, true); 45  46  List<@Nullable Present<V>> values = 47  futures.isEmpty() 48  ? Collections.<@Nullable Present<V>>emptyList() 49  : Lists.<@Nullable Present<V>>newArrayListWithCapacity(futures.size()); 50  51  // Populate the results list with null initially. 52  for (int i = 0; i < futures.size(); ++i) { 53  values.add(null); 54  } 55  56  this.values = values; 57  } 58  59  @Override 60  final void collectOneValue(int index, @ParametricNullness V returnValue) { 61  List<@Nullable Present<V>> localValues = values; 62  if (localValues != null) { 63  localValues.set(index, new Present<>(returnValue)); 64  } 65  } 66  67  @Override 68  final void handleAllCompleted() { 69  List<@Nullable Present<V>> localValues = values; 70  if (localValues != null) { 71  set(combine(localValues)); 72  } 73  } 74  75  @Override 76  void releaseResources(ReleaseResourcesReason reason) { 77  super.releaseResources(reason); 78  this.values = null; 79  } 80  81  abstract C combine(List<@Nullable Present<V>> values); 82  83  /** Used for {@link Futures#allAsList} and {@link Futures#successfulAsList}. */ 84  static final class ListFuture<V extends @Nullable Object> 85  extends CollectionFuture<V, List<@Nullable V>> { 86  ListFuture( 87  ImmutableCollection<? extends ListenableFuture<? extends V>> futures, 88  boolean allMustSucceed) { 89  super(futures, allMustSucceed); 90  init(); 91  } 92  93  @Override 94  public List<@Nullable V> combine(List<@Nullable Present<V>> values) { 95  List<@Nullable V> result = newArrayListWithCapacity(values.size()); 96  for (Present<V> element : values) { 97  result.add(element != null ? element.value : null); 98  } 99  return unmodifiableList(result); 100  } 101  } 102  103  /** The result of a successful {@code Future}. */ 104  private static final class Present<V extends @Nullable Object> { 105  V value; 106  107  Present(V value) { 108  this.value = value; 109  } 110  } 111 }