Convert simple anonymous Comparators to comparing()

Where a comparator is obviously comparing one or more derived keys,
either manually or with ComparisonChain, replace with the equivalent
comparing() call. Not all comparators are simple to translate in this
way, such as ChunkManager#getDiffChunkComparator. Even though these
cases could be improved, or, quite likely, they contain bugs, that work
is left for a future change.

In the common case of creating a comparator to pass to Collections#sort
to sort a newly-created ArrayList, replace these with equivalent stream
expressions. Again, not all cases of Collections#sort are simple enough
to tackle here.

In some of these cases, we could probably further alter the types to
more aggressively use immutable types. Generally speaking, that would
introduce ripple effects which would make this change less focused and
difficult to review. Don't do that, in order to limit the cleanup here.

Change-Id: I098d3820927367ee98c96698481cb0edcceb3d64
This commit is contained in:
Dave Borowitz
2018-08-29 10:25:59 -07:00
parent 2bc3667c6a
commit 62210a3166
24 changed files with 133 additions and 310 deletions

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.restapi.project;
import static com.google.gerrit.reviewdb.client.RefNames.isConfigRef;
import static java.util.Comparator.comparing;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.extensions.api.projects.ProjectApi.ListRefsRequest;
@@ -40,7 +41,6 @@ import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
@@ -135,14 +135,7 @@ public class ListTags implements RestReadView<ProjectResource> {
}
}
Collections.sort(
tags,
new Comparator<TagInfo>() {
@Override
public int compare(TagInfo a, TagInfo b) {
return a.ref.compareTo(b.ref);
}
});
Collections.sort(tags, comparing(t -> t.ref));
return new RefFilter<TagInfo>(Constants.R_TAGS)
.start(start)