Merge "Improve readability, build ImmutableMap directly"

This commit is contained in:
Joerg Zieren
2020-01-31 12:23:22 +00:00
committed by Gerrit Code Review

View File

@@ -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 <T> the RevisionNote for the comment type.
*/
class RevisionNoteMap<T extends RevisionNote<? extends Comment>> {
// CommitID => blob ID
/** CommitID => blob ID */
final NoteMap noteMap;
// CommitID => parsed data, immutable map.
/** CommitID => parsed data */
final ImmutableMap<ObjectId, T> revisionNotes;
private RevisionNoteMap(NoteMap noteMap, ImmutableMap<ObjectId, T> revisionNotes) {
this.noteMap = noteMap;
this.revisionNotes = revisionNotes;
}
static RevisionNoteMap<ChangeRevisionNote> parse(
ChangeNoteJson noteJson, ObjectReader reader, NoteMap noteMap, Comment.Status status)
throws ConfigInvalidException, IOException {
Map<ObjectId, ChangeRevisionNote> result = new HashMap<>();
ImmutableMap.Builder<ObjectId, ChangeRevisionNote> 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<RobotCommentsRevisionNote> parseRobotComments(
ChangeNoteJson changeNoteJson, ObjectReader reader, NoteMap noteMap)
throws ConfigInvalidException, IOException {
Map<ObjectId, RobotCommentsRevisionNote> result = new HashMap<>();
ImmutableMap.Builder<ObjectId, RobotCommentsRevisionNote> 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 <T extends RevisionNote<? extends Comment>> RevisionNoteMap<T> emptyMap() {
return new RevisionNoteMap<>(NoteMap.newEmptyMap(), ImmutableMap.of());
}
private RevisionNoteMap(NoteMap noteMap, ImmutableMap<ObjectId, T> revisionNotes) {
this.noteMap = noteMap;
this.revisionNotes = revisionNotes;
}
}