Convert File.NameKey to AutoValue

Change-Id: If0110c1a391d67f2d70a3413b46e9ffe7c9e2c1c
diff --git a/.factorypath b/.factorypath
new file mode 100644
index 0000000..c1c6e82
--- /dev/null
+++ b/.factorypath
@@ -0,0 +1,3 @@
+<factorypath>
+    <factorypathentry kind="VARJAR" id="M2_REPO/com/google/auto/value/auto-value/1.7/auto-value-1.7.jar" enabled="true" runInBatchMode="false"/>
+</factorypath>
diff --git a/BUILD b/BUILD
index 0e08d92..f364456 100644
--- a/BUILD
+++ b/BUILD
@@ -5,6 +5,46 @@
     "gerrit_plugin",
 )
 
+load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
+
+java_plugin(
+    name = "auto-annotation-plugin",
+    processor_class = "com.google.auto.value.processor.AutoAnnotationProcessor",
+    deps = [
+        "@auto-value-annotations//jar",
+        "@auto-value//jar",
+    ],
+)
+
+java_plugin(
+    name = "auto-value-plugin",
+    processor_class = "com.google.auto.value.processor.AutoValueProcessor",
+    deps = [
+        "@auto-value-annotations//jar",
+        "@auto-value//jar",
+    ],
+)
+
+java_library(
+    name = "auto-value",
+    exported_plugins = [
+        ":auto-annotation-plugin",
+        ":auto-value-plugin",
+    ],
+    visibility = ["//visibility:public"],
+    exports = ["@auto-value//jar"],
+)
+
+java_library(
+    name = "auto-value-annotations",
+    exported_plugins = [
+        ":auto-annotation-plugin",
+        ":auto-value-plugin",
+    ],
+    visibility = ["//visibility:public"],
+    exports = ["@auto-value-annotations//jar"],
+)
+
 gerrit_plugin(
     name = "batch",
     srcs = glob(["src/main/java/**/*.java"]),
@@ -17,4 +57,8 @@
         "Gerrit-SshModule: com.googlesource.gerrit.plugins.batch.ssh.SshModule",
     ],
     resources = glob(["src/main/resources/**/*"]),
+    deps = [
+        ":auto-value",
+        ":auto-value-annotations"
+    ],
 )
diff --git a/WORKSPACE b/WORKSPACE
index 4f70dda..8438894 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -22,5 +22,21 @@
 # Load release Plugin API
 gerrit_api()
 
+load("//tools/bzl:maven_jar.bzl", "GERRIT", "maven_jar")
+
+AUTO_VALUE_VERSION = "1.7"
+
+maven_jar(
+    name = "auto-value",
+    artifact = "com.google.auto.value:auto-value:" + AUTO_VALUE_VERSION,
+    sha1 = "fe8387764ed19460eda4f106849c664f51c07121",
+)
+
+maven_jar(
+    name = "auto-value-annotations",
+    artifact = "com.google.auto.value:auto-value-annotations:" + AUTO_VALUE_VERSION,
+    sha1 = "5be124948ebdc7807df68207f35a0f23ce427f29",
+)
+
 # Load snapshot Plugin API
 #gerrit_api_maven_local()
diff --git a/pom.xml b/pom.xml
index ad3ac91..0ef2e07 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,6 +28,7 @@
   <properties>
     <Gerrit-ApiType>plugin</Gerrit-ApiType>
     <Gerrit-ApiVersion>${project.version}</Gerrit-ApiVersion>
+    <auto-value.version>1.7</auto-value.version>
   </properties>
 
   <build>
@@ -64,6 +65,13 @@
           <source>1.8</source>
           <target>1.8</target>
           <encoding>UTF-8</encoding>
+          <annotationProcessorPaths>
+            <path>
+              <groupId>com.google.auto.value</groupId>
+              <artifactId>auto-value</artifactId>
+              <version>${auto-value.version}</version>
+            </path>
+          </annotationProcessorPaths>
         </configuration>
       </plugin>
     </plugins>
@@ -76,6 +84,12 @@
       <version>${Gerrit-ApiVersion}</version>
       <scope>provided</scope>
     </dependency>
+
+    <dependency>
+      <groupId>com.google.auto.value</groupId>
+      <artifactId>auto-value-annotations</artifactId>
+      <version>${auto-value.version}</version>
+    </dependency>
   </dependencies>
 
   <repositories>
diff --git a/src/main/java/com/google/gerrit/entities/File.java b/src/main/java/com/google/gerrit/entities/File.java
deleted file mode 100644
index 7c58be8..0000000
--- a/src/main/java/com/google/gerrit/entities/File.java
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright (C) 2017 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.entities;
-
-import com.google.gerrit.entities.BranchNameKey;
-import com.google.gerrit.entities.Project;
-
-public class File {
-  /** An immutable reference to a file in gerrit repo. */
-  public static class NameKey implements Comparable<NameKey> {
-    protected BranchNameKey branch;
-    protected String fileName;
-
-    protected NameKey() {
-      branch = BranchNameKey.create(Project.nameKey(null), null);
-    }
-
-    public NameKey(BranchNameKey br, String file) {
-      branch = br;
-      fileName = file;
-    }
-
-    public String get() {
-      return fileName;
-    }
-
-    public BranchNameKey getParentKey() {
-      return branch;
-    }
-
-    @Override
-    public final int compareTo(NameKey o) {
-      return get().compareTo(o.get());
-    }
-  }
-}
diff --git a/src/main/java/com/google/gerrit/entities/FileNameKey.java b/src/main/java/com/google/gerrit/entities/FileNameKey.java
new file mode 100644
index 0000000..63531a5
--- /dev/null
+++ b/src/main/java/com/google/gerrit/entities/FileNameKey.java
@@ -0,0 +1,39 @@
+// Copyright (C) 2017 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.entities;
+
+import com.google.auto.value.AutoValue;
+import com.google.gerrit.entities.BranchNameKey;
+import com.google.gerrit.entities.Project;
+
+/** An immutable reference to a file in gerrit repo. */
+@AutoValue
+public abstract class FileNameKey implements Comparable<FileNameKey> {
+  public static FileNameKey create(BranchNameKey branch, String file) {
+    return new AutoValue_FileNameKey(branch, file);
+  }
+
+  public static FileNameKey create() {
+    return new AutoValue_FileNameKey(BranchNameKey.create(Project.nameKey(null), null), null);
+  }
+
+  public abstract BranchNameKey branch();
+  public abstract String file();
+
+  @Override
+  public final int compareTo(FileNameKey o) {
+    return file().compareTo(o.file());
+  }
+}
diff --git a/src/main/java/com/google/gerrit/server/git/meta/GitFile.java b/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
index 311b560..2bf5a14 100644
--- a/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
+++ b/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
@@ -15,7 +15,7 @@
 package com.google.gerrit.server.git.meta;
 
 import com.google.gerrit.entities.BranchNameKey;
-import com.google.gerrit.entities.File;
+import com.google.gerrit.entities.FileNameKey;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.project.NoSuchProjectException;
@@ -31,7 +31,7 @@
 /** A GitFile is a text file (UTF8) from a git repository */
 public class GitFile extends VersionedMetaData {
   public interface Factory {
-    GitFile create(@Assisted File.NameKey file);
+    GitFile create(@Assisted FileNameKey file);
   }
 
   protected final MetaDataUpdate.User metaDataUpdateFactory;
@@ -46,11 +46,11 @@
   public GitFile(
       MetaDataUpdate.User metaDataUpdateFactory,
       GitRepositoryManager repos,
-      @Assisted File.NameKey file) {
+      @Assisted FileNameKey file) {
     this.metaDataUpdateFactory = metaDataUpdateFactory;
     this.repos = repos;
-    this.branch = file.getParentKey();
-    this.file = file.get();
+    this.branch = file.branch();
+    this.file = file.file();
   }
 
   public String read() throws ConfigInvalidException, IOException, NoSuchProjectException {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
index 12f93d1..7ce10a6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
@@ -15,7 +15,7 @@
 
 import com.google.gerrit.entities.Account;
 import com.google.gerrit.entities.BranchNameKey;
-import com.google.gerrit.entities.File;
+import com.google.gerrit.entities.FileNameKey;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.server.config.AllProjectsName;
 import com.google.gerrit.server.git.GitRepositoryManager;
@@ -137,7 +137,7 @@
     throw new NoSuchBatchException(id);
   }
 
-  protected File.NameKey getFileNameKey(String id) {
+  protected FileNameKey getFileNameKey(String id) {
     return getFileNameKey(getBranch(id));
   }
 
@@ -145,7 +145,7 @@
     return BranchNameKey.create(project, BATCHES_REF + id);
   }
 
-  protected File.NameKey getFileNameKey(BranchNameKey branch) {
-    return new File.NameKey(branch, FILE_NAME);
+  protected FileNameKey getFileNameKey(BranchNameKey branch) {
+    return FileNameKey.create(branch, FILE_NAME);
   }
 }
diff --git a/tools/bzl/maven_jar.bzl b/tools/bzl/maven_jar.bzl
new file mode 100644
index 0000000..ed1504d
--- /dev/null
+++ b/tools/bzl/maven_jar.bzl
@@ -0,0 +1,4 @@
+load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", _gerrit = "GERRIT", _maven_jar = "maven_jar")
+
+maven_jar = _maven_jar
+GERRIT = _gerrit