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

@@ -14,6 +14,7 @@
package com.google.gerrit.audit;
import com.google.auto.value.AutoValue;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
@@ -36,41 +37,14 @@ public class AuditEvent {
public final long elapsed;
public final UUID uuid;
public static class UUID {
protected final String uuid;
protected UUID() {
uuid = String.format("audit:%s", java.util.UUID.randomUUID().toString());
@AutoValue
public abstract static class UUID {
private static UUID create() {
return new AutoValue_AuditEvent_UUID(
String.format("audit:%s", java.util.UUID.randomUUID().toString()));
}
public UUID(final String n) {
uuid = n;
}
public String get() {
return uuid;
}
@Override
public int hashCode() {
return uuid.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof UUID)) {
return false;
}
return uuid.equals(((UUID) obj).uuid);
}
public abstract String uuid();
}
/**
@@ -93,7 +67,7 @@ public class AuditEvent {
this.when = when;
this.timeAtStart = this.when;
this.params = MoreObjects.firstNonNull(params, EMPTY_PARAMS);
this.uuid = new UUID();
this.uuid = UUID.create();
this.result = result;
this.elapsed = TimeUtil.nowMs() - timeAtStart;
}
@@ -116,6 +90,6 @@ public class AuditEvent {
@Override
public String toString() {
return String.format("AuditEvent UUID:%s, SID:%s, TS:%d, who:%s, what:%s",
uuid.get(), sessionId, when, who, what);
uuid.uuid(), sessionId, when, who, what);
}
}