Java-8ify ReviewDbUtil

We can use method references within ReviewDbUtil to cut down on some
code. However, going through usages didn't show a uniform benefit to
using native Java 8 Comparator constructs. The naive replacement:

  comparing(Change.Id::get)

is shorter and more readable when it's correct, but it's not always
correct in the presence of nulls.

Ordering is also probably not going away from Guava, as it provides
some handy methods that are not (yet?) present on Comparator. For
example, creating a sorted copy of a Collection with Comparator is
arguably readable, but is not exactly short:

  myList.stream().sorted(comparing(Change.Id::get)).collect(toList())

Compared with the Ordering alernative:

  ReviewDbUtil.intKeyOrdering().sortedCopy(myList)

This benefit is greater when dealing with Iterable, which does not
have a readily available stream() method.

Leave some guidance in the intKeyOrdering Javadoc with advice.

Change-Id: Ib645abf060302cfc1c6d83f691e94f1dbf7c6db5
This commit is contained in:
Dave Borowitz
2016-09-20 09:01:21 -04:00
parent e45363dcbd
commit e128d6f413
9 changed files with 53 additions and 49 deletions

View File

@@ -34,7 +34,6 @@ import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.ReviewerSet;
import com.google.gerrit.server.StarredChangesUtil;
import com.google.gerrit.server.index.FieldDef;
@@ -694,8 +693,7 @@ public class ChangeField {
@Override
public Iterable<Integer> get(ChangeData input, FillArgs args)
throws OrmException {
return Iterables.transform(input.stars().keySet(),
ReviewDbUtil.INT_KEY_FUNCTION);
return Iterables.transform(input.stars().keySet(), Account.Id::get);
}
};