示例#1
0
public class DefaultNativeComponent implements NativeComponentInternal {
  private final NotationParser<Object, Set<LanguageSourceSet>> sourcesNotationParser =
      SourceSetNotationParser.parser();
  private final NativeBuildComponentIdentifier id;
  private final DomainObjectSet<LanguageSourceSet> sourceSets;
  private final DefaultDomainObjectSet<NativeBinary> binaries;
  private final Set<String> targetPlatforms = new HashSet<String>();
  private final Set<String> buildTypes = new HashSet<String>();
  private final Set<String> flavors = new HashSet<String>();
  private String baseName;

  public DefaultNativeComponent(NativeBuildComponentIdentifier id, Instantiator instantiator) {
    this.id = id;
    this.sourceSets = new DefaultDomainObjectSet<LanguageSourceSet>(LanguageSourceSet.class);
    binaries = new DefaultDomainObjectSet<NativeBinary>(NativeBinary.class);
  }

  public String getProjectPath() {
    return id.getProjectPath();
  }

  public String getName() {
    return id.getName();
  }

  public DomainObjectSet<LanguageSourceSet> getSource() {
    return sourceSets;
  }

  public void source(Object sources) {
    sourceSets.addAll(sourcesNotationParser.parseNotation(sources));
  }

  public DomainObjectSet<NativeBinary> getBinaries() {
    return binaries;
  }

  public String getBaseName() {
    return GUtil.elvis(baseName, getName());
  }

  public void setBaseName(String baseName) {
    this.baseName = baseName;
  }

  public void targetFlavors(Object... flavorSelectors) {
    for (Object flavorSelector : flavorSelectors) {
      // TODO:DAZ Allow Flavor instance and selector
      assert flavorSelector instanceof String;
      flavors.add((String) flavorSelector);
    }
  }

  public void targetPlatforms(Object... platformSelectors) {
    for (Object platformSelector : platformSelectors) {
      // TODO:DAZ Allow Platform instance and selector
      assert platformSelector instanceof String;
      targetPlatforms.add((String) platformSelector);
    }
  }

  public void targetBuildTypes(Object... buildTypeSelectors) {
    for (Object buildTypeSelector : buildTypeSelectors) {
      // TODO:DAZ Allow BuildType instance and selector
      assert buildTypeSelector instanceof String;
      buildTypes.add((String) buildTypeSelector);
    }
  }

  public Set<Flavor> chooseFlavors(Set<? extends Flavor> candidates) {
    return chooseElements(Flavor.class, candidates, flavors);
  }

  public Set<BuildType> chooseBuildTypes(Set<? extends BuildType> candidates) {
    return chooseElements(BuildType.class, candidates, buildTypes);
  }

  public Set<Platform> choosePlatforms(Set<? extends Platform> candidates) {
    return chooseElements(Platform.class, candidates, targetPlatforms);
  }

  private <T extends Named> Set<T> chooseElements(
      Class<T> type, Set<? extends T> candidates, final Set<String> names) {
    if (names.isEmpty()) {
      return new LinkedHashSet<T>(candidates);
    }

    Set<String> unusedNames = new HashSet<String>(names);
    Set<T> chosen = new LinkedHashSet<T>();
    for (T candidate : candidates) {
      if (unusedNames.remove(candidate.getName())) {
        chosen.add(candidate);
      }
    }

    if (!unusedNames.isEmpty()) {
      throw new InvalidUserDataException(
          String.format("Invalid %s: '%s'", type.getSimpleName(), unusedNames.iterator().next()));
    }

    return chosen;
  }
}