class DefaultCredentialRetrievers {

  public List<CredentialRetriever> asList() throws FileNotFoundException {
    List<CredentialRetriever> credentialRetrievers = new ArrayList<>();
    if (knownCredentialRetriever != null) {
      credentialRetrievers.add(knownCredentialRetriever);
    }
    if (credentialHelper != null) {
      // If credential helper contains file separator, treat as path; otherwise treat as suffix
      if (credentialHelper.contains(FileSystems.getDefault().getSeparator())) {
        if (!Files.exists(Paths.get(credentialHelper))) {
          String osName = systemProperties.getProperty("os.name").toLowerCase(Locale.ENGLISH);
          if (!osName.contains("windows")
              || (!Files.exists(Paths.get(credentialHelper + ".cmd"))
                  && !Files.exists(Paths.get(credentialHelper + ".exe")))) {
            throw new FileNotFoundException(
                "Specified credential helper was not found: " + credentialHelper);
          }
        }
        credentialRetrievers.add(
            credentialRetrieverFactory.dockerCredentialHelper(credentialHelper));
      } else {
        String suffix = credentialHelper; // not path; treat as suffix
        credentialRetrievers.add(
            credentialRetrieverFactory.dockerCredentialHelper("docker-credential-" + suffix));
      }
    }
    if (inferredCredentialRetriever != null) {
      credentialRetrievers.add(inferredCredentialRetriever);
    }

    List<Path> checkedDockerDirs = new ArrayList<>();
    String dockerConfigEnv = environment.get("DOCKER_CONFIG");
    if (dockerConfigEnv != null) {
      Path dockerConfigEnvPath = Paths.get(dockerConfigEnv);
      addDockerFiles(credentialRetrievers, Paths.get(dockerConfigEnv));
      checkedDockerDirs.add(dockerConfigEnvPath);
    }

    String homeProperty = systemProperties.getProperty("user.home");
    if (homeProperty != null) {
      Path homePropertyPath = Paths.get(homeProperty).resolve(DOCKER_DIRECTORY);
      if (!checkedDockerDirs.contains(homePropertyPath)) {
        addDockerFiles(credentialRetrievers, homePropertyPath);
        checkedDockerDirs.add(homePropertyPath);
      }
    }

    String homeEnvVar = environment.get("HOME");
    if (homeEnvVar != null) {
      Path homeEnvDockerPath = Paths.get(homeEnvVar).resolve(DOCKER_DIRECTORY);
      if (!checkedDockerDirs.contains(homeEnvDockerPath)) {
        addDockerFiles(credentialRetrievers, homeEnvDockerPath);
      }
    }

    credentialRetrievers.add(credentialRetrieverFactory.wellKnownCredentialHelpers());
    credentialRetrievers.add(credentialRetrieverFactory.googleApplicationDefaultCredentials());
    return credentialRetrievers;
  }

  @VisibleForTesting  DefaultCredentialRetrievers(
      CredentialRetrieverFactory credentialRetrieverFactory,
      Properties systemProperties,
      Map<String, String> environment);

}