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; 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. */ /** Parse a Change.Id out of a string representation. */
public static Id parse(final String str) { public static Id parse(final String str) {
final Id r = new Id(); final Id r = new Id();

View File

@@ -14,8 +14,6 @@
package com.google.gerrit.reviewdb.client; 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.Column;
import com.google.gwtorm.client.IntKey; import com.google.gwtorm.client.IntKey;
@@ -109,19 +107,9 @@ public final class PatchSet {
} }
public String toRefName() { public String toRefName() {
StringBuilder r = new StringBuilder(); return changeId.refPrefixBuilder()
r.append(REFS_CHANGES); .append(patchSetId)
int change = changeId.get(); .toString();
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();
} }
/** Parse a PatchSet.Id out of a string representation. */ /** Parse a PatchSet.Id out of a string representation. */

View File

@@ -71,6 +71,14 @@ public class ChangeTest {
assertNotRef("refs/changes/01/1/1/meta"); assertNotRef("refs/changes/01/1/1/meta");
} }
@Test
public void toRefPrefix() {
assertThat(new Change.Id(1).toRefPrefix())
.isEqualTo("refs/changes/01/1/");
assertThat(new Change.Id(1234).toRefPrefix())
.isEqualTo("refs/changes/34/1234/");
}
private static void assertRef(int changeId, String refName) { private static void assertRef(int changeId, String refName) {
assertThat(Change.Id.fromRef(refName)).isEqualTo(new Change.Id(changeId)); assertThat(Change.Id.fromRef(refName)).isEqualTo(new Change.Id(changeId));
} }

View File

@@ -83,6 +83,14 @@ public class PatchSetTest {
assertThat(joinGroups(ImmutableList.of("", "cd"))).isEqualTo(",cd"); assertThat(joinGroups(ImmutableList.of("", "cd"))).isEqualTo(",cd");
} }
@Test
public void testToRefName() {
assertThat(new PatchSet.Id(new Change.Id(1), 23).toRefName())
.isEqualTo("refs/changes/01/1/23");
assertThat(new PatchSet.Id(new Change.Id(1234), 5).toRefName())
.isEqualTo("refs/changes/34/1234/5");
}
private static void assertRef(int changeId, int psId, String refName) { private static void assertRef(int changeId, int psId, String refName) {
assertThat(PatchSet.isChangeRef(refName)).isTrue(); assertThat(PatchSet.isChangeRef(refName)).isTrue();
assertThat(PatchSet.Id.fromRef(refName)) assertThat(PatchSet.Id.fromRef(refName))