Add Change.Id#toRefPrefix() and use from PatchSet.Id#toRefName().

Presize the StringBuilder; the default size for new StringBuilder() is
16, but "refs/changes/01/1/1" is 19 characters, guaranteeing a wasted
allocation.

Change-Id: I1ac3e5ff40b4994e7b05979e9bb65c4e6c59d7a9
This commit is contained in:
Dave Borowitz
2015-10-16 09:36:01 -04:00
parent 24b688269c
commit bc981e4e02
4 changed files with 36 additions and 15 deletions

View File

@@ -125,6 +125,23 @@ public final class Change {
id = newValue;
}
public String toRefPrefix() {
return refPrefixBuilder().toString();
}
StringBuilder refPrefixBuilder() {
StringBuilder r = new StringBuilder(32)
.append(REFS_CHANGES);
int m = id % 100;
if (m < 10) {
r.append('0');
}
return r.append(m)
.append('/')
.append(id)
.append('/');
}
/** Parse a Change.Id out of a string representation. */
public static Id parse(final String str) {
final Id r = new Id();

View File

@@ -14,8 +14,6 @@
package com.google.gerrit.reviewdb.client;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_CHANGES;
import com.google.gwtorm.client.Column;
import com.google.gwtorm.client.IntKey;
@@ -109,19 +107,9 @@ public final class PatchSet {
}
public String toRefName() {
StringBuilder r = new StringBuilder();
r.append(REFS_CHANGES);
int change = changeId.get();
int m = change % 100;
if (m < 10) {
r.append('0');
}
r.append(m);
r.append('/');
r.append(change);
r.append('/');
r.append(patchSetId);
return r.toString();
return changeId.refPrefixBuilder()
.append(patchSetId)
.toString();
}
/** Parse a PatchSet.Id out of a string representation. */