/*
 * Copyright 2015-present Facebook, 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 com.facebook.buck.jvm.java.intellij;

import com.facebook.buck.android.AndroidLibraryDescription;
import com.facebook.buck.jvm.java.JavaLibraryDescription;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.TargetNode;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

import java.util.HashMap;
import java.util.Map;

/**
 * Calculates the transitive closure of exported deps for every node in a {@link TargetGraph}.
 */
public class ExportedDepsClosureResolver {

  private TargetGraph targetGraph;
  private Map<BuildTarget, ImmutableSet<BuildTarget>> index;

  public ExportedDepsClosureResolver(TargetGraph targetGraph) {
    this.targetGraph = targetGraph;
    index = new HashMap<>();
  }

  /**
   * @param buildTarget target to process.
   * @return the set of {@link BuildTarget}s that must be appended to the
   *         dependencies of a node Y if node Y depends on X.
   */
  public ImmutableSet<BuildTarget> getExportedDepsClosure(BuildTarget buildTarget) {
    if (index.containsKey(buildTarget)) {
      return index.get(buildTarget);
    }

    ImmutableSet<BuildTarget> exportedDeps = ImmutableSet.of();
    TargetNode<?, ?> targetNode = targetGraph.get(buildTarget);
    if (targetNode.getType().equals(Description.getBuildRuleType(JavaLibraryDescription.class))) {
      JavaLibraryDescription.Arg arg = (JavaLibraryDescription.Arg) targetNode.getConstructorArg();
      exportedDeps = arg.exportedDeps;
    } else if (
        targetNode.getType().equals(
            Description.getBuildRuleType(AndroidLibraryDescription.class))) {
      AndroidLibraryDescription.Arg arg =
          (AndroidLibraryDescription.Arg) targetNode.getConstructorArg();
      exportedDeps = arg.exportedDeps;
    }

    ImmutableSet<BuildTarget> exportedDepsClosure = FluentIterable.from(exportedDeps)
        .transformAndConcat(
            new Function<BuildTarget, Iterable<BuildTarget>>() {
              @Override
              public Iterable<BuildTarget> apply(BuildTarget input) {
                return Iterables.concat(ImmutableSet.of(input), getExportedDepsClosure(input));
              }
            })
        .toSet();
    index.put(buildTarget, exportedDepsClosure);
    return exportedDepsClosure;
  }

}

@@ -954,84 +954,41 @@
 com.
-google.common.base.Function;%0Aimport com.google.common.collect.FluentIterable
+facebook.buck.util.MoreCollectors
 ;%0Aim
@@ -1035,52 +1035,8 @@
 Set;
-%0Aimport com.google.common.collect.Iterables;
 %0A%0Aim
@@ -1080,16 +1080,48 @@
 til.Map;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -2569,28 +2569,8 @@
 e = 
-FluentIterable.from(
 expo
@@ -2577,17 +2577,16 @@
 rtedDeps
-)
 %0A       
@@ -2591,331 +2591,215 @@
    .
+s
 tr
-ansformAndConcat(%0A            new Function%3CBuildTarget, Iterable%3CBuildTarget%3E%3E() %7B%0A              @Override%0A              public Iterable%3CBuildTarget%3E apply(BuildTarget input) %7B%0A                return Iterables.concat(ImmutableSet.of(input), getExportedDepsClosure(input));%0A              %7D%0A            %7D)%0A        .to
+eam()%0A        .flatMap(target -%3E%0A            Stream.concat(%0A                Stream.of(target),%0A                getExportedDepsClosure(target).stream()))%0A        .collect(MoreCollectors.toImmutable
 Set()
-;
+);%0A
 %0A   

Use lambdas in ExportedDepsClosureResolver