

package com.facebook.buck.core.rules.attr;

import com.facebook.buck.core.model.OutputLabel;
import com.facebook.buck.core.rules.BuildRule;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;

/**
 * {@link com.facebook.buck.core.rules.BuildRule} instances that support multiple outputs via output
 * labels.
 */
public interface HasMultipleOutputs extends BuildRule {
  /**
   * Returns {@link SourcePath} instances to the outputs associated with the given output label, or
   * {@code null} if the output label does not exist. If the default output label is given, returns
   * the default outputs associated with the rule.
   */
  @Nullable
  ImmutableSortedSet<SourcePath> getSourcePathToOutput(OutputLabel outputLabel);

  /** returns a set of {@link OutputLabel} instances associated with this build rule. */
  ImmutableSet<OutputLabel> getOutputLabels();

  @Nullable
  @Override
  default SourcePath getSourcePathToOutput() {
    ImmutableSortedSet<SourcePath> sourcePaths = getSourcePathToOutput(OutputLabel.defaultLabel());
    return sourcePaths == null || sourcePaths.isEmpty()
        ? null
        : Iterables.getOnlyElement(sourcePaths);
  }
}
