class Guavate {

  @SuppressWarnings("unchecked")
  public static <K, V, F extends CompletableFuture<? extends V>> CompletableFuture<Map<K, V>>
      combineFuturesAsMap(Map<? extends K, ? extends F> futures) {

    int size = futures.size();
    K[] keyArray = (K[]) new Object[size];
    CompletableFuture<? extends V>[] futuresArray = new CompletableFuture[size];
    int index = 0;
    for (Entry<? extends K, ? extends F> entry : futures.entrySet()) {
      keyArray[index] = entry.getKey();
      futuresArray[index] = entry.getValue();
      index++;
    }
    return CompletableFuture.allOf(futuresArray)
        .thenApply(unused -> {
          ImmutableMap.Builder<K, V> builder = ImmutableMap.builderWithExpectedSize(size);
          for (int i = 0; i < size; i++) {
            builder.put(keyArray[i], futuresArray[i].join());
          }
          return builder.build();
        });
  }

  private  Guavate();

}