LfsBackend: Make constructor and DEFAULT private

Make the constructor private and abstract the creation of instances
to static methods so that callers don't need to explicitly pass null
to create the default instance.

Make DEFAULT private and introduce a name() method that returns the
name, or DEFAULT if name is null.

The hashCode method unnecessarily converts null to the DEFAULT value,
so change that to just use the value (potentially null) as is.

Change-Id: Ibac02e42217e498f6101700660b085a23005f2b3
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsBackend.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsBackend.java
index a2fbfe2..ecdfa60 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsBackend.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsBackend.java
@@ -15,22 +15,36 @@
 package com.googlesource.gerrit.plugins.lfs;
 
 import com.google.common.base.Strings;
+import com.google.gerrit.common.Nullable;
 import java.util.Objects;
 
 public class LfsBackend {
-  public static final String DEFAULT = "default";
+  private static final String DEFAULT = "default";
 
   public final String name;
   public final LfsBackendType type;
 
-  public LfsBackend(String name, LfsBackendType type) {
+  public static LfsBackend create(@Nullable String name, LfsBackendType type) {
+    return new LfsBackend(name, type);
+  }
+
+  public static LfsBackend createDefault(LfsBackendType type) {
+    return create(null, type);
+  }
+
+  private LfsBackend(String name, LfsBackendType type) {
     this.name = name;
     this.type = type;
   }
 
+  /** @return the backend name, or the default name if null. */
+  public String name() {
+    return Strings.isNullOrEmpty(name) ? DEFAULT : name;
+  }
+
   @Override
   public int hashCode() {
-    return Objects.hash(Strings.isNullOrEmpty(name) ? DEFAULT : name, type);
+    return Objects.hash(name, type);
   }
 
   @Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGlobalConfig.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGlobalConfig.java
index 6283c9e..e22139e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGlobalConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsGlobalConfig.java
@@ -30,15 +30,15 @@
   }
 
   public LfsBackend getDefaultBackend() {
-    LfsBackendType type = cfg.getEnum("storage", null, "backend", LfsBackendType.FS);
-    return new LfsBackend(null, type);
+    return LfsBackend.createDefault(cfg.getEnum("storage", null, "backend", LfsBackendType.FS));
   }
 
   public Map<String, LfsBackend> getBackends() {
     Builder<String, LfsBackend> builder = ImmutableMap.builder();
     for (LfsBackendType type : LfsBackendType.values()) {
       Map<String, LfsBackend> backendsOfType =
-          FluentIterable.from(cfg.getSubsections(type.name())).toMap(s -> new LfsBackend(s, type));
+          FluentIterable.from(cfg.getSubsections(type.name()))
+              .toMap(name -> LfsBackend.create(name, type));
       builder.putAll(backendsOfType);
     }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsRepositoryResolver.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsRepositoryResolver.java
index 5087976..3e97db3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsRepositoryResolver.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/LfsRepositoryResolver.java
@@ -14,8 +14,6 @@
 
 package com.googlesource.gerrit.plugins.lfs;
 
-import static com.googlesource.gerrit.plugins.lfs.LfsBackend.DEFAULT;
-
 import com.google.common.base.Strings;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.inject.Inject;
@@ -49,10 +47,7 @@
     } else {
       backend = backends.get(backendName);
       if (backend == null) {
-        log.error(
-            "Project {} is configured with not existing backend {}",
-            project,
-            Strings.isNullOrEmpty(backendName) ? DEFAULT : backendName);
+        log.error("Project {} is configured with not existing backend {}", project, backendName);
         throw new LfsRepositoryNotFound(project.get());
       }
     }
@@ -66,7 +61,7 @@
     log.error(
         "Project {} is configured with not existing backend {} of type {}",
         project,
-        Strings.isNullOrEmpty(backendName) ? DEFAULT : backendName,
+        backend.name(),
         backend.type);
     throw new LfsRepositoryNotFound(project.get());
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
index 172861b..a48d4bc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
@@ -14,7 +14,6 @@
 
 package com.googlesource.gerrit.plugins.lfs.fs;
 
-import static com.googlesource.gerrit.plugins.lfs.LfsBackend.DEFAULT;
 import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
 import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
 
@@ -94,10 +93,7 @@
   }
 
   private static String getContentPath(LfsBackend backend) {
-    return CONTENT_PATH
-        + "/"
-        + (Strings.isNullOrEmpty(backend.name) ? DEFAULT : backend.name)
-        + "/";
+    return CONTENT_PATH + "/" + backend.name() + "/";
   }
 
   private static Path getOrCreateDataDir(