/**
  * Select the service based on the index value.
  *
  * @param candidates the list of candiate services
  * @return the selected candidate or null
  */
 private AttributeConsumingService selectByIndex(List<AttributeConsumingService> candidates) {
   log.debug("Selecting AttributeConsumingService by index");
   for (AttributeConsumingService attribCS : candidates) {
     // Check for null b/c don't ever want to fail with an NPE due to autoboxing.
     // Note: metadata index property is an int, not an Integer.
     if (index != null) {
       if (index == attribCS.getIndex()) {
         log.debug("Selected AttributeConsumingService with index: {}", index);
         return attribCS;
       }
     }
   }
   log.debug("A service index of '{}' was specified, but was not found in metadata", index);
   return null;
 }
  /**
   * Select the default service.
   *
   * @param candidates the list of candiate services
   * @return the selected candidate or null
   */
  private AttributeConsumingService selectDefault(List<AttributeConsumingService> candidates) {
    log.debug("Selecting default AttributeConsumingService");
    AttributeConsumingService firstNoDefault = null;
    for (AttributeConsumingService attribCS : candidates) {
      if (attribCS.isDefault()) {
        log.debug("Selected AttributeConsumingService with explicit isDefault of true");
        return attribCS;
      }

      // This records the first element whose isDefault is not explicitly false
      if (firstNoDefault == null && attribCS.isDefaultXSBoolean() == null) {
        firstNoDefault = attribCS;
      }
    }

    if (firstNoDefault != null) {
      log.debug("Selected first AttributeConsumingService with no explicit isDefault");
      return firstNoDefault;
    } else {
      log.debug("Selected first AttributeConsumingService with explicit isDefault of false");
      return candidates.get(0);
    }
  }