private ModuleComponentIdentifier chooseBestMatchingDependencyWithMetaData(
      ModuleVersionListing versions,
      DependencyMetaData dependency,
      ModuleComponentRepositoryAccess moduleAccess) {
    for (Versioned candidate : sortLatestFirst(versions)) {
      MutableModuleVersionMetaData metaData =
          resolveComponentMetaData(dependency, candidate, moduleAccess);
      ModuleComponentIdentifier candidateIdentifier = metaData.getComponentId();

      // Apply version selection rules
      ModuleComponentSelector requestedComponentSelector =
          DefaultModuleComponentSelector.newSelector(dependency.getRequested());
      VersionSelectionInternal selection =
          new DefaultVersionSelection(requestedComponentSelector, candidateIdentifier);
      versionSelectionRules.apply(selection);

      switch (selection.getState()) {
        case ACCEPTED:
          return candidateIdentifier;
        case REJECTED:
          continue;
        default:
          break;
      }

      // Invoke version matcher
      if (versionMatcher.accept(dependency.getRequested().getVersion(), metaData)) {
        // We already resolved the correct module.
        return candidateIdentifier;
      }
    }
    return null;
  }
 public ModuleComponentIdentifier choose(
     ModuleVersionListing versions,
     DependencyMetaData dependency,
     ModuleComponentRepositoryAccess moduleAccess) {
   if (versionMatcher.needModuleMetadata(dependency.getRequested().getVersion())) {
     return chooseBestMatchingDependencyWithMetaData(versions, dependency, moduleAccess);
   } else {
     return chooseBestMatchingDependency(versions, dependency.getRequested());
   }
 }
 private MutableModuleVersionMetaData resolveComponentMetaData(
     DependencyMetaData dependency,
     Versioned candidate,
     ModuleComponentRepositoryAccess moduleAccess) {
   ModuleVersionSelector selector = dependency.getRequested();
   ModuleComponentIdentifier candidateId =
       DefaultModuleComponentIdentifier.newId(
           selector.getGroup(), selector.getName(), candidate.getVersion());
   DependencyMetaData moduleVersionDependency =
       dependency.withRequestedVersion(candidate.getVersion());
   BuildableModuleVersionMetaDataResolveResult descriptorResult =
       new DefaultBuildableModuleVersionMetaDataResolveResult();
   moduleAccess.resolveComponentMetaData(moduleVersionDependency, candidateId, descriptorResult);
   return descriptorResult.getMetaData();
 }