From 6724990a322180b493dab45c9e4a82862e0f9026 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Fri, 31 Aug 2018 13:26:57 -0700 Subject: [PATCH] Avoid Collections#sort With the introduction of default methods in Java 8, Java finally grew a List#sort method that can be used in place of the static Collections#sort. Convert simple in-place sorts that need to be in-place sorts to use this method where possible. During the course of this change, it became obvious that many instances of sort were just sorting a newly-created ArrayList in place. These can be replaced with more idiomatic Stream constructions, sometimes even eliminating a loop to populate the list. One difference between List#sort and Collections#sort is there is no List#sort() with no arguments; callers must always pass either naturalOrder() or a null comparators. In this change, there were so few remaining instances of sorting by natural order that typing naturalOrder() didn't seem too repetitious, and it is quite readable. Change-Id: I4d89421a72127e9a36cbd32aeac425c0471a1b3f --- .../google/gerrit/client/info/ChangeInfo.java | 6 +- .../google/gerrit/client/info/FileInfo.java | 4 +- .../google/gerrit/client/rpc/NativeMap.java | 8 +-- .../client/account/MyGpgKeysScreen.java | 3 +- .../client/account/MyIdentitiesScreen.java | 5 +- .../client/admin/AccessSectionEditor.java | 8 +-- .../gerrit/client/admin/GroupTable.java | 3 +- .../google/gerrit/client/change/Labels.java | 63 ++++++++++--------- .../google/gerrit/client/change/ReplyBox.java | 17 +++-- .../changes/AccountDashboardScreen.java | 2 +- .../client/dashboards/DashboardsTable.java | 3 +- .../client/diff/CommentsCollections.java | 3 +- .../gerrit/client/ui/ProjectsTable.java | 3 +- .../java/net/codemirror/mode/ModeInfo.java | 3 +- .../httpd/plugins/HttpPluginServlet.java | 4 +- .../google/gerrit/index/query/AndSource.java | 8 +-- .../google/gerrit/pgm/init/InitPlugins.java | 3 +- .../google/gerrit/server/CommentsUtil.java | 2 +- .../gerrit/server/change/WalkSorter.java | 3 +- .../gerrit/server/events/EventFactory.java | 6 +- .../google/gerrit/server/git/MergeUtil.java | 2 +- .../gerrit/server/git/meta/TabFile.java | 10 +-- .../server/mail/send/CommentSender.java | 2 +- .../server/notedb/ChangeNotesParser.java | 5 +- .../server/notedb/LegacyChangeNoteWrite.java | 7 +-- .../server/patch/DiffSummaryLoader.java | 6 +- .../server/patch/PatchScriptBuilder.java | 3 +- .../server/permissions/SectionSortCache.java | 3 +- .../gerrit/server/project/ProjectConfig.java | 7 +-- .../server/restapi/change/CommentJson.java | 5 +- .../restapi/change/ListRevisionDrafts.java | 3 +- .../restapi/change/ListRobotComments.java | 3 +- .../server/restapi/group/GetAuditLog.java | 3 +- .../server/restapi/group/ListSubgroups.java | 4 +- .../server/restapi/project/ListBranches.java | 3 +- .../server/restapi/project/ListTags.java | 3 +- .../server/submit/SubmitStrategyOp.java | 4 +- .../gerrit/sshd/commands/ShowConnections.java | 46 +++++++------- 38 files changed, 130 insertions(+), 146 deletions(-) diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/ChangeInfo.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/ChangeInfo.java index 3587bf8519..0f786a6d66 100644 --- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/ChangeInfo.java +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/ChangeInfo.java @@ -32,7 +32,6 @@ import com.google.gwt.core.client.JsArrayString; import com.google.gwtjsonrpc.client.impl.ser.JavaSqlTimestamp_JsonSerializer; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -448,9 +447,8 @@ public class ChangeInfo extends JavaScriptObject { public static void sortRevisionInfoByNumber(JsArray list) { final int editParent = findEditParent(list); - Collections.sort( - Natives.asList(list), - comparing(r -> !r.isEdit() ? 2 * (r._number() - 1) + 1 : 2 * editParent)); + Natives.asList(list) + .sort(comparing(r -> !r.isEdit() ? 2 * (r._number() - 1) + 1 : 2 * editParent)); } public static int findEditParent(JsArray list) { diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/FileInfo.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/FileInfo.java index 345a260cee..fc3dbf1b04 100644 --- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/FileInfo.java +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/FileInfo.java @@ -19,7 +19,6 @@ import com.google.gerrit.common.data.FilenameComparator; import com.google.gerrit.reviewdb.client.Patch; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; -import java.util.Collections; import java.util.Comparator; public class FileInfo extends JavaScriptObject { @@ -55,8 +54,7 @@ public class FileInfo extends JavaScriptObject { public final native void _row(int r) /*-{ this._row = r }-*/; public static void sortFileInfoByPath(JsArray list) { - Collections.sort( - Natives.asList(list), Comparator.comparing(FileInfo::path, FilenameComparator.INSTANCE)); + Natives.asList(list).sort(Comparator.comparing(FileInfo::path, FilenameComparator.INSTANCE)); } public static String getFileName(String path) { diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java index 4b170687c9..41306ff2fd 100644 --- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/rpc/NativeMap.java @@ -14,11 +14,12 @@ package com.google.gerrit.client.rpc; +import static java.util.stream.Collectors.toCollection; + import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.user.client.rpc.AsyncCallback; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Set; @@ -57,10 +58,7 @@ public class NativeMap extends JavaScriptObject { } public final List sortedKeys() { - Set keys = keySet(); - List sorted = new ArrayList<>(keys); - Collections.sort(sorted); - return sorted; + return keySet().stream().sorted().collect(toCollection(ArrayList::new)); } public final native JsArray values() /*-{ diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyGpgKeysScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyGpgKeysScreen.java index bbd94f27e7..1a0090a01f 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyGpgKeysScreen.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyGpgKeysScreen.java @@ -42,7 +42,6 @@ import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwtexpui.clippy.client.CopyableLabel; import com.google.gwtexpui.globalkey.client.NpTextArea; import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class MyGpgKeysScreen extends SettingsScreen { @@ -119,7 +118,7 @@ public class MyGpgKeysScreen extends SettingsScreen { List list = Natives.asList(result.values()); // TODO(dborowitz): Sort on something more meaningful, like // created date? - Collections.sort(list, comparing(GpgKeyInfo::id)); + list.sort(comparing(GpgKeyInfo::id)); keys.clear(); keyText.setText(""); errorPanel.setVisible(false); diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyIdentitiesScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyIdentitiesScreen.java index 5c6d40f389..730d98e185 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyIdentitiesScreen.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyIdentitiesScreen.java @@ -14,6 +14,8 @@ package com.google.gerrit.client.account; +import static java.util.Comparator.naturalOrder; + import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.VoidResult; import com.google.gerrit.client.rpc.GerritCallback; @@ -29,7 +31,6 @@ import com.google.gwt.user.client.Window.Location; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.CheckBox; import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; -import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -169,7 +170,7 @@ public class MyIdentitiesScreen extends SettingsScreen { void display(JsArray results) { List idList = Natives.asList(results); - Collections.sort(idList); + idList.sort(naturalOrder()); while (1 < table.getRowCount()) { table.removeRow(table.getRowCount() - 1); diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccessSectionEditor.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccessSectionEditor.java index 1b946cdeaa..7bd8b825d4 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccessSectionEditor.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccessSectionEditor.java @@ -14,6 +14,8 @@ package com.google.gerrit.client.admin; +import static java.util.stream.Collectors.toCollection; + import com.google.gerrit.client.Gerrit; import com.google.gerrit.common.data.AccessSection; import com.google.gerrit.common.data.LabelType; @@ -45,7 +47,6 @@ import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.ValueListBox; import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class AccessSectionEditor extends Composite @@ -205,9 +206,8 @@ public class AccessSectionEditor extends Composite } private void sortPermissions(AccessSection accessSection) { - List permissionList = new ArrayList<>(accessSection.getPermissions()); - Collections.sort(permissionList); - accessSection.setPermissions(permissionList); + accessSection.setPermissions( + accessSection.getPermissions().stream().sorted().collect(toCollection(ArrayList::new))); } void setEditing(boolean editing) { diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/GroupTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/GroupTable.java index c09f93622a..be0db41ac1 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/GroupTable.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/GroupTable.java @@ -34,7 +34,6 @@ import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; import com.google.gwt.user.client.ui.HTMLTable.Cell; import com.google.gwt.user.client.ui.Image; -import java.util.Collections; import java.util.List; public class GroupTable extends NavigationTable { @@ -106,7 +105,7 @@ public class GroupTable extends NavigationTable { table.removeRow(table.getRowCount() - 1); } - Collections.sort(list, comparing(GroupInfo::name)); + list.sort(comparing(GroupInfo::name)); for (GroupInfo group : list.subList(fromIndex, toIndex)) { final int row = table.getRowCount(); table.insertRow(row); diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/Labels.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/Labels.java index 1f4820f22a..801a9274f2 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/Labels.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/Labels.java @@ -14,6 +14,10 @@ package com.google.gerrit.client.change; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toList; + import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.changes.ChangeApi; import com.google.gerrit.client.changes.Util; @@ -135,9 +139,12 @@ class Labels extends Grid { } void set(ChangeInfo info) { - List names = new ArrayList<>(info.labels()); + List names = + info.labels() + .stream() + .sorted() + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); Set removable = info.removableReviewerIds(); - Collections.sort(names); resize(names.size(), 2); @@ -197,8 +204,7 @@ class Labels extends Grid { } private static List sort(Set keySet, int a, int b) { - List r = new ArrayList<>(keySet); - Collections.sort(r); + List r = keySet.stream().sorted().collect(toCollection(ArrayList::new)); if (keySet.contains(a)) { r.remove(Integer.valueOf(a)); r.add(0, a); @@ -238,31 +244,32 @@ class Labels extends Grid { Set removable, String label, Map votable) { - List users = new ArrayList<>(in); - Collections.sort( - users, - new Comparator() { - @Override - public int compare(AccountInfo a, AccountInfo b) { - String as = name(a); - String bs = name(b); - if (as.isEmpty()) { - return 1; - } else if (bs.isEmpty()) { - return -1; - } - return as.compareTo(bs); - } + List users = + in.stream() + .sorted( + new Comparator() { + @Override + public int compare(AccountInfo a, AccountInfo b) { + String as = name(a); + String bs = name(b); + if (as.isEmpty()) { + return 1; + } else if (bs.isEmpty()) { + return -1; + } + return as.compareTo(bs); + } - private String name(AccountInfo a) { - if (a.name() != null) { - return a.name(); - } else if (a.email() != null) { - return a.email(); - } - return ""; - } - }); + private String name(AccountInfo a) { + if (a.name() != null) { + return a.name(); + } else if (a.email() != null) { + return a.email(); + } + return ""; + } + }) + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); SafeHtmlBuilder html = new SafeHtmlBuilder(); Iterator itr = users.iterator(); diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ReplyBox.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ReplyBox.java index 0bbd6149e6..8a1a2d5367 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ReplyBox.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ReplyBox.java @@ -16,6 +16,8 @@ package com.google.gerrit.client.change; import static com.google.gwt.event.dom.client.KeyCodes.KEY_ENTER; import static com.google.gwt.event.dom.client.KeyCodes.KEY_MAC_ENTER; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.changes.ChangeApi; @@ -123,11 +125,15 @@ public class ReplyBox extends Composite { this.lc = new LocalComments(project, psId.getParentKey()); initWidget(uiBinder.createAndBindUi(this)); - List names = new ArrayList<>(permitted.keySet()); + List names = + permitted + .keySet() + .stream() + .sorted() + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); if (names.isEmpty()) { UIObject.setVisible(labelsParent, false); } else { - Collections.sort(names); renderLabels(names, all, permitted); } @@ -439,8 +445,11 @@ public class ReplyBox extends Composite { clp, project, psId, Util.C.commitMessage(), copyPath(Patch.MERGE_LIST, l))); } - List paths = new ArrayList<>(m.keySet()); - Collections.sort(paths); + List paths = + m.keySet() + .stream() + .sorted() + .collect(collectingAndThen(toList(), Collections::unmodifiableList)); for (String path : paths) { if (!Patch.isMagic(path)) { diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/AccountDashboardScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/AccountDashboardScreen.java index 495a3d4661..7ec1102115 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/AccountDashboardScreen.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/AccountDashboardScreen.java @@ -178,7 +178,7 @@ public class AccountDashboardScreen extends Screen implements ChangeListScreen { } } - Collections.sort(Natives.asList(out), outComparator()); + Natives.asList(out).sort(outComparator()); table.updateColumnsForLabels(wip, out, in, done); workInProgress.display(wip); diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/dashboards/DashboardsTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/dashboards/DashboardsTable.java index 1a2f502b56..dcb9c01861 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/dashboards/DashboardsTable.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/dashboards/DashboardsTable.java @@ -27,7 +27,6 @@ import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; import com.google.gwt.user.client.ui.Image; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,7 +75,7 @@ public class DashboardsTable extends NavigationTable { table.removeRow(table.getRowCount() - 1); } - Collections.sort(list, comparing(DashboardInfo::id)); + list.sort(comparing(DashboardInfo::id)); String ref = null; for (DashboardInfo d : list) { diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/diff/CommentsCollections.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/diff/CommentsCollections.java index 5db62bd20f..2698584ed2 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/diff/CommentsCollections.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/diff/CommentsCollections.java @@ -29,7 +29,6 @@ import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.Project; import com.google.gwt.core.client.JsArray; import com.google.gwt.user.client.rpc.AsyncCallback; -import java.util.Collections; /** Collection of published and draft comments loaded from the server. */ class CommentsCollections { @@ -159,7 +158,7 @@ class CommentsCollections { for (CommentInfo c : Natives.asList(in)) { c.path(path); } - Collections.sort(Natives.asList(in), comparing(CommentInfo::updated)); + Natives.asList(in).sort(comparing(CommentInfo::updated)); } return in; } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/ProjectsTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/ProjectsTable.java index 74f04ffc76..3576b12598 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/ProjectsTable.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/ProjectsTable.java @@ -22,7 +22,6 @@ import com.google.gerrit.client.projects.ProjectMap; import com.google.gerrit.client.rpc.Natives; import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; import com.google.gwt.user.client.ui.Image; -import java.util.Collections; import java.util.List; public class ProjectsTable extends NavigationTable { @@ -70,7 +69,7 @@ public class ProjectsTable extends NavigationTable { } List list = Natives.asList(projects.values()); - Collections.sort(list, comparing(ProjectInfo::name)); + list.sort(comparing(ProjectInfo::name)); for (ProjectInfo p : list.subList(fromIndex, toIndex)) { insert(table.getRowCount(), p); } diff --git a/gerrit-gwtui/src/main/java/net/codemirror/mode/ModeInfo.java b/gerrit-gwtui/src/main/java/net/codemirror/mode/ModeInfo.java index ff12a07e39..9df066dc0f 100644 --- a/gerrit-gwtui/src/main/java/net/codemirror/mode/ModeInfo.java +++ b/gerrit-gwtui/src/main/java/net/codemirror/mode/ModeInfo.java @@ -23,7 +23,6 @@ import com.google.gwt.core.client.JsArray; import com.google.gwt.core.client.JsArrayString; import com.google.gwt.resources.client.DataResource; import com.google.gwt.safehtml.shared.SafeUri; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -243,7 +242,7 @@ public class ModeInfo extends JavaScriptObject { byMime.put(m.mode(), m); } } - Collections.sort(Natives.asList(filtered), comparing((ModeInfo m) -> m.name().toLowerCase())); + Natives.asList(filtered).sort(comparing(m -> m.name().toLowerCase())); setAll(filtered); } diff --git a/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java b/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java index 9a24e47cd8..74cadd3b9a 100644 --- a/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java +++ b/java/com/google/gerrit/httpd/plugins/HttpPluginServlet.java @@ -456,8 +456,8 @@ class HttpPluginServlet extends HttpServlet implements StartPluginListener, Relo } } - Collections.sort(cmds, PluginEntry.COMPARATOR_BY_NAME); - Collections.sort(docs, PluginEntry.COMPARATOR_BY_NAME); + cmds.sort(PluginEntry.COMPARATOR_BY_NAME); + docs.sort(PluginEntry.COMPARATOR_BY_NAME); StringBuilder md = new StringBuilder(); md.append(String.format("# Plugin %s #\n", pluginName)); diff --git a/java/com/google/gerrit/index/query/AndSource.java b/java/com/google/gerrit/index/query/AndSource.java index e2605f4c4f..d1e1c30b09 100644 --- a/java/com/google/gerrit/index/query/AndSource.java +++ b/java/com/google/gerrit/index/query/AndSource.java @@ -15,6 +15,7 @@ package com.google.gerrit.index.query; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.ImmutableList.toImmutableList; import com.google.common.base.Throwables; import com.google.common.collect.FluentIterable; @@ -26,7 +27,6 @@ import com.google.gwtorm.server.OrmRuntimeException; import com.google.gwtorm.server.ResultSet; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -175,10 +175,8 @@ public class AndSource extends AndPredicate return cardinality; } - private List> sort(Collection> that) { - List> r = new ArrayList<>(that); - Collections.sort(r, this); - return r; + private ImmutableList> sort(Collection> that) { + return that.stream().sorted(this).collect(toImmutableList()); } @Override diff --git a/java/com/google/gerrit/pgm/init/InitPlugins.java b/java/com/google/gerrit/pgm/init/InitPlugins.java index 7f5590308b..e43114ce81 100644 --- a/java/com/google/gerrit/pgm/init/InitPlugins.java +++ b/java/com/google/gerrit/pgm/init/InitPlugins.java @@ -30,7 +30,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.jar.Attributes; import java.util.jar.JarFile; @@ -66,7 +65,7 @@ public class InitPlugins implements InitStep { } result.add(new PluginData(pluginName, pluginVersion, tmpPlugin)); }); - Collections.sort(result, comparing(p -> p.name)); + result.sort(comparing(p -> p.name)); return result; } diff --git a/java/com/google/gerrit/server/CommentsUtil.java b/java/com/google/gerrit/server/CommentsUtil.java index 18d9b3d4e7..99dfbbb1a2 100644 --- a/java/com/google/gerrit/server/CommentsUtil.java +++ b/java/com/google/gerrit/server/CommentsUtil.java @@ -502,7 +502,7 @@ public class CommentsUtil { } private static List sort(List comments) { - Collections.sort(comments, COMMENT_ORDER); + comments.sort(COMMENT_ORDER); return comments; } diff --git a/java/com/google/gerrit/server/change/WalkSorter.java b/java/com/google/gerrit/server/change/WalkSorter.java index cff1ac7732..916a62bb36 100644 --- a/java/com/google/gerrit/server/change/WalkSorter.java +++ b/java/com/google/gerrit/server/change/WalkSorter.java @@ -34,7 +34,6 @@ import java.io.IOException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Deque; import java.util.HashSet; import java.util.List; @@ -110,7 +109,7 @@ public class WalkSorter { for (Map.Entry> e : byProject.asMap().entrySet()) { sortedByProject.add(sortProject(e.getKey(), e.getValue())); } - Collections.sort(sortedByProject, PROJECT_LIST_SORTER); + sortedByProject.sort(PROJECT_LIST_SORTER); return Iterables.concat(sortedByProject); } diff --git a/java/com/google/gerrit/server/events/EventFactory.java b/java/com/google/gerrit/server/events/EventFactory.java index 2fbc1c7478..fbce4b273f 100644 --- a/java/com/google/gerrit/server/events/EventFactory.java +++ b/java/com/google/gerrit/server/events/EventFactory.java @@ -73,7 +73,6 @@ import java.io.IOException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -329,10 +328,9 @@ public class EventFactory { } } // Sort by original parent order. - Collections.sort( - ca.dependsOn, + ca.dependsOn.sort( comparing( - (DependencyAttribute d) -> { + d -> { for (int i = 0; i < parentNames.size(); i++) { if (parentNames.get(i).equals(d.revision)) { return i; diff --git a/java/com/google/gerrit/server/git/MergeUtil.java b/java/com/google/gerrit/server/git/MergeUtil.java index 0231378b4d..686be1973e 100644 --- a/java/com/google/gerrit/server/git/MergeUtil.java +++ b/java/com/google/gerrit/server/git/MergeUtil.java @@ -220,7 +220,7 @@ public class MergeUtil { } catch (IOException e) { throw new IntegrationException("Branch head sorting failed", e); } - Collections.sort(result, CodeReviewCommit.ORDER); + result.sort(CodeReviewCommit.ORDER); return result; } diff --git a/java/com/google/gerrit/server/git/meta/TabFile.java b/java/com/google/gerrit/server/git/meta/TabFile.java index ef25cd85b1..4c0378a0ec 100644 --- a/java/com/google/gerrit/server/git/meta/TabFile.java +++ b/java/com/google/gerrit/server/git/meta/TabFile.java @@ -14,13 +14,15 @@ package com.google.gerrit.server.git.meta; +import static com.google.common.collect.ImmutableList.toImmutableList; + +import com.google.common.collect.ImmutableList; import com.google.gerrit.server.git.ValidationError; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -122,10 +124,8 @@ public class TabFile { return buf.toString(); } - protected static > List sort(Collection m) { - ArrayList r = new ArrayList<>(m); - Collections.sort(r); - return r; + protected static > ImmutableList sort(Collection m) { + return m.stream().sorted().collect(toImmutableList()); } protected static String pad(int len, String src) { diff --git a/java/com/google/gerrit/server/mail/send/CommentSender.java b/java/com/google/gerrit/server/mail/send/CommentSender.java index 54176e2981..0baaa11c1d 100644 --- a/java/com/google/gerrit/server/mail/send/CommentSender.java +++ b/java/com/google/gerrit/server/mail/send/CommentSender.java @@ -239,7 +239,7 @@ public class CommentSender extends ReplyToChangeSender { } } - Collections.sort(groups, Comparator.comparing(g -> g.filename, FilenameComparator.INSTANCE)); + groups.sort(Comparator.comparing(g -> g.filename, FilenameComparator.INSTANCE)); return groups; } diff --git a/java/com/google/gerrit/server/notedb/ChangeNotesParser.java b/java/com/google/gerrit/server/notedb/ChangeNotesParser.java index 4eeab81133..cbb7020207 100644 --- a/java/com/google/gerrit/server/notedb/ChangeNotesParser.java +++ b/java/com/google/gerrit/server/notedb/ChangeNotesParser.java @@ -79,7 +79,6 @@ import java.nio.charset.Charset; import java.sql.Timestamp; import java.text.ParseException; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -297,9 +296,7 @@ class ChangeNotesParser { } result.put(a.getPatchSetId(), a); } - for (Collection v : result.asMap().values()) { - Collections.sort((List) v, ChangeNotes.PSA_BY_TIME); - } + result.keySet().forEach(k -> result.get(k).sort(ChangeNotes.PSA_BY_TIME)); return result; } diff --git a/java/com/google/gerrit/server/notedb/LegacyChangeNoteWrite.java b/java/com/google/gerrit/server/notedb/LegacyChangeNoteWrite.java index 7931d8850f..c9711b537d 100644 --- a/java/com/google/gerrit/server/notedb/LegacyChangeNoteWrite.java +++ b/java/com/google/gerrit/server/notedb/LegacyChangeNoteWrite.java @@ -15,10 +15,12 @@ package com.google.gerrit.server.notedb; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.gerrit.server.CommentsUtil.COMMENT_ORDER; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ListMultimap; import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Comment; @@ -30,8 +32,6 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; import org.eclipse.jgit.lib.PersonIdent; @@ -87,8 +87,7 @@ public class LegacyChangeNoteWrite { return; } - List psIds = new ArrayList<>(comments.keySet()); - Collections.sort(psIds); + ImmutableList psIds = comments.keySet().stream().sorted().collect(toImmutableList()); OutputStreamWriter streamWriter = new OutputStreamWriter(out, UTF_8); try (PrintWriter writer = new PrintWriter(streamWriter)) { diff --git a/java/com/google/gerrit/server/patch/DiffSummaryLoader.java b/java/com/google/gerrit/server/patch/DiffSummaryLoader.java index 8bca19f3c9..9153638199 100644 --- a/java/com/google/gerrit/server/patch/DiffSummaryLoader.java +++ b/java/com/google/gerrit/server/patch/DiffSummaryLoader.java @@ -19,7 +19,6 @@ import com.google.gerrit.reviewdb.client.Project; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; @@ -66,8 +65,9 @@ public class DiffSummaryLoader implements Callable { break; } } - Collections.sort(r); return new DiffSummary( - r.toArray(new String[r.size()]), patchList.getInsertions(), patchList.getDeletions()); + r.stream().sorted().toArray(String[]::new), + patchList.getInsertions(), + patchList.getDeletions()); } } diff --git a/java/com/google/gerrit/server/patch/PatchScriptBuilder.java b/java/com/google/gerrit/server/patch/PatchScriptBuilder.java index e94f7e83d4..61f0180879 100644 --- a/java/com/google/gerrit/server/patch/PatchScriptBuilder.java +++ b/java/com/google/gerrit/server/patch/PatchScriptBuilder.java @@ -35,7 +35,6 @@ import eu.medsea.mimeutil.MimeType; import eu.medsea.mimeutil.MimeUtil2; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Optional; @@ -364,7 +363,7 @@ class PatchScriptBuilder { // them correctly later. // edits.addAll(empty); - Collections.sort(edits, EDIT_SORT); + edits.sort(EDIT_SORT); } private void safeAdd(List empty, Edit toAdd) { diff --git a/java/com/google/gerrit/server/permissions/SectionSortCache.java b/java/com/google/gerrit/server/permissions/SectionSortCache.java index 48c8bff8e1..e5392b04e8 100644 --- a/java/com/google/gerrit/server/permissions/SectionSortCache.java +++ b/java/com/google/gerrit/server/permissions/SectionSortCache.java @@ -26,7 +26,6 @@ import com.google.inject.Module; import com.google.inject.Singleton; import com.google.inject.name.Named; import java.util.ArrayList; -import java.util.Collections; import java.util.IdentityHashMap; import java.util.List; @@ -88,7 +87,7 @@ public class SectionSortCache { poison |= srcMap.put(sections.get(i), i) != null; } - Collections.sort(sections, new MostSpecificComparator(ref)); + sections.sort(new MostSpecificComparator(ref)); int[] srcIdx; if (isIdentityTransform(sections, srcMap)) { diff --git a/java/com/google/gerrit/server/project/ProjectConfig.java b/java/com/google/gerrit/server/project/ProjectConfig.java index 60c9ffdb88..5b79ab9325 100644 --- a/java/com/google/gerrit/server/project/ProjectConfig.java +++ b/java/com/google/gerrit/server/project/ProjectConfig.java @@ -15,6 +15,7 @@ package com.google.gerrit.server.project; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.gerrit.common.data.Permission.isPermission; import static com.google.gerrit.reviewdb.client.Project.DEFAULT_SUBMIT_TYPE; import static java.util.stream.Collectors.toList; @@ -1451,10 +1452,8 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError. validationErrors.add(error); } - private static > List sort(Collection m) { - ArrayList r = new ArrayList<>(m); - Collections.sort(r); - return r; + private static > ImmutableList sort(Collection m) { + return m.stream().sorted().collect(toImmutableList()); } public boolean hasLegacyPermissions() { diff --git a/java/com/google/gerrit/server/restapi/change/CommentJson.java b/java/com/google/gerrit/server/restapi/change/CommentJson.java index 742fe160dc..a562592360 100644 --- a/java/com/google/gerrit/server/restapi/change/CommentJson.java +++ b/java/com/google/gerrit/server/restapi/change/CommentJson.java @@ -37,7 +37,6 @@ import com.google.gerrit.server.account.AccountLoader; import com.google.gerrit.server.permissions.PermissionBackendException; import com.google.inject.Inject; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -98,9 +97,7 @@ class CommentJson { list.add(o); } - for (List list : out.values()) { - Collections.sort(list, COMMENT_INFO_ORDER); - } + out.values().forEach(l -> l.sort(COMMENT_INFO_ORDER)); if (loader != null) { loader.fill(); diff --git a/java/com/google/gerrit/server/restapi/change/ListRevisionDrafts.java b/java/com/google/gerrit/server/restapi/change/ListRevisionDrafts.java index db8ef0c728..dbd0ccf233 100644 --- a/java/com/google/gerrit/server/restapi/change/ListRevisionDrafts.java +++ b/java/com/google/gerrit/server/restapi/change/ListRevisionDrafts.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.restapi.change; +import com.google.common.collect.ImmutableList; import com.google.gerrit.extensions.common.CommentInfo; import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.reviewdb.client.Comment; @@ -61,7 +62,7 @@ public class ListRevisionDrafts implements RestReadView { .format(listComments(rsrc)); } - public List getComments(RevisionResource rsrc) + public ImmutableList getComments(RevisionResource rsrc) throws OrmException, PermissionBackendException { return commentJson .get() diff --git a/java/com/google/gerrit/server/restapi/change/ListRobotComments.java b/java/com/google/gerrit/server/restapi/change/ListRobotComments.java index 66138ab35d..99366aa7d4 100644 --- a/java/com/google/gerrit/server/restapi/change/ListRobotComments.java +++ b/java/com/google/gerrit/server/restapi/change/ListRobotComments.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.restapi.change; +import com.google.common.collect.ImmutableList; import com.google.gerrit.extensions.common.RobotCommentInfo; import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.reviewdb.client.RobotComment; @@ -52,7 +53,7 @@ public class ListRobotComments implements RestReadView { .format(listComments(rsrc)); } - public List getComments(RevisionResource rsrc) + public ImmutableList getComments(RevisionResource rsrc) throws OrmException, PermissionBackendException { return commentJson .get() diff --git a/java/com/google/gerrit/server/restapi/group/GetAuditLog.java b/java/com/google/gerrit/server/restapi/group/GetAuditLog.java index 7af4284d10..dcdd8a8802 100644 --- a/java/com/google/gerrit/server/restapi/group/GetAuditLog.java +++ b/java/com/google/gerrit/server/restapi/group/GetAuditLog.java @@ -41,7 +41,6 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Optional; import org.eclipse.jgit.errors.ConfigInvalidException; @@ -138,7 +137,7 @@ public class GetAuditLog implements RestReadView { accountLoader.fill(); // sort by date and then reverse so that the newest audit event comes first - Collections.sort(auditEvents, comparing((GroupAuditEventInfo a) -> a.date).reversed()); + auditEvents.sort(comparing((GroupAuditEventInfo a) -> a.date).reversed()); return auditEvents; } } diff --git a/java/com/google/gerrit/server/restapi/group/ListSubgroups.java b/java/com/google/gerrit/server/restapi/group/ListSubgroups.java index 26b6db4567..864b01bd9b 100644 --- a/java/com/google/gerrit/server/restapi/group/ListSubgroups.java +++ b/java/com/google/gerrit/server/restapi/group/ListSubgroups.java @@ -30,7 +30,6 @@ import com.google.gwtorm.server.OrmException; import com.google.inject.Inject; import com.google.inject.Singleton; import java.util.ArrayList; -import java.util.Collections; import java.util.List; @Singleton @@ -72,8 +71,7 @@ public class ListSubgroups implements RestReadView { continue; } } - Collections.sort( - included, + included.sort( comparing((GroupInfo g) -> nullToEmpty(g.name)).thenComparing(g -> nullToEmpty(g.id))); return included; } diff --git a/java/com/google/gerrit/server/restapi/project/ListBranches.java b/java/com/google/gerrit/server/restapi/project/ListBranches.java index bf4a547d94..a0d2528ed0 100644 --- a/java/com/google/gerrit/server/restapi/project/ListBranches.java +++ b/java/com/google/gerrit/server/restapi/project/ListBranches.java @@ -46,7 +46,6 @@ import com.google.inject.Inject; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Set; @@ -226,7 +225,7 @@ public class ListBranches implements RestReadView { // Do nothing. } } - Collections.sort(branches, new BranchComparator()); + branches.sort(new BranchComparator()); return branches; } diff --git a/java/com/google/gerrit/server/restapi/project/ListTags.java b/java/com/google/gerrit/server/restapi/project/ListTags.java index 578fbf36c2..f59e98421c 100644 --- a/java/com/google/gerrit/server/restapi/project/ListTags.java +++ b/java/com/google/gerrit/server/restapi/project/ListTags.java @@ -40,7 +40,6 @@ import com.google.inject.Inject; import java.io.IOException; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import org.eclipse.jgit.errors.RepositoryNotFoundException; @@ -135,7 +134,7 @@ public class ListTags implements RestReadView { } } - Collections.sort(tags, comparing(t -> t.ref)); + tags.sort(comparing(t -> t.ref)); return new RefFilter(Constants.R_TAGS) .start(start) diff --git a/java/com/google/gerrit/server/submit/SubmitStrategyOp.java b/java/com/google/gerrit/server/submit/SubmitStrategyOp.java index 290e9175c2..51dad5b490 100644 --- a/java/com/google/gerrit/server/submit/SubmitStrategyOp.java +++ b/java/com/google/gerrit/server/submit/SubmitStrategyOp.java @@ -53,7 +53,6 @@ import com.google.gerrit.server.update.RepoContext; import com.google.gwtorm.server.OrmException; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -184,8 +183,7 @@ abstract class SubmitStrategyOp implements BatchUpdateOp { continue; // Bogus ref, can't be merged into tip so we don't care. } } - Collections.sort( - commits, + commits.sort( ReviewDbUtil.intKeyOrdering().reverse().onResultOf(CodeReviewCommit::getPatchsetId)); CodeReviewCommit result = MergeUtil.findAnyMergedInto(rw, commits, tip); if (result == null) { diff --git a/java/com/google/gerrit/sshd/commands/ShowConnections.java b/java/com/google/gerrit/sshd/commands/ShowConnections.java index 9b517c6a24..baadf028a2 100644 --- a/java/com/google/gerrit/sshd/commands/ShowConnections.java +++ b/java/com/google/gerrit/sshd/commands/ShowConnections.java @@ -15,8 +15,10 @@ package com.google.gerrit.sshd.commands; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; +import com.google.common.collect.ImmutableList; import com.google.gerrit.common.TimeUtil; import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.extensions.annotations.RequiresCapability; @@ -33,11 +35,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.Date; -import java.util.List; import java.util.Optional; import org.apache.sshd.common.io.IoAcceptor; import org.apache.sshd.common.io.IoSession; @@ -92,25 +90,27 @@ final class ShowConnections extends SshCommand { throw new Failure(1, "fatal: sshd no longer running"); } - final List list = new ArrayList<>(acceptor.getManagedSessions().values()); - Collections.sort( - list, - new Comparator() { - @Override - public int compare(IoSession arg0, IoSession arg1) { - if (arg0 instanceof MinaSession) { - MinaSession mArg0 = (MinaSession) arg0; - MinaSession mArg1 = (MinaSession) arg1; - if (mArg0.getSession().getCreationTime() < mArg1.getSession().getCreationTime()) { - return -1; - } else if (mArg0.getSession().getCreationTime() - > mArg1.getSession().getCreationTime()) { - return 1; - } - } - return (int) (arg0.getId() - arg1.getId()); - } - }); + final ImmutableList list = + acceptor + .getManagedSessions() + .values() + .stream() + .sorted( + (arg0, arg1) -> { + if (arg0 instanceof MinaSession) { + MinaSession mArg0 = (MinaSession) arg0; + MinaSession mArg1 = (MinaSession) arg1; + if (mArg0.getSession().getCreationTime() + < mArg1.getSession().getCreationTime()) { + return -1; + } else if (mArg0.getSession().getCreationTime() + > mArg1.getSession().getCreationTime()) { + return 1; + } + } + return (int) (arg0.getId() - arg1.getId()); + }) + .collect(toImmutableList()); hostNameWidth = wide ? Integer.MAX_VALUE : columns - 9 - 9 - 10 - 32;