Batch plugin: refactor BatchSubmitter

Refactor BatchSubmitter to re-use objects instead of creating
objects in loop. Also, remove repeated object creation.

Change-Id: I610bc1f3cbfeb3ba909be8ffef0748a18722133e
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
index 546ae42..06bcd27 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
@@ -136,24 +136,24 @@
       throws IOException, RepositoryNotFoundException, RestApiException, UpdateException,
           PermissionBackendException {
     if (dest.changes != null) {
+      Project.NameKey project = Project.nameKey(dest.project);
       // TODO: Is using the first change in the batch for each dest the correct thing to do?
       Change firstInDest =
           notesFactory
-              .createChecked(
-                  Project.nameKey(dest.project), dest.changes.get(0).toPatchSetId().changeId())
+              .createChecked(project, dest.changes.get(0).toPatchSetId().changeId())
               .getChange();
       SubmissionId submissionId = new SubmissionId(firstInDest);
       for (Batch.Change change : dest.changes) {
-        closeChange(Project.nameKey(dest.project), change.toPatchSetId(), dest.sha1, submissionId);
+        closeChange(project, change.toPatchSetId(), dest.sha1, submissionId);
       }
     }
   }
 
   private void closeChange(
-      Project.NameKey destProject, PatchSet.Id psId, String sha1, SubmissionId submissionId)
+      Project.NameKey project, PatchSet.Id psId, String sha1, SubmissionId submissionId)
       throws IOException, RepositoryNotFoundException, RestApiException, UpdateException,
           PermissionBackendException {
-    ChangeNotes changeNotes = notesFactory.createChecked(destProject, psId.changeId());
+    ChangeNotes changeNotes = notesFactory.createChecked(project, psId.changeId());
     permissionBackend.user(user).change(changeNotes).check(ChangePermission.READ);
     Change change = changeNotes.getChange();
     PatchSet ps = psUtil.get(changeNotes, psId);
@@ -166,8 +166,6 @@
         || change.getStatus() == Change.Status.ABANDONED) {
       return;
     }
-    BranchNameKey destination = change.getDest();
-    Project.NameKey project = destination.project();
 
     try (TraceContext traceContext =
             TraceContext.open()
@@ -182,7 +180,7 @@
       bu.addOp(
           psId.changeId(),
           mergedByPushOpFactory.create(
-              requestScopePropagator, psId, submissionId, destination.branch(), sha1));
+              requestScopePropagator, psId, submissionId, change.getDest().branch(), sha1));
       bu.execute();
     }
   }