Convert some value classes to AutoValue

These classes were found by searching for custom hashCode
implementations, and omitting some cases:

 - Classes requiring custom serialization, which is not supported[1][2].
 - Most instances with custom hashCode or equals implementations,
   where the code savings is not as significant.
 - All classes in the extension API. We may migrate these eventually,
   but let's avoid this large backwards incompatible change until
   we're more used to AutoValue elsewhere.
 - All classes in the UI package.[3]

There are likely still more value classes that were missed by this
search that do not implement equals or hashCode.

[1] https://github.com/google/auto/tree/master/value#serialization
[2] This excludes, among other things, all persistent cache keys. It
might be possible to convert persistent caches to use a key
marshalling strategy other than Java serialization, but probably not
without invalidating all existing entries.
[3] This should still be possible as generated classes are generally
GWT compatible.

Change-Id: I96796b9879b7e487b80949b63115ac4032180f8b
This commit is contained in:
Dave Borowitz
2014-11-09 17:57:45 -08:00
parent c87d29ec16
commit eba26614e5
24 changed files with 159 additions and 332 deletions

View File

@@ -16,18 +16,18 @@ package com.google.gerrit.server.util;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import java.util.Objects;
/** A single vote on a label, consisting of a label name and a value. */
public class LabelVote {
@AutoValue
public abstract class LabelVote {
public static LabelVote parse(String text) {
checkArgument(!Strings.isNullOrEmpty(text), "Empty label vote");
if (text.charAt(0) == '-') {
return new LabelVote(text.substring(1), (short) 0);
return create(text.substring(1), (short) 0);
}
short sign = 0;
int i;
@@ -44,9 +44,9 @@ public class LabelVote {
}
}
if (sign == 0) {
return new LabelVote(text, (short) 1);
return create(text, (short) 1);
}
return new LabelVote(text.substring(0, i),
return create(text.substring(0, i),
(short)(sign * Short.parseShort(text.substring(i + 1))));
}
@@ -54,63 +54,39 @@ public class LabelVote {
checkArgument(!Strings.isNullOrEmpty(text), "Empty label vote");
int e = text.lastIndexOf('=');
checkArgument(e >= 0, "Label vote missing '=': %s", text);
return new LabelVote(text.substring(0, e),
return create(text.substring(0, e),
Short.parseShort(text.substring(e + 1), text.length()));
}
private final String name;
private final short value;
public LabelVote(String name, short value) {
this.name = LabelType.checkNameInternal(name);
this.value = value;
public static LabelVote create(String label, short value) {
return new AutoValue_LabelVote(LabelType.checkNameInternal(label), value);
}
public LabelVote(PatchSetApproval psa) {
this(psa.getLabel(), psa.getValue());
public static LabelVote create(PatchSetApproval psa) {
return create(psa.getLabel(), psa.getValue());
}
public String getLabel() {
return name;
}
public short getValue() {
return value;
}
public abstract String label();
public abstract short value();
public String format() {
if (value == (short) 0) {
return '-' + name;
} else if (value < 0) {
return name + value;
if (value() == (short) 0) {
return '-' + label();
} else if (value() < 0) {
return label() + value();
} else {
return name + '+' + value;
return label() + '+' + value();
}
}
public String formatWithEquals() {
if (value <= (short) 0) {
return name + '=' + value;
if (value() <= (short) 0) {
return label() + '=' + value();
} else {
return name + "=+" + value;
return label() + "=+" + value();
}
}
@Override
public boolean equals(Object o) {
if (o instanceof LabelVote) {
LabelVote l = (LabelVote) o;
return Objects.equals(name, l.name)
&& value == l.value;
}
return false;
}
@Override
public int hashCode() {
return 17 * value + name.hashCode();
}
@Override
public String toString() {
return format();