Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.notedb; 16 : 17 : import com.google.common.collect.ImmutableMap; 18 : import com.google.gerrit.entities.Comment; 19 : import java.io.IOException; 20 : import org.eclipse.jgit.errors.ConfigInvalidException; 21 : import org.eclipse.jgit.lib.ObjectId; 22 : import org.eclipse.jgit.lib.ObjectReader; 23 : import org.eclipse.jgit.notes.Note; 24 : import org.eclipse.jgit.notes.NoteMap; 25 : 26 : /** 27 : * A utility class that parses a NoteMap into commit => comment list data. 28 : * 29 : * @param <T> the RevisionNote for the comment type. 30 : */ 31 : class RevisionNoteMap<T extends RevisionNote<? extends Comment>> { 32 : /** CommitID => blob ID */ 33 : final NoteMap noteMap; 34 : 35 : /** CommitID => parsed data */ 36 : final ImmutableMap<ObjectId, T> revisionNotes; 37 : 38 103 : private RevisionNoteMap(NoteMap noteMap, ImmutableMap<ObjectId, T> revisionNotes) { 39 103 : this.noteMap = noteMap; 40 103 : this.revisionNotes = revisionNotes; 41 103 : } 42 : 43 : static RevisionNoteMap<ChangeRevisionNote> parse( 44 : ChangeNoteJson noteJson, ObjectReader reader, NoteMap noteMap, Comment.Status status) 45 : throws ConfigInvalidException, IOException { 46 103 : ImmutableMap.Builder<ObjectId, ChangeRevisionNote> result = ImmutableMap.builder(); 47 103 : for (Note note : noteMap) { 48 64 : ChangeRevisionNote rn = new ChangeRevisionNote(noteJson, reader, note.getData(), status); 49 64 : rn.parse(); 50 : 51 64 : result.put(note.copy(), rn); 52 64 : } 53 103 : return new RevisionNoteMap<>(noteMap, result.build()); 54 : } 55 : 56 : static RevisionNoteMap<RobotCommentsRevisionNote> parseRobotComments( 57 : ChangeNoteJson changeNoteJson, ObjectReader reader, NoteMap noteMap) 58 : throws ConfigInvalidException, IOException { 59 9 : ImmutableMap.Builder<ObjectId, RobotCommentsRevisionNote> result = ImmutableMap.builder(); 60 9 : for (Note note : noteMap) { 61 9 : RobotCommentsRevisionNote rn = 62 9 : new RobotCommentsRevisionNote(changeNoteJson, reader, note.getData()); 63 9 : rn.parse(); 64 9 : result.put(note.copy(), rn); 65 9 : } 66 9 : return new RevisionNoteMap<>(noteMap, result.build()); 67 : } 68 : 69 : static <T extends RevisionNote<? extends Comment>> RevisionNoteMap<T> emptyMap() { 70 9 : return new RevisionNoteMap<>(NoteMap.newEmptyMap(), ImmutableMap.of()); 71 : } 72 : }