NoteDb: Use java.util.Optional

This has the benefit of using only one Optional type in
ChangeNotesParser, but we have to work around the fact that Guava's
Enums.getIfPresent returns a Guava Optional.

Change-Id: I344530a9246b770a198cc433b7b06cce3a131964
This commit is contained in:
Dave Borowitz
2016-10-13 16:04:19 -04:00
parent b06cfdbe4b
commit 8069ac9268
2 changed files with 22 additions and 23 deletions

View File

@@ -35,7 +35,6 @@ import static java.util.stream.Collectors.joining;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
import com.google.common.base.Enums; import com.google.common.base.Enums;
import com.google.common.base.Optional;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBasedTable;
@@ -90,6 +89,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NavigableSet; import java.util.NavigableSet;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
@@ -213,7 +213,7 @@ class ChangeNotesParser {
topic, topic,
originalSubject, originalSubject,
submissionId, submissionId,
assignee != null ? assignee.orNull() : null, assignee != null ? assignee.orElse(null) : null,
status, status,
Sets.newLinkedHashSet(Lists.reverse(pastAssignees)), Sets.newLinkedHashSet(Lists.reverse(pastAssignees)),
@@ -503,10 +503,10 @@ class ChangeNotesParser {
Optional<Account.Id> parsedAssignee; Optional<Account.Id> parsedAssignee;
if (assigneeValue.equals("")) { if (assigneeValue.equals("")) {
// Empty footer found, assignee deleted // Empty footer found, assignee deleted
parsedAssignee = Optional.absent(); parsedAssignee = Optional.empty();
} else { } else {
PersonIdent ident = RawParseUtils.parsePersonIdent(assigneeValue); PersonIdent ident = RawParseUtils.parsePersonIdent(assigneeValue);
parsedAssignee = Optional.fromNullable(noteUtil.parseIdent(ident, id)); parsedAssignee = Optional.ofNullable(noteUtil.parseIdent(ident, id));
} }
if (assignee == null) { if (assignee == null) {
assignee = parsedAssignee; assignee = parsedAssignee;
@@ -538,12 +538,12 @@ class ChangeNotesParser {
} else if (statusLines.size() > 1) { } else if (statusLines.size() > 1) {
throw expectedOneFooter(FOOTER_STATUS, statusLines); throw expectedOneFooter(FOOTER_STATUS, statusLines);
} }
Optional<Change.Status> status = Enums.getIfPresent( Change.Status status = Enums.getIfPresent(
Change.Status.class, statusLines.get(0).toUpperCase()); Change.Status.class, statusLines.get(0).toUpperCase()).orNull();
if (!status.isPresent()) { if (status == null) {
throw invalidFooter(FOOTER_STATUS, statusLines.get(0)); throw invalidFooter(FOOTER_STATUS, statusLines.get(0));
} }
return status.get(); return status;
} }
private PatchSet.Id parsePatchSetId(ChangeNotesCommit commit) private PatchSet.Id parsePatchSetId(ChangeNotesCommit commit)
@@ -567,10 +567,11 @@ class ChangeNotesParser {
} }
String withParens = psIdLine.substring(s + 1); String withParens = psIdLine.substring(s + 1);
if (withParens.startsWith("(") && withParens.endsWith(")")) { if (withParens.startsWith("(") && withParens.endsWith(")")) {
Optional<PatchSetState> state = Enums.getIfPresent(PatchSetState.class, PatchSetState state = Enums.getIfPresent(PatchSetState.class,
withParens.substring(1, withParens.length() - 1).toUpperCase()); withParens.substring(1, withParens.length() - 1).toUpperCase())
if (state.isPresent()) { .orNull();
return state.get(); if (state != null) {
return state;
} }
} }
throw invalidFooter(FOOTER_PATCH_SET, psIdLine); throw invalidFooter(FOOTER_PATCH_SET, psIdLine);
@@ -787,10 +788,9 @@ class ChangeNotesParser {
submitRecords.add(rec); submitRecords.add(rec);
int s = line.indexOf(' '); int s = line.indexOf(' ');
String statusStr = s >= 0 ? line.substring(0, s) : line; String statusStr = s >= 0 ? line.substring(0, s) : line;
Optional<SubmitRecord.Status> status = rec.status =
Enums.getIfPresent(SubmitRecord.Status.class, statusStr); Enums.getIfPresent(SubmitRecord.Status.class, statusStr).orNull();
checkFooter(status.isPresent(), FOOTER_SUBMITTED_WITH, line); checkFooter(rec.status != null, FOOTER_SUBMITTED_WITH, line);
rec.status = status.get();
if (s >= 0) { if (s >= 0) {
rec.errorMessage = line.substring(s); rec.errorMessage = line.substring(s);
} }
@@ -802,10 +802,9 @@ class ChangeNotesParser {
} }
rec.labels.add(label); rec.labels.add(label);
Optional<SubmitRecord.Label.Status> status = Enums.getIfPresent( label.status = Enums.getIfPresent(
SubmitRecord.Label.Status.class, line.substring(0, c)); SubmitRecord.Label.Status.class, line.substring(0, c)).orNull();
checkFooter(status.isPresent(), FOOTER_SUBMITTED_WITH, line); checkFooter(label.status != null, FOOTER_SUBMITTED_WITH, line);
label.status = status.get();
int c2 = line.indexOf(": ", c + 2); int c2 = line.indexOf(": ", c + 2);
if (c2 >= 0) { if (c2 >= 0) {
label.label = line.substring(c + 2, c2); label.label = line.substring(c + 2, c2);

View File

@@ -39,7 +39,6 @@ import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
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.collect.Table; import com.google.common.collect.Table;
@@ -84,6 +83,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
@@ -284,7 +284,7 @@ public class ChangeUpdate extends AbstractChangeUpdate {
} }
public void removeApprovalFor(Account.Id reviewer, String label) { public void removeApprovalFor(Account.Id reviewer, String label) {
approvals.put(label, reviewer, Optional.<Short> absent()); approvals.put(label, reviewer, Optional.empty());
} }
public void merge(RequestId submissionId, public void merge(RequestId submissionId,
@@ -414,7 +414,7 @@ public class ChangeUpdate extends AbstractChangeUpdate {
} }
public void removeAssignee() { public void removeAssignee() {
this.assignee = Optional.absent(); this.assignee = Optional.empty();
} }
public Map<Account.Id, ReviewerStateInternal> getReviewers() { public Map<Account.Id, ReviewerStateInternal> getReviewers() {