Convert Patch.Key to AutoValue

See I6982fb24 for context.

Change-Id: I9087151a4022dca4b9d6d591495ea9020370f14a
This commit is contained in:
Dave Borowitz
2019-04-19 07:48:47 -07:00
parent 7eaa2f9ff5
commit 245af2a3f8
12 changed files with 47 additions and 55 deletions

View File

@@ -150,7 +150,7 @@ public class CatServlet extends HttpServlet {
return; return;
} }
String path = patchKey.getFileName(); String path = patchKey.fileName();
String restUrl = String restUrl =
String.format( String.format(
"%s/changes/%d/revisions/%s/files/%s/download?parent=%d", "%s/changes/%d/revisions/%s/files/%s/download?parent=%d",

View File

@@ -14,7 +14,12 @@
package com.google.gerrit.reviewdb.client; package com.google.gerrit.reviewdb.client;
import com.google.gwtorm.client.StringKey; import static com.google.common.base.Preconditions.checkArgument;
import com.google.auto.value.AutoValue;
import com.google.common.base.Splitter;
import com.google.common.primitives.Ints;
import java.util.List;
/** A single modified file in a {@link PatchSet}. */ /** A single modified file in a {@link PatchSet}. */
public final class Patch { public final class Patch {
@@ -35,46 +40,33 @@ public final class Patch {
return COMMIT_MSG.equals(path) || MERGE_LIST.equals(path); return COMMIT_MSG.equals(path) || MERGE_LIST.equals(path);
} }
public static class Key extends StringKey<PatchSet.Id> { public static Key key(PatchSet.Id patchSetId, String fileName) {
private static final long serialVersionUID = 1L; return new AutoValue_Patch_Key(patchSetId, fileName);
}
protected PatchSet.Id patchSetId; @AutoValue
public abstract static class Key {
protected String fileName; /** Parse a Patch.Key out of a string representation. */
protected Key() {
patchSetId = new PatchSet.Id();
}
public Key(PatchSet.Id ps, String name) {
this.patchSetId = ps;
this.fileName = name;
}
@Override
public PatchSet.Id getParentKey() {
return patchSetId;
}
@Override
public String get() {
return fileName;
}
@Override
protected void set(String newValue) {
fileName = newValue;
}
/** Parse a Patch.Id out of a string representation. */
public static Key parse(String str) { public static Key parse(String str) {
final Key r = new Key(); List<String> parts = Splitter.on(',').limit(3).splitToList(str);
r.fromString(str); checkKeyFormat(parts.size() == 3, str);
return r; Integer changeId = Ints.tryParse(parts.get(0));
checkKeyFormat(changeId != null, str);
Integer patchSetNum = Ints.tryParse(parts.get(1));
checkKeyFormat(patchSetNum != null, str);
return key(new PatchSet.Id(new Change.Id(changeId), patchSetNum), parts.get(2));
} }
public String getFileName() { private static void checkKeyFormat(boolean test, String input) {
return get(); checkArgument(test, "invalid patch key: %s", input);
}
public abstract PatchSet.Id patchSetId();
public abstract String fileName();
public PatchSet.Id getParentKey() {
return patchSetId();
} }
} }
@@ -259,7 +251,7 @@ public final class Patch {
} }
public String getFileName() { public String getFileName() {
return key.fileName; return key.fileName();
} }
public String getSourceFileName() { public String getSourceFileName() {

View File

@@ -57,7 +57,7 @@ public final class PatchLineComment {
public static PatchLineComment from( public static PatchLineComment from(
Change.Id changeId, PatchLineComment.Status status, Comment c) { Change.Id changeId, PatchLineComment.Status status, Comment c) {
Patch.Key patchKey = new Patch.Key(new PatchSet.Id(changeId, c.key.patchSetId), c.key.filename); Patch.Key patchKey = Patch.key(new PatchSet.Id(changeId, c.key.patchSetId), c.key.filename);
PatchLineComment plc = PatchLineComment plc =
new PatchLineComment( new PatchLineComment(
patchKey, c.key.uuid, c.lineNbr, c.author.getId(), c.parentUuid, c.writtenOn); patchKey, c.key.uuid, c.lineNbr, c.author.getId(), c.parentUuid, c.writtenOn);
@@ -259,7 +259,7 @@ public final class PatchLineComment {
public Comment asComment(String serverId) { public Comment asComment(String serverId) {
Comment c = Comment c =
new Comment( new Comment(
new Comment.Key(uuid, patchKey.getFileName(), patchKey.getParentKey().get()), new Comment.Key(uuid, patchKey.fileName(), patchKey.getParentKey().get()),
author, author,
writtenOn, writtenOn,
side, side,

View File

@@ -29,7 +29,7 @@ public class FileResource implements RestResource {
public FileResource(RevisionResource rev, String name) { public FileResource(RevisionResource rev, String name) {
this.rev = rev; this.rev = rev;
this.key = new Patch.Key(rev.getPatchSet().getId(), name); this.key = Patch.key(rev.getPatchSet().getId(), name);
} }
public Patch.Key getPatchKey() { public Patch.Key getPatchKey() {

View File

@@ -229,7 +229,7 @@ public class PatchListEntry {
} }
Patch toPatch(PatchSet.Id setId) { Patch toPatch(PatchSet.Id setId) {
final Patch p = new Patch(new Patch.Key(setId, getNewName())); final Patch p = new Patch(Patch.key(setId, getNewName()));
p.setChangeType(getChangeType()); p.setChangeType(getChangeType());
p.setPatchType(getPatchType()); p.setPatchType(getPatchType());
p.setSourceFileName(getOldName()); p.setSourceFileName(getOldName());

View File

@@ -319,12 +319,12 @@ public class PatchScriptFactory implements Callable<PatchScript> {
} }
} }
Patch p = new Patch(new Patch.Key(ps.getId(), name)); Patch p = new Patch(Patch.key(ps.getId(), name));
history.add(p); history.add(p);
byKey.put(p.getKey(), p); byKey.put(p.getKey(), p);
} }
if (edit != null && edit.isPresent()) { if (edit != null && edit.isPresent()) {
Patch p = new Patch(new Patch.Key(new PatchSet.Id(psb.getParentKey(), 0), fileName)); Patch p = new Patch(Patch.key(new PatchSet.Id(psb.getParentKey(), 0), fileName));
history.add(p); history.add(p);
byKey.put(p.getKey(), p); byKey.put(p.getKey(), p);
} }
@@ -386,7 +386,7 @@ public class PatchScriptFactory implements Callable<PatchScript> {
for (Comment c : commentsUtil.publishedByChangeFile(notes, file)) { for (Comment c : commentsUtil.publishedByChangeFile(notes, file)) {
comments.include(notes.getChangeId(), c); comments.include(notes.getChangeId(), c);
PatchSet.Id psId = new PatchSet.Id(notes.getChangeId(), c.key.patchSetId); PatchSet.Id psId = new PatchSet.Id(notes.getChangeId(), c.key.patchSetId);
Patch.Key pKey = new Patch.Key(psId, c.key.filename); Patch.Key pKey = Patch.key(psId, c.key.filename);
Patch p = byKey.get(pKey); Patch p = byKey.get(pKey);
if (p != null) { if (p != null) {
p.setCommentCount(p.getCommentCount() + 1); p.setCommentCount(p.getCommentCount() + 1);
@@ -398,7 +398,7 @@ public class PatchScriptFactory implements Callable<PatchScript> {
for (Comment c : commentsUtil.draftByChangeFileAuthor(notes, file, me)) { for (Comment c : commentsUtil.draftByChangeFileAuthor(notes, file, me)) {
comments.include(notes.getChangeId(), c); comments.include(notes.getChangeId(), c);
PatchSet.Id psId = new PatchSet.Id(notes.getChangeId(), c.key.patchSetId); PatchSet.Id psId = new PatchSet.Id(notes.getChangeId(), c.key.patchSetId);
Patch.Key pKey = new Patch.Key(psId, c.key.filename); Patch.Key pKey = Patch.key(psId, c.key.filename);
Patch p = byKey.get(pKey); Patch p = byKey.get(pKey);
if (p != null) { if (p != null) {
p.setDraftCount(p.getDraftCount() + 1); p.setDraftCount(p.getDraftCount() + 1);

View File

@@ -43,7 +43,7 @@ public class DownloadContent implements RestReadView<FileResource> {
@Override @Override
public BinaryResult apply(FileResource rsrc) public BinaryResult apply(FileResource rsrc)
throws ResourceNotFoundException, IOException, NoSuchChangeException { throws ResourceNotFoundException, IOException, NoSuchChangeException {
String path = rsrc.getPatchKey().get(); String path = rsrc.getPatchKey().fileName();
RevisionResource rev = rsrc.getRevision(); RevisionResource rev = rsrc.getRevision();
ObjectId revstr = ObjectId.fromString(rev.getPatchSet().getRevision().get()); ObjectId revstr = ObjectId.fromString(rev.getPatchSet().getRevision().get());
return fileContentUtil.downloadContent( return fileContentUtil.downloadContent(

View File

@@ -97,7 +97,7 @@ public class GetBlame implements RestReadView<FileResource> {
RevCommit revCommit = revWalk.parseCommit(objectId); RevCommit revCommit = revWalk.parseCommit(objectId);
RevCommit[] parents = revCommit.getParents(); RevCommit[] parents = revCommit.getParents();
String path = resource.getPatchKey().getFileName(); String path = resource.getPatchKey().fileName();
List<BlameInfo> result; List<BlameInfo> result;
if (!base) { if (!base) {

View File

@@ -63,7 +63,7 @@ public class GetContent implements RestReadView<FileResource> {
@Override @Override
public BinaryResult apply(FileResource rsrc) public BinaryResult apply(FileResource rsrc)
throws ResourceNotFoundException, IOException, BadRequestException { throws ResourceNotFoundException, IOException, BadRequestException {
String path = rsrc.getPatchKey().get(); String path = rsrc.getPatchKey().fileName();
if (Patch.COMMIT_MSG.equals(path)) { if (Patch.COMMIT_MSG.equals(path)) {
String msg = getMessage(rsrc.getRevision().getChangeResource().getNotes()); String msg = getMessage(rsrc.getRevision().getChangeResource().getNotes());
return BinaryResult.create(msg) return BinaryResult.create(msg)

View File

@@ -141,7 +141,7 @@ public class GetDiff implements RestReadView<FileResource> {
PatchScriptFactory psf; PatchScriptFactory psf;
PatchSet basePatchSet = null; PatchSet basePatchSet = null;
PatchSet.Id pId = resource.getPatchKey().getParentKey(); PatchSet.Id pId = resource.getPatchKey().getParentKey();
String fileName = resource.getPatchKey().getFileName(); String fileName = resource.getPatchKey().fileName();
ChangeNotes notes = resource.getRevision().getNotes(); ChangeNotes notes = resource.getRevision().getNotes();
if (base != null) { if (base != null) {
RevisionResource baseResource = RevisionResource baseResource =

View File

@@ -42,7 +42,7 @@ public class Reviewed {
s.markReviewed( s.markReviewed(
resource.getPatchKey().getParentKey(), resource.getPatchKey().getParentKey(),
resource.getAccountId(), resource.getAccountId(),
resource.getPatchKey().getFileName())); resource.getPatchKey().fileName()));
return reviewFlagUpdated ? Response.created("") : Response.ok(""); return reviewFlagUpdated ? Response.created("") : Response.ok("");
} }
} }
@@ -63,7 +63,7 @@ public class Reviewed {
s.clearReviewed( s.clearReviewed(
resource.getPatchKey().getParentKey(), resource.getPatchKey().getParentKey(),
resource.getAccountId(), resource.getAccountId(),
resource.getPatchKey().getFileName())); resource.getPatchKey().fileName()));
return Response.none(); return Response.none();
} }
} }

View File

@@ -34,9 +34,9 @@ public class PatchTest extends GerritBaseTests {
@Test @Test
public void parseKey() { public void parseKey() {
assertThat(Patch.Key.parse("1,2,foo.txt")) assertThat(Patch.Key.parse("1,2,foo.txt"))
.isEqualTo(new Patch.Key(new PatchSet.Id(new Change.Id(1), 2), "foo.txt")); .isEqualTo(Patch.key(new PatchSet.Id(new Change.Id(1), 2), "foo.txt"));
assertThat(Patch.Key.parse("01,02,foo.txt")) assertThat(Patch.Key.parse("01,02,foo.txt"))
.isEqualTo(new Patch.Key(new PatchSet.Id(new Change.Id(1), 2), "foo.txt")); .isEqualTo(Patch.key(new PatchSet.Id(new Change.Id(1), 2), "foo.txt"));
assertInvalidKey(null); assertInvalidKey(null);
assertInvalidKey(""); assertInvalidKey("");
assertInvalidKey("1,2"); assertInvalidKey("1,2");