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;
}
String path = patchKey.getFileName();
String path = patchKey.fileName();
String restUrl =
String.format(
"%s/changes/%d/revisions/%s/files/%s/download?parent=%d",

View File

@@ -14,7 +14,12 @@
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}. */
public final class Patch {
@@ -35,46 +40,33 @@ public final class Patch {
return COMMIT_MSG.equals(path) || MERGE_LIST.equals(path);
}
public static class Key extends StringKey<PatchSet.Id> {
private static final long serialVersionUID = 1L;
public static Key key(PatchSet.Id patchSetId, String fileName) {
return new AutoValue_Patch_Key(patchSetId, fileName);
}
protected PatchSet.Id patchSetId;
protected String fileName;
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. */
@AutoValue
public abstract static class Key {
/** Parse a Patch.Key out of a string representation. */
public static Key parse(String str) {
final Key r = new Key();
r.fromString(str);
return r;
List<String> parts = Splitter.on(',').limit(3).splitToList(str);
checkKeyFormat(parts.size() == 3, str);
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() {
return get();
private static void checkKeyFormat(boolean test, String input) {
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() {
return key.fileName;
return key.fileName();
}
public String getSourceFileName() {

View File

@@ -57,7 +57,7 @@ public final class PatchLineComment {
public static PatchLineComment from(
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 =
new PatchLineComment(
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) {
Comment c =
new Comment(
new Comment.Key(uuid, patchKey.getFileName(), patchKey.getParentKey().get()),
new Comment.Key(uuid, patchKey.fileName(), patchKey.getParentKey().get()),
author,
writtenOn,
side,

View File

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

View File

@@ -229,7 +229,7 @@ public class PatchListEntry {
}
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.setPatchType(getPatchType());
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);
byKey.put(p.getKey(), p);
}
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);
byKey.put(p.getKey(), p);
}
@@ -386,7 +386,7 @@ public class PatchScriptFactory implements Callable<PatchScript> {
for (Comment c : commentsUtil.publishedByChangeFile(notes, file)) {
comments.include(notes.getChangeId(), c);
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);
if (p != null) {
p.setCommentCount(p.getCommentCount() + 1);
@@ -398,7 +398,7 @@ public class PatchScriptFactory implements Callable<PatchScript> {
for (Comment c : commentsUtil.draftByChangeFileAuthor(notes, file, me)) {
comments.include(notes.getChangeId(), c);
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);
if (p != null) {
p.setDraftCount(p.getDraftCount() + 1);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -34,9 +34,9 @@ public class PatchTest extends GerritBaseTests {
@Test
public void parseKey() {
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"))
.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("");
assertInvalidKey("1,2");