From b8746713327b708b90b7b073c11e593d6dc04903 Mon Sep 17 00:00:00 2001 From: Joerg Zieren Date: Fri, 31 Jan 2020 12:50:30 +0100 Subject: [PATCH] Improve readability, build ImmutableMap directly Change-Id: I4cf2323c70989a4512347bd8a73823ce654c9882 --- .../gerrit/server/notedb/RevisionNoteMap.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/java/com/google/gerrit/server/notedb/RevisionNoteMap.java b/java/com/google/gerrit/server/notedb/RevisionNoteMap.java index cf1607334d..98c9873835 100644 --- a/java/com/google/gerrit/server/notedb/RevisionNoteMap.java +++ b/java/com/google/gerrit/server/notedb/RevisionNoteMap.java @@ -17,8 +17,6 @@ package com.google.gerrit.server.notedb; import com.google.common.collect.ImmutableMap; import com.google.gerrit.entities.Comment; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectReader; @@ -31,44 +29,44 @@ import org.eclipse.jgit.notes.NoteMap; * @param the RevisionNote for the comment type. */ class RevisionNoteMap> { - // CommitID => blob ID + /** CommitID => blob ID */ final NoteMap noteMap; - // CommitID => parsed data, immutable map. + /** CommitID => parsed data */ final ImmutableMap revisionNotes; + private RevisionNoteMap(NoteMap noteMap, ImmutableMap revisionNotes) { + this.noteMap = noteMap; + this.revisionNotes = revisionNotes; + } + static RevisionNoteMap parse( ChangeNoteJson noteJson, ObjectReader reader, NoteMap noteMap, Comment.Status status) throws ConfigInvalidException, IOException { - Map result = new HashMap<>(); + ImmutableMap.Builder result = ImmutableMap.builder(); for (Note note : noteMap) { ChangeRevisionNote rn = new ChangeRevisionNote(noteJson, reader, note.getData(), status); rn.parse(); result.put(note.copy(), rn); } - return new RevisionNoteMap<>(noteMap, ImmutableMap.copyOf(result)); + return new RevisionNoteMap<>(noteMap, result.build()); } static RevisionNoteMap parseRobotComments( ChangeNoteJson changeNoteJson, ObjectReader reader, NoteMap noteMap) throws ConfigInvalidException, IOException { - Map result = new HashMap<>(); + ImmutableMap.Builder result = ImmutableMap.builder(); for (Note note : noteMap) { RobotCommentsRevisionNote rn = new RobotCommentsRevisionNote(changeNoteJson, reader, note.getData()); rn.parse(); result.put(note.copy(), rn); } - return new RevisionNoteMap<>(noteMap, ImmutableMap.copyOf(result)); + return new RevisionNoteMap<>(noteMap, result.build()); } static > RevisionNoteMap emptyMap() { return new RevisionNoteMap<>(NoteMap.newEmptyMap(), ImmutableMap.of()); } - - private RevisionNoteMap(NoteMap noteMap, ImmutableMap revisionNotes) { - this.noteMap = noteMap; - this.revisionNotes = revisionNotes; - } }