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:
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.info;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.rpc.NativeMap;
|
||||
import com.google.gerrit.client.rpc.NativeString;
|
||||
import com.google.gerrit.client.rpc.Natives;
|
||||
@@ -31,7 +33,6 @@ import com.google.gwtjsonrpc.client.impl.ser.JavaSqlTimestamp_JsonSerializer;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -449,16 +450,7 @@ public class ChangeInfo extends JavaScriptObject {
|
||||
final int editParent = findEditParent(list);
|
||||
Collections.sort(
|
||||
Natives.asList(list),
|
||||
new Comparator<RevisionInfo>() {
|
||||
@Override
|
||||
public int compare(RevisionInfo a, RevisionInfo b) {
|
||||
return num(a) - num(b);
|
||||
}
|
||||
|
||||
private int num(RevisionInfo r) {
|
||||
return !r.isEdit() ? 2 * (r._number() - 1) + 1 : 2 * editParent;
|
||||
}
|
||||
});
|
||||
comparing(r -> !r.isEdit() ? 2 * (r._number() - 1) + 1 : 2 * editParent));
|
||||
}
|
||||
|
||||
public static int findEditParent(JsArray<RevisionInfo> list) {
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.account;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.info.GpgKeyInfo;
|
||||
import com.google.gerrit.client.rpc.GerritCallback;
|
||||
@@ -41,7 +43,6 @@ 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.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class MyGpgKeysScreen extends SettingsScreen {
|
||||
@@ -118,14 +119,7 @@ public class MyGpgKeysScreen extends SettingsScreen {
|
||||
List<GpgKeyInfo> list = Natives.asList(result.values());
|
||||
// TODO(dborowitz): Sort on something more meaningful, like
|
||||
// created date?
|
||||
Collections.sort(
|
||||
list,
|
||||
new Comparator<GpgKeyInfo>() {
|
||||
@Override
|
||||
public int compare(GpgKeyInfo a, GpgKeyInfo b) {
|
||||
return a.id().compareTo(b.id());
|
||||
}
|
||||
});
|
||||
Collections.sort(list, comparing(GpgKeyInfo::id));
|
||||
keys.clear();
|
||||
keyText.setText("");
|
||||
errorPanel.setVisible(false);
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.admin;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Dispatcher;
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.VoidResult;
|
||||
@@ -29,7 +31,6 @@ import com.google.gerrit.client.ui.AddMemberBox;
|
||||
import com.google.gerrit.client.ui.FancyFlexTable;
|
||||
import com.google.gerrit.client.ui.Hyperlink;
|
||||
import com.google.gerrit.client.ui.SmallHeading;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
import com.google.gwt.event.dom.client.ClickEvent;
|
||||
import com.google.gwt.event.dom.client.ClickHandler;
|
||||
@@ -295,26 +296,9 @@ public class AccountGroupMembersScreen extends AccountGroupScreen {
|
||||
|
||||
void insert(AccountInfo info) {
|
||||
Comparator<AccountInfo> c =
|
||||
new Comparator<AccountInfo>() {
|
||||
@Override
|
||||
public int compare(AccountInfo a, AccountInfo b) {
|
||||
int cmp = nullToEmpty(a.name()).compareTo(nullToEmpty(b.name()));
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
|
||||
cmp = nullToEmpty(a.email()).compareTo(nullToEmpty(b.email()));
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
|
||||
return a._accountId() - b._accountId();
|
||||
}
|
||||
|
||||
public String nullToEmpty(String str) {
|
||||
return str == null ? "" : str;
|
||||
}
|
||||
};
|
||||
comparing((AccountInfo a) -> nullToEmpty(a.name()))
|
||||
.thenComparing(a -> nullToEmpty(a.email()))
|
||||
.thenComparing(AccountInfo::_accountId);
|
||||
int insertPos = getInsertRow(c, info);
|
||||
if (insertPos >= 0) {
|
||||
table.insertRow(insertPos);
|
||||
@@ -405,20 +389,7 @@ public class AccountGroupMembersScreen extends AccountGroupScreen {
|
||||
|
||||
void insert(GroupInfo info) {
|
||||
Comparator<GroupInfo> c =
|
||||
new Comparator<GroupInfo>() {
|
||||
@Override
|
||||
public int compare(GroupInfo a, GroupInfo b) {
|
||||
int cmp = nullToEmpty(a.name()).compareTo(nullToEmpty(b.name()));
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
return a.getGroupUUID().compareTo(b.getGroupUUID());
|
||||
}
|
||||
|
||||
private String nullToEmpty(@Nullable String str) {
|
||||
return (str == null) ? "" : str;
|
||||
}
|
||||
};
|
||||
comparing((GroupInfo g) -> nullToEmpty(g.name())).thenComparing(GroupInfo::getGroupUUID);
|
||||
int insertPos = getInsertRow(c, info);
|
||||
if (insertPos >= 0) {
|
||||
table.insertRow(insertPos);
|
||||
@@ -457,4 +428,9 @@ public class AccountGroupMembersScreen extends AccountGroupScreen {
|
||||
setRowItem(row, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Like Guava's Strings#nullToEmpty, which can't be used in GWT UI code.
|
||||
private static String nullToEmpty(String str) {
|
||||
return str == null ? "" : str;
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.admin;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Dispatcher;
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.groups.GroupList;
|
||||
@@ -33,7 +35,6 @@ 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.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class GroupTable extends NavigationTable<GroupInfo> {
|
||||
@@ -105,14 +106,7 @@ public class GroupTable extends NavigationTable<GroupInfo> {
|
||||
table.removeRow(table.getRowCount() - 1);
|
||||
}
|
||||
|
||||
Collections.sort(
|
||||
list,
|
||||
new Comparator<GroupInfo>() {
|
||||
@Override
|
||||
public int compare(GroupInfo a, GroupInfo b) {
|
||||
return a.name().compareTo(b.name());
|
||||
}
|
||||
});
|
||||
Collections.sort(list, comparing(GroupInfo::name));
|
||||
for (GroupInfo group : list.subList(fromIndex, toIndex)) {
|
||||
final int row = table.getRowCount();
|
||||
table.insertRow(row);
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.changes;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.NotFoundScreen;
|
||||
import com.google.gerrit.client.info.ChangeInfo;
|
||||
@@ -187,16 +189,7 @@ public class AccountDashboardScreen extends Screen implements ChangeListScreen {
|
||||
}
|
||||
|
||||
private Comparator<ChangeInfo> outComparator() {
|
||||
return new Comparator<ChangeInfo>() {
|
||||
@Override
|
||||
public int compare(ChangeInfo a, ChangeInfo b) {
|
||||
int cmp = a.created().compareTo(b.created());
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
return a._number() - b._number();
|
||||
}
|
||||
};
|
||||
return comparing(ChangeInfo::created).thenComparing(ChangeInfo::_number);
|
||||
}
|
||||
|
||||
private boolean hasChanges(JsArray<ChangeList> result) {
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.dashboards;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.rpc.Natives;
|
||||
import com.google.gerrit.client.ui.NavigationTable;
|
||||
@@ -26,7 +28,6 @@ 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.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -75,14 +76,7 @@ public class DashboardsTable extends NavigationTable<DashboardInfo> {
|
||||
table.removeRow(table.getRowCount() - 1);
|
||||
}
|
||||
|
||||
Collections.sort(
|
||||
list,
|
||||
new Comparator<DashboardInfo>() {
|
||||
@Override
|
||||
public int compare(DashboardInfo a, DashboardInfo b) {
|
||||
return a.id().compareTo(b.id());
|
||||
}
|
||||
});
|
||||
Collections.sort(list, comparing(DashboardInfo::id));
|
||||
|
||||
String ref = null;
|
||||
for (DashboardInfo d : list) {
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.diff;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.DiffObject;
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.changes.CommentApi;
|
||||
@@ -28,7 +30,6 @@ 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;
|
||||
import java.util.Comparator;
|
||||
|
||||
/** Collection of published and draft comments loaded from the server. */
|
||||
class CommentsCollections {
|
||||
@@ -158,14 +159,7 @@ class CommentsCollections {
|
||||
for (CommentInfo c : Natives.asList(in)) {
|
||||
c.path(path);
|
||||
}
|
||||
Collections.sort(
|
||||
Natives.asList(in),
|
||||
new Comparator<CommentInfo>() {
|
||||
@Override
|
||||
public int compare(CommentInfo a, CommentInfo b) {
|
||||
return a.updated().compareTo(b.updated());
|
||||
}
|
||||
});
|
||||
Collections.sort(Natives.asList(in), comparing(CommentInfo::updated));
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.diff;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.diff.DiffInfo.Region;
|
||||
import com.google.gerrit.client.diff.DiffInfo.Span;
|
||||
import com.google.gerrit.client.rpc.Natives;
|
||||
@@ -227,12 +229,7 @@ class UnifiedChunkManager extends ChunkManager {
|
||||
|
||||
/** Diff chunks are ordered by their starting lines in CodeMirror */
|
||||
private Comparator<UnifiedDiffChunkInfo> getDiffChunkComparatorCmLine() {
|
||||
return new Comparator<UnifiedDiffChunkInfo>() {
|
||||
@Override
|
||||
public int compare(UnifiedDiffChunkInfo o1, UnifiedDiffChunkInfo o2) {
|
||||
return o1.getCmLine() - o2.getCmLine();
|
||||
}
|
||||
};
|
||||
return comparing(UnifiedDiffChunkInfo::getCmLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.client.ui;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.Gerrit;
|
||||
import com.google.gerrit.client.projects.ProjectInfo;
|
||||
import com.google.gerrit.client.projects.ProjectMap;
|
||||
@@ -21,7 +23,6 @@ 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.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class ProjectsTable extends NavigationTable<ProjectInfo> {
|
||||
@@ -69,14 +70,7 @@ public class ProjectsTable extends NavigationTable<ProjectInfo> {
|
||||
}
|
||||
|
||||
List<ProjectInfo> list = Natives.asList(projects.values());
|
||||
Collections.sort(
|
||||
list,
|
||||
new Comparator<ProjectInfo>() {
|
||||
@Override
|
||||
public int compare(ProjectInfo a, ProjectInfo b) {
|
||||
return a.name().compareTo(b.name());
|
||||
}
|
||||
});
|
||||
Collections.sort(list, comparing(ProjectInfo::name));
|
||||
for (ProjectInfo p : list.subList(fromIndex, toIndex)) {
|
||||
insert(table.getRowCount(), p);
|
||||
}
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package net.codemirror.mode;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.client.rpc.NativeMap;
|
||||
import com.google.gerrit.client.rpc.Natives;
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
@@ -22,7 +24,6 @@ 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.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -242,14 +243,7 @@ public class ModeInfo extends JavaScriptObject {
|
||||
byMime.put(m.mode(), m);
|
||||
}
|
||||
}
|
||||
Collections.sort(
|
||||
Natives.asList(filtered),
|
||||
new Comparator<ModeInfo>() {
|
||||
@Override
|
||||
public int compare(ModeInfo a, ModeInfo b) {
|
||||
return a.name().toLowerCase().compareTo(b.name().toLowerCase());
|
||||
}
|
||||
});
|
||||
Collections.sort(Natives.asList(filtered), comparing((ModeInfo m) -> m.name().toLowerCase()));
|
||||
setAll(filtered);
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,8 @@
|
||||
|
||||
package com.google.gerrit.pgm.init;
|
||||
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.gerrit.common.PluginData;
|
||||
import com.google.gerrit.pgm.init.api.ConsoleUI;
|
||||
import com.google.gerrit.pgm.init.api.InitFlags;
|
||||
@@ -25,12 +26,11 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.JarFile;
|
||||
@@ -58,25 +58,16 @@ public class InitPlugins implements InitStep {
|
||||
throws IOException {
|
||||
final List<PluginData> result = new ArrayList<>();
|
||||
pluginsDistribution.foreach(
|
||||
new PluginsDistribution.Processor() {
|
||||
@Override
|
||||
public void process(String pluginName, InputStream in) throws IOException {
|
||||
(pluginName, in) -> {
|
||||
Path tmpPlugin = JarPluginProvider.storeInTemp(pluginName, in, site);
|
||||
String pluginVersion = getVersion(tmpPlugin);
|
||||
if (deleteTempPluginFile) {
|
||||
Files.delete(tmpPlugin);
|
||||
}
|
||||
result.add(new PluginData(pluginName, pluginVersion, tmpPlugin));
|
||||
}
|
||||
});
|
||||
return FluentIterable.from(result)
|
||||
.toSortedList(
|
||||
new Comparator<PluginData>() {
|
||||
@Override
|
||||
public int compare(PluginData a, PluginData b) {
|
||||
return a.name.compareTo(b.name);
|
||||
}
|
||||
});
|
||||
Collections.sort(result, comparing(p -> p.name));
|
||||
return result;
|
||||
}
|
||||
|
||||
private final ConsoleUI ui;
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.common.data.GroupReference;
|
||||
@@ -25,12 +27,7 @@ import java.util.Comparator;
|
||||
public class GroupBackends {
|
||||
|
||||
public static final Comparator<GroupReference> GROUP_REF_NAME_COMPARATOR =
|
||||
new Comparator<GroupReference>() {
|
||||
@Override
|
||||
public int compare(GroupReference a, GroupReference b) {
|
||||
return a.getName().compareTo(b.getName());
|
||||
}
|
||||
};
|
||||
comparing(GroupReference::getName);
|
||||
|
||||
/**
|
||||
* Runs {@link GroupBackend#suggest(String, ProjectState)} and filters the result to return the
|
||||
|
@@ -14,6 +14,9 @@
|
||||
|
||||
package com.google.gerrit.server.change;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -22,7 +25,6 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -151,15 +153,7 @@ public class IncludedInResolver {
|
||||
*/
|
||||
private void partition(List<RevCommit> before, List<RevCommit> after) {
|
||||
int insertionPoint =
|
||||
Collections.binarySearch(
|
||||
tipsByCommitTime,
|
||||
target,
|
||||
new Comparator<RevCommit>() {
|
||||
@Override
|
||||
public int compare(RevCommit c1, RevCommit c2) {
|
||||
return c1.getCommitTime() - c2.getCommitTime();
|
||||
}
|
||||
});
|
||||
Collections.binarySearch(tipsByCommitTime, target, comparing(RevCommit::getCommitTime));
|
||||
if (insertionPoint < 0) {
|
||||
insertionPoint = -(insertionPoint + 1);
|
||||
}
|
||||
@@ -211,19 +205,8 @@ public class IncludedInResolver {
|
||||
}
|
||||
commitToRef.put(commit, ref.getName());
|
||||
}
|
||||
tipsByCommitTime = Lists.newArrayList(commitToRef.keySet());
|
||||
sortOlderFirst(tipsByCommitTime);
|
||||
}
|
||||
|
||||
private void sortOlderFirst(List<RevCommit> tips) {
|
||||
Collections.sort(
|
||||
tips,
|
||||
new Comparator<RevCommit>() {
|
||||
@Override
|
||||
public int compare(RevCommit c1, RevCommit c2) {
|
||||
return c1.getCommitTime() - c2.getCommitTime();
|
||||
}
|
||||
});
|
||||
tipsByCommitTime =
|
||||
commitToRef.keySet().stream().sorted(comparing(RevCommit::getCommitTime)).collect(toList());
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.server.mime;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.inject.Inject;
|
||||
@@ -22,12 +24,9 @@ import eu.medsea.mimeutil.MimeException;
|
||||
import eu.medsea.mimeutil.MimeType;
|
||||
import eu.medsea.mimeutil.MimeUtil2;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
|
||||
@@ -115,16 +114,7 @@ public class MimeUtilFileTypeRegistry implements FileTypeRegistry {
|
||||
return MimeUtil2.UNKNOWN_MIME_TYPE;
|
||||
}
|
||||
|
||||
final List<MimeType> types = new ArrayList<>(mimeTypes);
|
||||
Collections.sort(
|
||||
types,
|
||||
new Comparator<MimeType>() {
|
||||
@Override
|
||||
public int compare(MimeType a, MimeType b) {
|
||||
return getCorrectedMimeSpecificity(b) - getCorrectedMimeSpecificity(a);
|
||||
}
|
||||
});
|
||||
return types.get(0);
|
||||
return Collections.max(mimeTypes, comparing(this::getCorrectedMimeSpecificity));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -47,12 +47,7 @@ public class PatchList implements Serializable {
|
||||
private static final long serialVersionUID = PatchListKey.serialVersionUID;
|
||||
|
||||
private static final Comparator<PatchListEntry> PATCH_CMP =
|
||||
new Comparator<PatchListEntry>() {
|
||||
@Override
|
||||
public int compare(PatchListEntry a, PatchListEntry b) {
|
||||
return comparePaths(a.getNewName(), b.getNewName());
|
||||
}
|
||||
};
|
||||
Comparator.comparing(PatchListEntry::getNewName, PatchList::comparePaths);
|
||||
|
||||
@VisibleForTesting
|
||||
static int comparePaths(String a, String b) {
|
||||
|
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.server.patch;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gerrit.common.data.CommentDetail;
|
||||
@@ -55,13 +56,7 @@ class PatchScriptBuilder {
|
||||
static final int MAX_CONTEXT = 5000000;
|
||||
static final int BIG_FILE = 9000;
|
||||
|
||||
private static final Comparator<Edit> EDIT_SORT =
|
||||
new Comparator<Edit>() {
|
||||
@Override
|
||||
public int compare(Edit o1, Edit o2) {
|
||||
return o1.getBeginA() - o2.getBeginA();
|
||||
}
|
||||
};
|
||||
private static final Comparator<Edit> EDIT_SORT = comparing(Edit::getBeginA);
|
||||
|
||||
private Repository db;
|
||||
private Project.NameKey projectKey;
|
||||
|
@@ -14,6 +14,8 @@
|
||||
|
||||
package com.google.gerrit.server.plugins;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
@@ -28,13 +30,7 @@ import java.util.Optional;
|
||||
public class PluginEntry {
|
||||
public static final String ATTR_CHARACTER_ENCODING = "Character-Encoding";
|
||||
public static final String ATTR_CONTENT_TYPE = "Content-Type";
|
||||
public static final Comparator<PluginEntry> COMPARATOR_BY_NAME =
|
||||
new Comparator<PluginEntry>() {
|
||||
@Override
|
||||
public int compare(PluginEntry a, PluginEntry b) {
|
||||
return a.getName().compareTo(b.getName());
|
||||
}
|
||||
};
|
||||
public static final Comparator<PluginEntry> COMPARATOR_BY_NAME = comparing(PluginEntry::getName);
|
||||
|
||||
private static final Map<Object, String> EMPTY_ATTRS = Collections.emptyMap();
|
||||
private static final Optional<Long> NO_SIZE = Optional.empty();
|
||||
|
@@ -14,6 +14,9 @@
|
||||
|
||||
package com.google.gerrit.server.restapi.account;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.gerrit.extensions.common.EmailInfo;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
@@ -25,10 +28,8 @@ import com.google.gerrit.server.permissions.PermissionBackendException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Singleton
|
||||
public class GetEmails implements RestReadView<AccountResource> {
|
||||
@@ -47,24 +48,19 @@ public class GetEmails implements RestReadView<AccountResource> {
|
||||
if (!self.get().hasSameAccountId(rsrc.getUser())) {
|
||||
permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
|
||||
}
|
||||
return rsrc.getUser()
|
||||
.getEmailAddresses()
|
||||
.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(e -> toEmailInfo(rsrc, e))
|
||||
.sorted(comparing((EmailInfo e) -> e.email))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
List<EmailInfo> emails = new ArrayList<>();
|
||||
for (String email : rsrc.getUser().getEmailAddresses()) {
|
||||
if (email != null) {
|
||||
private static EmailInfo toEmailInfo(AccountResource rsrc, String email) {
|
||||
EmailInfo e = new EmailInfo();
|
||||
e.email = email;
|
||||
e.preferred(rsrc.getUser().getAccount().getPreferredEmail());
|
||||
emails.add(e);
|
||||
}
|
||||
}
|
||||
Collections.sort(
|
||||
emails,
|
||||
new Comparator<EmailInfo>() {
|
||||
@Override
|
||||
public int compare(EmailInfo a, EmailInfo b) {
|
||||
return a.email.compareTo(b.email);
|
||||
}
|
||||
});
|
||||
return emails;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
@@ -14,8 +14,10 @@
|
||||
|
||||
package com.google.gerrit.server.restapi.account;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gerrit.extensions.client.ProjectWatchInfo;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
@@ -36,11 +38,7 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
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.ConfigInvalidException;
|
||||
|
||||
@Singleton
|
||||
@@ -67,31 +65,28 @@ public class GetWatchedProjects implements RestReadView<AccountResource> {
|
||||
|
||||
Account.Id accountId = rsrc.getUser().getAccountId();
|
||||
AccountState account = accounts.get(accountId).orElseThrow(ResourceNotFoundException::new);
|
||||
List<ProjectWatchInfo> projectWatchInfos = new ArrayList<>();
|
||||
for (Map.Entry<ProjectWatchKey, ImmutableSet<NotifyType>> e :
|
||||
account.getProjectWatches().entrySet()) {
|
||||
return account
|
||||
.getProjectWatches()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.map(e -> toProjectWatchInfo(e.getKey(), e.getValue()))
|
||||
.sorted(
|
||||
comparing((ProjectWatchInfo pwi) -> pwi.project)
|
||||
.thenComparing(pwi -> Strings.nullToEmpty(pwi.filter)))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private static ProjectWatchInfo toProjectWatchInfo(
|
||||
ProjectWatchKey key, ImmutableSet<NotifyType> watchTypes) {
|
||||
ProjectWatchInfo pwi = new ProjectWatchInfo();
|
||||
pwi.filter = e.getKey().filter();
|
||||
pwi.project = e.getKey().project().get();
|
||||
pwi.notifyAbandonedChanges = toBoolean(e.getValue().contains(NotifyType.ABANDONED_CHANGES));
|
||||
pwi.notifyNewChanges = toBoolean(e.getValue().contains(NotifyType.NEW_CHANGES));
|
||||
pwi.notifyNewPatchSets = toBoolean(e.getValue().contains(NotifyType.NEW_PATCHSETS));
|
||||
pwi.notifySubmittedChanges = toBoolean(e.getValue().contains(NotifyType.SUBMITTED_CHANGES));
|
||||
pwi.notifyAllComments = toBoolean(e.getValue().contains(NotifyType.ALL_COMMENTS));
|
||||
projectWatchInfos.add(pwi);
|
||||
}
|
||||
Collections.sort(
|
||||
projectWatchInfos,
|
||||
new Comparator<ProjectWatchInfo>() {
|
||||
@Override
|
||||
public int compare(ProjectWatchInfo pwi1, ProjectWatchInfo pwi2) {
|
||||
return ComparisonChain.start()
|
||||
.compare(pwi1.project, pwi2.project)
|
||||
.compare(Strings.nullToEmpty(pwi1.filter), Strings.nullToEmpty(pwi2.filter))
|
||||
.result();
|
||||
}
|
||||
});
|
||||
return projectWatchInfos;
|
||||
pwi.filter = key.filter();
|
||||
pwi.project = key.project().get();
|
||||
pwi.notifyAbandonedChanges = toBoolean(watchTypes.contains(NotifyType.ABANDONED_CHANGES));
|
||||
pwi.notifyNewChanges = toBoolean(watchTypes.contains(NotifyType.NEW_CHANGES));
|
||||
pwi.notifyNewPatchSets = toBoolean(watchTypes.contains(NotifyType.NEW_PATCHSETS));
|
||||
pwi.notifySubmittedChanges = toBoolean(watchTypes.contains(NotifyType.SUBMITTED_CHANGES));
|
||||
pwi.notifyAllComments = toBoolean(watchTypes.contains(NotifyType.ALL_COMMENTS));
|
||||
return pwi;
|
||||
}
|
||||
|
||||
private static Boolean toBoolean(boolean value) {
|
||||
|
@@ -14,7 +14,9 @@
|
||||
|
||||
package com.google.gerrit.server.restapi.config;
|
||||
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
@@ -35,8 +37,6 @@ import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -106,20 +106,14 @@ public class ListTasks implements RestReadView<ConfigResource> {
|
||||
}
|
||||
|
||||
private List<TaskInfo> getTasks() {
|
||||
List<TaskInfo> taskInfos = workQueue.getTaskInfos(TaskInfo::new);
|
||||
Collections.sort(
|
||||
taskInfos,
|
||||
new Comparator<TaskInfo>() {
|
||||
@Override
|
||||
public int compare(TaskInfo a, TaskInfo b) {
|
||||
return ComparisonChain.start()
|
||||
.compare(a.state.ordinal(), b.state.ordinal())
|
||||
.compare(a.delay, b.delay)
|
||||
.compare(a.command, b.command)
|
||||
.result();
|
||||
}
|
||||
});
|
||||
return taskInfos;
|
||||
return workQueue
|
||||
.getTaskInfos(TaskInfo::new)
|
||||
.stream()
|
||||
.sorted(
|
||||
comparing((TaskInfo t) -> t.state.ordinal())
|
||||
.thenComparing(t -> t.delay)
|
||||
.thenComparing(t -> t.command))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public static class TaskInfo {
|
||||
|
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.server.restapi.group;
|
||||
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.gerrit.common.data.GroupDescription;
|
||||
@@ -30,7 +31,6 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Singleton
|
||||
@@ -74,16 +74,7 @@ public class ListSubgroups implements RestReadView<GroupResource> {
|
||||
}
|
||||
Collections.sort(
|
||||
included,
|
||||
new Comparator<GroupInfo>() {
|
||||
@Override
|
||||
public int compare(GroupInfo a, GroupInfo b) {
|
||||
int cmp = nullToEmpty(a.name).compareTo(nullToEmpty(b.name));
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
return nullToEmpty(a.id).compareTo(nullToEmpty(b.id));
|
||||
}
|
||||
});
|
||||
comparing((GroupInfo g) -> nullToEmpty(g.name)).thenComparing(g -> nullToEmpty(g.id)));
|
||||
return included;
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
||||
|
@@ -14,6 +14,9 @@
|
||||
|
||||
package com.google.gwtexpui.globalkey.client;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.gwt.event.dom.client.ClickEvent;
|
||||
import com.google.gwt.event.dom.client.ClickHandler;
|
||||
import com.google.gwt.event.dom.client.KeyCodes;
|
||||
@@ -31,10 +34,7 @@ import com.google.gwt.user.client.ui.HasHorizontalAlignment;
|
||||
import com.google.gwt.user.client.ui.PopupPanel;
|
||||
import com.google.gwtexpui.safehtml.client.SafeHtml;
|
||||
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -228,15 +228,6 @@ public class KeyHelpPopup extends PopupPanel implements KeyPressHandler, KeyDown
|
||||
}
|
||||
|
||||
private List<KeyCommand> sort(KeyCommandSet set) {
|
||||
final List<KeyCommand> keys = new ArrayList<>(set.getKeys());
|
||||
Collections.sort(
|
||||
keys,
|
||||
new Comparator<KeyCommand>() {
|
||||
@Override
|
||||
public int compare(KeyCommand arg0, KeyCommand arg1) {
|
||||
return arg0.getHelpText().compareTo(arg1.getHelpText());
|
||||
}
|
||||
});
|
||||
return keys;
|
||||
return set.getKeys().stream().sorted(comparing(KeyCommand::getHelpText)).collect(toList());
|
||||
}
|
||||
}
|
||||
|
@@ -14,11 +14,12 @@
|
||||
|
||||
package com.google.gwtexpui.safehtml.client;
|
||||
|
||||
import static java.util.Comparator.comparing;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.gwt.user.client.ui.SuggestOracle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -115,15 +116,8 @@ public abstract class HighlightSuggestOracle extends SuggestOracle {
|
||||
* terms.
|
||||
*/
|
||||
private static List<String> splitQuery(String query) {
|
||||
List<String> queryTerms = Arrays.asList(query.split("\\s+"));
|
||||
Collections.sort(
|
||||
queryTerms,
|
||||
new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
return Integer.compare(s2.length(), s1.length());
|
||||
}
|
||||
});
|
||||
List<String> queryTerms =
|
||||
Arrays.stream(query.split("\\s+")).sorted(comparing(String::length)).collect(toList());
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
for (String s : queryTerms) {
|
||||
|
Reference in New Issue
Block a user