Convert ChangeTriplet to @AutoValue

Instead of throwing an exception when parsing fails, use Optional.

Change-Id: I124eb2717ae625c6a0cbd4739ae0600236cfc11d
This commit is contained in:
Dave Borowitz 2014-12-10 07:51:20 -08:00
parent af4b19079d
commit 665e7bda73
2 changed files with 40 additions and 34 deletions

View File

@ -20,6 +20,7 @@ import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
@ -560,15 +561,14 @@ public class ChangeUtil {
} }
// Try change triplet // Try change triplet
ChangeTriplet triplet; Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id);
try { if (triplet.isPresent()) {
triplet = new ChangeTriplet(id); return db.get().changes().byBranchKey(
} catch (ChangeTriplet.ParseException e) { triplet.get().branch(),
throw new ResourceNotFoundException(id); triplet.get().id()).toList();
} }
return db.get().changes().byBranchKey(
triplet.getBranchNameKey(), throw new ResourceNotFoundException(id);
triplet.getChangeKey()).toList();
} }
private IdentifiedUser user() { private IdentifiedUser user() {

View File

@ -14,6 +14,8 @@
package com.google.gerrit.server.change; package com.google.gerrit.server.change;
import com.google.auto.value.AutoValue;
import com.google.common.base.Optional;
import com.google.gerrit.extensions.restapi.Url; import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.reviewdb.client.Branch; import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@ -21,17 +23,30 @@ import com.google.gerrit.reviewdb.client.Project;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
public class ChangeTriplet { @AutoValue
public abstract class ChangeTriplet {
public static String format(Change change) {
return format(change.getDest(), change.getKey());
}
private final Change.Key changeKey; private static String format(Branch.NameKey branch, Change.Key change) {
private final Project.NameKey projectNameKey; return branch.getParentKey().get()
private final Branch.NameKey branchNameKey; + "~" + branch.getShortName()
+ "~" + change.get();
}
public ChangeTriplet(final String triplet) throws ParseException { /**
* Parse a triplet out of a string.
*
* @param triplet string of the form "project~branch~id".
* @return the triplet if the input string has the proper format, or absent if
* not.
*/
public static Optional<ChangeTriplet> parse(String triplet) {
int t2 = triplet.lastIndexOf('~'); int t2 = triplet.lastIndexOf('~');
int t1 = triplet.lastIndexOf('~', t2 - 1); int t1 = triplet.lastIndexOf('~', t2 - 1);
if (t1 < 0 || t2 < 0) { if (t1 < 0 || t2 < 0) {
throw new ParseException(); return Optional.absent();
} }
String project = Url.decode(triplet.substring(0, t1)); String project = Url.decode(triplet.substring(0, t1));
@ -42,30 +57,21 @@ public class ChangeTriplet {
branch = Constants.R_HEADS + branch; branch = Constants.R_HEADS + branch;
} }
changeKey = new Change.Key(changeId); ChangeTriplet result = new AutoValue_ChangeTriplet(
projectNameKey = new Project.NameKey(project); new Branch.NameKey(new Project.NameKey(project), branch),
branchNameKey = new Branch.NameKey(projectNameKey, branch); new Change.Key(changeId));
return Optional.of(result);
} }
public Change.Key getChangeKey() { public final Project.NameKey project() {
return changeKey; return branch().getParentKey();
} }
public Branch.NameKey getBranchNameKey() { public abstract Branch.NameKey branch();
return branchNameKey; public abstract Change.Key id();
}
public static String format(final Change change) { @Override
return change.getProject().get() + "~" public String toString() {
+ change.getDest().getShortName() + "~" return format(branch(), id());
+ change.getKey().get();
}
public static class ParseException extends Exception {
private static final long serialVersionUID = 1L;
ParseException() {
super();
}
} }
} }