WebLinks: Convert to java.util, streams, and ImmutableList

Change-Id: I119489c22ce03ec091a81712228bc24bb4909117
This commit is contained in:
Dave Borowitz
2019-05-16 16:15:01 +02:00
committed by David Pursehouse
parent 60bcae2a3c
commit 9fff4e87fe
8 changed files with 50 additions and 43 deletions

View File

@@ -14,10 +14,11 @@
package com.google.gerrit.server; package com.google.gerrit.server;
import com.google.common.base.Function; import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.base.Predicate;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.common.DiffWebLinkInfo; import com.google.gerrit.extensions.common.DiffWebLinkInfo;
import com.google.gerrit.extensions.common.WebLinkInfo; import com.google.gerrit.extensions.common.WebLinkInfo;
@@ -35,8 +36,8 @@ import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.Collections; import java.util.function.Function;
import java.util.List; import java.util.function.Predicate;
@Singleton @Singleton
public class WebLinks { public class WebLinks {
@@ -87,7 +88,7 @@ public class WebLinks {
* @param commit SHA1 of commit. * @param commit SHA1 of commit.
* @return Links for patch sets. * @return Links for patch sets.
*/ */
public List<WebLinkInfo> getPatchSetLinks(Project.NameKey project, String commit) { public ImmutableList<WebLinkInfo> getPatchSetLinks(Project.NameKey project, String commit) {
return filterLinks(patchSetLinks, webLink -> webLink.getPatchSetWebLink(project.get(), commit)); return filterLinks(patchSetLinks, webLink -> webLink.getPatchSetWebLink(project.get(), commit));
} }
@@ -96,7 +97,7 @@ public class WebLinks {
* @param revision SHA1 of the parent revision. * @param revision SHA1 of the parent revision.
* @return Links for patch sets. * @return Links for patch sets.
*/ */
public List<WebLinkInfo> getParentLinks(Project.NameKey project, String revision) { public ImmutableList<WebLinkInfo> getParentLinks(Project.NameKey project, String revision) {
return filterLinks(parentLinks, webLink -> webLink.getParentWebLink(project.get(), revision)); return filterLinks(parentLinks, webLink -> webLink.getParentWebLink(project.get(), revision));
} }
@@ -106,9 +107,9 @@ public class WebLinks {
* @param file File name. * @param file File name.
* @return Links for files. * @return Links for files.
*/ */
public List<WebLinkInfo> getFileLinks(String project, String revision, String file) { public ImmutableList<WebLinkInfo> getFileLinks(String project, String revision, String file) {
return Patch.isMagic(file) return Patch.isMagic(file)
? Collections.emptyList() ? ImmutableList.of()
: filterLinks(fileLinks, webLink -> webLink.getFileWebLink(project, revision, file)); : filterLinks(fileLinks, webLink -> webLink.getFileWebLink(project, revision, file));
} }
@@ -118,14 +119,15 @@ public class WebLinks {
* @param file File name. * @param file File name.
* @return Links for file history * @return Links for file history
*/ */
public List<WebLinkInfo> getFileHistoryLinks(String project, String revision, String file) { public ImmutableList<WebLinkInfo> getFileHistoryLinks(
String project, String revision, String file) {
if (Patch.isMagic(file)) { if (Patch.isMagic(file)) {
return Collections.emptyList(); return ImmutableList.of();
} }
return FluentIterable.from(fileHistoryLinks) return Streams.stream(fileHistoryLinks)
.transform(webLink -> webLink.getFileHistoryWebLink(project, revision, file)) .map(webLink -> webLink.getFileHistoryWebLink(project, revision, file))
.filter(INVALID_WEBLINK) .filter(INVALID_WEBLINK)
.toList(); .collect(toImmutableList());
} }
/** /**
@@ -138,20 +140,20 @@ public class WebLinks {
* @param fileB File name of side B. * @param fileB File name of side B.
* @return Links for file diffs. * @return Links for file diffs.
*/ */
public List<DiffWebLinkInfo> getDiffLinks( public ImmutableList<DiffWebLinkInfo> getDiffLinks(
final String project, String project,
final int changeId, int changeId,
final Integer patchSetIdA, Integer patchSetIdA,
final String revisionA, String revisionA,
final String fileA, String fileA,
final int patchSetIdB, int patchSetIdB,
final String revisionB, String revisionB,
final String fileB) { String fileB) {
if (Patch.isMagic(fileA) || Patch.isMagic(fileB)) { if (Patch.isMagic(fileA) || Patch.isMagic(fileB)) {
return Collections.emptyList(); return ImmutableList.of();
} }
return FluentIterable.from(diffLinks) return Streams.stream(diffLinks)
.transform( .map(
webLink -> webLink ->
webLink.getDiffLink( webLink.getDiffLink(
project, project,
@@ -163,14 +165,14 @@ public class WebLinks {
revisionB, revisionB,
fileB)) fileB))
.filter(INVALID_WEBLINK) .filter(INVALID_WEBLINK)
.toList(); .collect(toImmutableList());
} }
/** /**
* @param project Project name. * @param project Project name.
* @return Links for projects. * @return Links for projects.
*/ */
public List<WebLinkInfo> getProjectLinks(String project) { public ImmutableList<WebLinkInfo> getProjectLinks(String project) {
return filterLinks(projectLinks, webLink -> webLink.getProjectWeblink(project)); return filterLinks(projectLinks, webLink -> webLink.getProjectWeblink(project));
} }
@@ -179,7 +181,7 @@ public class WebLinks {
* @param branch Branch name * @param branch Branch name
* @return Links for branches. * @return Links for branches.
*/ */
public List<WebLinkInfo> getBranchLinks(String project, String branch) { public ImmutableList<WebLinkInfo> getBranchLinks(String project, String branch) {
return filterLinks(branchLinks, webLink -> webLink.getBranchWebLink(project, branch)); return filterLinks(branchLinks, webLink -> webLink.getBranchWebLink(project, branch));
} }
@@ -188,12 +190,15 @@ public class WebLinks {
* @param tag Tag name * @param tag Tag name
* @return Links for tags. * @return Links for tags.
*/ */
public List<WebLinkInfo> getTagLinks(String project, String tag) { public ImmutableList<WebLinkInfo> getTagLinks(String project, String tag) {
return filterLinks(tagLinks, webLink -> webLink.getTagWebLink(project, tag)); return filterLinks(tagLinks, webLink -> webLink.getTagWebLink(project, tag));
} }
private <T extends WebLink> List<WebLinkInfo> filterLinks( private <T extends WebLink> ImmutableList<WebLinkInfo> filterLinks(
DynamicSet<T> links, Function<T, WebLinkInfo> transformer) { DynamicSet<T> links, Function<T, WebLinkInfo> transformer) {
return FluentIterable.from(links).transform(transformer).filter(INVALID_WEBLINK).toList(); return Streams.stream(links)
.map(transformer)
.filter(INVALID_WEBLINK)
.collect(toImmutableList());
} }
} }

View File

@@ -28,6 +28,7 @@ import static com.google.gerrit.extensions.client.ListChangesOption.PUSH_CERTIFI
import static com.google.gerrit.extensions.client.ListChangesOption.WEB_LINKS; import static com.google.gerrit.extensions.client.ListChangesOption.WEB_LINKS;
import static com.google.gerrit.server.CommonConverters.toGitPerson; import static com.google.gerrit.server.CommonConverters.toGitPerson;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
@@ -71,7 +72,6 @@ import com.google.inject.assistedinject.Assisted;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
@@ -182,7 +182,7 @@ public class RevisionJson {
info.message = commit.getFullMessage(); info.message = commit.getFullMessage();
if (addLinks) { if (addLinks) {
List<WebLinkInfo> links = webLinks.getPatchSetLinks(project, commit.name()); ImmutableList<WebLinkInfo> links = webLinks.getPatchSetLinks(project, commit.name());
info.webLinks = links.isEmpty() ? null : links; info.webLinks = links.isEmpty() ? null : links;
} }
@@ -192,7 +192,7 @@ public class RevisionJson {
i.commit = parent.name(); i.commit = parent.name();
i.subject = parent.getShortMessage(); i.subject = parent.getShortMessage();
if (addLinks) { if (addLinks) {
List<WebLinkInfo> parentLinks = webLinks.getParentLinks(project, parent.name()); ImmutableList<WebLinkInfo> parentLinks = webLinks.getParentLinks(project, parent.name());
i.webLinks = parentLinks.isEmpty() ? null : parentLinks; i.webLinks = parentLinks.isEmpty() ? null : parentLinks;
} }
info.parents.add(i); info.parents.add(i);

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.project;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.data.LabelType; import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelValue; import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.extensions.common.LabelTypeInfo; import com.google.gerrit.extensions.common.LabelTypeInfo;
@@ -29,7 +30,6 @@ import com.google.gerrit.server.config.AllProjectsName;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
@Singleton @Singleton
public class ProjectJson { public class ProjectJson {
@@ -65,7 +65,7 @@ public class ProjectJson {
info.description = Strings.emptyToNull(p.getDescription()); info.description = Strings.emptyToNull(p.getDescription());
info.state = p.getState(); info.state = p.getState();
info.id = Url.encode(info.name); info.id = Url.encode(info.name);
List<WebLinkInfo> links = webLinks.getProjectLinks(p.getName()); ImmutableList<WebLinkInfo> links = webLinks.getProjectLinks(p.getName());
info.webLinks = links.isEmpty() ? null : links; info.webLinks = links.isEmpty() ? null : links;
return info; return info;
} }

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.restapi.change; package com.google.gerrit.server.restapi.change;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.extensions.common.DiffWebLinkInfo; import com.google.gerrit.extensions.common.DiffWebLinkInfo;
import com.google.gerrit.extensions.common.EditInfo; import com.google.gerrit.extensions.common.EditInfo;
import com.google.gerrit.extensions.common.Input; import com.google.gerrit.extensions.common.Input;
@@ -380,7 +381,7 @@ public class ChangeEdits implements ChildCollection<ChangeResource, ChangeEditRe
FileInfo r = new FileInfo(); FileInfo r = new FileInfo();
ChangeEdit edit = rsrc.getChangeEdit(); ChangeEdit edit = rsrc.getChangeEdit();
Change change = edit.getChange(); Change change = edit.getChange();
List<DiffWebLinkInfo> links = ImmutableList<DiffWebLinkInfo> links =
webLinks.getDiffLinks( webLinks.getDiffLinks(
change.getProject().get(), change.getProject().get(),
change.getChangeId(), change.getChangeId(),

View File

@@ -201,7 +201,7 @@ public class GetDiff implements RestReadView<FileResource> {
? resource.getRevision().getEdit().get().getRefName() ? resource.getRevision().getEdit().get().getRefName()
: resource.getRevision().getPatchSet().refName(); : resource.getRevision().getPatchSet().refName();
List<DiffWebLinkInfo> links = ImmutableList<DiffWebLinkInfo> links =
webLinks.getDiffLinks( webLinks.getDiffLinks(
state.getName(), state.getName(),
resource.getPatchKey().patchSetId().changeId().get(), resource.getPatchKey().patchSetId().changeId().get(),
@@ -273,7 +273,7 @@ public class GetDiff implements RestReadView<FileResource> {
} }
private List<WebLinkInfo> getFileWebLinks(Project project, String rev, String file) { private List<WebLinkInfo> getFileWebLinks(Project project, String rev, String file) {
List<WebLinkInfo> links = webLinks.getFileLinks(project.getName(), rev, file); ImmutableList<WebLinkInfo> links = webLinks.getFileLinks(project.getName(), rev, file);
return links.isEmpty() ? null : links; return links.isEmpty() ? null : links;
} }

View File

@@ -278,7 +278,8 @@ public class ListBranches implements RestReadView<ProjectResource> {
info.actions.put(d.getId(), new ActionInfo(d)); info.actions.put(d.getId(), new ActionInfo(d));
} }
List<WebLinkInfo> links = webLinks.getBranchLinks(projectState.getName(), ref.getName()); ImmutableList<WebLinkInfo> links =
webLinks.getBranchLinks(projectState.getName(), ref.getName());
info.webLinks = links.isEmpty() ? null : links; info.webLinks = links.isEmpty() ? null : links;
return info; return info;
} }

View File

@@ -505,7 +505,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
continue; continue;
} }
List<WebLinkInfo> links = webLinks.getProjectLinks(projectName.get()); ImmutableList<WebLinkInfo> links = webLinks.getProjectLinks(projectName.get());
info.webLinks = links.isEmpty() ? null : links; info.webLinks = links.isEmpty() ? null : links;
if (stdout == null || format.isJson()) { if (stdout == null || format.isJson()) {

View File

@@ -182,7 +182,7 @@ public class ListTags implements RestReadView<ProjectResource> {
perm.testOrFalse(RefPermission.DELETE) && projectState.statePermitsWrite() ? true : null; perm.testOrFalse(RefPermission.DELETE) && projectState.statePermitsWrite() ? true : null;
} }
List<WebLinkInfo> webLinks = links.getTagLinks(projectState.getName(), ref.getName()); ImmutableList<WebLinkInfo> webLinks = links.getTagLinks(projectState.getName(), ref.getName());
if (object instanceof RevTag) { if (object instanceof RevTag) {
// Annotated or signed tag // Annotated or signed tag
RevTag tag = (RevTag) object; RevTag tag = (RevTag) object;