// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.config;

import com.google.common.collect.ImmutableSet;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.server.change.ArchiveFormat;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.jgit.lib.Config;

import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/** Download protocol from {@code gerrit.config}. */
@Singleton
public class DownloadConfig {
  private final Set<DownloadScheme> downloadSchemes;
  private final Set<DownloadCommand> downloadCommands;
  private final Set<ArchiveFormat> archiveFormats;

  @Inject
  DownloadConfig(@GerritServerConfig final Config cfg) {
    List<DownloadScheme> allSchemes =
        ConfigUtil.getEnumList(cfg, "download", null, "scheme",
            DownloadScheme.values(), null);
    if (isOnlyNull(allSchemes)) {
      downloadSchemes = ImmutableSet.of(
          DownloadScheme.SSH,
          DownloadScheme.HTTP,
          DownloadScheme.ANON_HTTP);
    } else {
      downloadSchemes = ImmutableSet.copyOf(allSchemes);
    }

    DownloadCommand[] downloadCommandValues = DownloadCommand.values();
    List<DownloadCommand> allCommands =
        ConfigUtil.getEnumList(cfg, "download", null, "command",
            downloadCommandValues, null);
    if (isOnlyNull(allCommands)) {
      downloadCommands = ImmutableSet.copyOf(downloadCommandValues);
    } else {
      downloadCommands = ImmutableSet.copyOf(allCommands);
    }

    String v = cfg.getString("download", null, "archive");
    if (v == null) {
      archiveFormats = EnumSet.allOf(ArchiveFormat.class);
    } else if (v.isEmpty() || "off".equalsIgnoreCase(v)) {
      archiveFormats = Collections.emptySet();
    } else {
      archiveFormats = new HashSet<>(ConfigUtil.getEnumList(cfg,
          "download", null, "archive",
          ArchiveFormat.TGZ));
    }
  }

  private static boolean isOnlyNull(List<?> list) {
    return list.size() == 1 && list.get(0) == null;
  }

  /** Scheme used to download. */
  public Set<DownloadScheme> getDownloadSchemes() {
    return downloadSchemes;
  }

  /** Command used to download. */
  public Set<DownloadCommand> getDownloadCommands() {
    return downloadCommands;
  }

  /** Archive formats for downloading. */
  public Set<ArchiveFormat> getArchiveFormats() {
    return archiveFormats;
  }
}

@@ -1040,68 +1040,12 @@
 til.
-Collections;%0Aimport java.util.EnumSet;%0Aimport java.util.Hash
+Enum
 Set;
@@ -1071,30 +1071,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0A/*
@@ -1171,32 +1171,41 @@
   private final 
+Immutable
 Set%3CDownloadSche
@@ -1237,24 +1237,33 @@
 ivate final 
+Immutable
 Set%3CDownload
@@ -1305,16 +1305,25 @@
 e final 
+Immutable
 Set%3CArch
@@ -2318,16 +2318,36 @@
 rmats = 
+ImmutableSet.copyOf(
 EnumSet.
@@ -2372,16 +2372,17 @@
 t.class)
+)
 ;%0A    %7D 
@@ -2461,28 +2461,23 @@
 s = 
-Collections.emptySet
+ImmutableSet.of
 ();%0A
@@ -2516,21 +2516,27 @@
 s = 
-new HashSet%3C%3E
+ImmutableSet.copyOf
 (Con
@@ -2785,32 +2785,41 @@
 ad. */%0A  public 
+Immutable
 Set%3CDownloadSche
@@ -2918,24 +2918,33 @@
 */%0A  public 
+Immutable
 Set%3CDownload
@@ -3061,16 +3061,25 @@
  public 
+Immutable
 Set%3CArch

Make set methods return ImmutableSet