private ModuleComponentIdentifier chooseBestMatchingDependency(
      ModuleVersionListing versions, ModuleVersionSelector requested) {
    for (Versioned candidate : sortLatestFirst(versions)) {
      // Apply version selection rules
      ModuleComponentIdentifier candidateIdentifier =
          DefaultModuleComponentIdentifier.newId(
              requested.getGroup(), requested.getName(), candidate.getVersion());
      ModuleComponentSelector requestedComponentSelector =
          DefaultModuleComponentSelector.newSelector(requested);
      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(requested.getVersion(), candidate.getVersion())) {
        return candidateIdentifier;
      }
    }
    return null;
  }
 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();
 }