Skip to content

Commit

Permalink
Prefer list.sort(comp) over Collections.sort(list, comp)
Browse files Browse the repository at this point in the history
 - more fluent/direct
 - one less import in many cases
  • Loading branch information
mbien committed Feb 21, 2024
1 parent 1f4e851 commit a8efe63
Show file tree
Hide file tree
Showing 282 changed files with 398 additions and 597 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public boolean accept(File dir, String name) {
}
}));
System.out.println("Loading external cache from " + cacheDir + ", " + files.size() + " files");
Collections.sort(files, new Comparator<File>() {
files.sort(new Comparator<File>() {
public int compare(File f1, File f2) {
return - f1.getName().compareTo(f2.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -608,7 +607,7 @@ private static List<Datasource> getDatasources(final J2eeModuleProvider provider
datasources.addAll(serverDatasources);

ArrayList<Datasource> sortedDatasources = new ArrayList<Datasource>(datasources);
Collections.sort(sortedDatasources, new DatasourceComparator());
sortedDatasources.sort(new DatasourceComparator());
return sortedDatasources;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void populateDestinations(final Set<MessageDestination> destinatio
comboBox.setRenderer(new MessageDestinationListCellRenderer());

List<MessageDestination> sortedDestinations = new ArrayList<MessageDestination>(destinations);
Collections.sort(sortedDestinations, new MessageDestinationComparator());
sortedDestinations.sort(new MessageDestinationComparator());

comboBox.removeAllItems();
for (MessageDestination d : sortedDestinations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.1" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.modules.j2ee.common.DatasourceUIHelper;
import org.netbeans.modules.j2ee.common.ProjectUtil;
import org.netbeans.modules.j2ee.common.ServerUtil;
import org.netbeans.modules.j2ee.core.api.support.progress.ProgressSupport;
import org.netbeans.modules.j2ee.core.api.support.progress.ProgressSupport.Action;
Expand Down Expand Up @@ -192,7 +191,7 @@ private static void populateDataSources(final Set<Datasource> datasources, final
assert datasources != null && comboBox != null;

List<Datasource> sortedDatasources = new ArrayList<Datasource>(datasources);
Collections.sort(sortedDatasources, DatasourceUIHelper.createDatasourceComparator());
sortedDatasources.sort(DatasourceUIHelper.createDatasourceComparator());

comboBox.removeAllItems();
for (Datasource ds : sortedDatasources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void populateMessageDrivenBeans(final List<MdbHolder> mdbs, final
comboBox.setRenderer(new MdbHolderListCellRenderer());

List<MdbHolder> sortedMdbs = new ArrayList<>(mdbs);
Collections.sort(sortedMdbs, new MdbHolderComparator());
sortedMdbs.sort(new MdbHolderComparator());

comboBox.removeAllItems();
textField.setText("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public HintOutput run(Class<?> hint, String hintCode) throws Exception {
result.addAll(e.getValue());
}

Collections.sort(result, ERRORS_COMPARATOR);
result.sort(ERRORS_COMPARATOR);

Reference<CompilationInfo> infoRef = new WeakReference<CompilationInfo>(info);
Reference<CompilationUnitTree> cut = new WeakReference<CompilationUnitTree>(info.getCompilationUnit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Handler;
Expand Down Expand Up @@ -278,7 +277,7 @@ private void write(String str) {

private List<ErrorDescription> getProblems(FileObject fileToTest) {
problems = AnnotationHolder.getInstance(fileToTest).getErrors();
Collections.sort(problems, new Comparator<ErrorDescription>() {
problems.sort(new Comparator<ErrorDescription>() {

@Override
public int compare(ErrorDescription o1, ErrorDescription o2) {
Expand Down Expand Up @@ -360,7 +359,7 @@ private List<Fix> getFixes(FileObject fileToTest) {
for (ErrorDescription errorDescription : getProblems(fileToTest)) {
fixes.addAll(errorDescription.getFixes().getFixes());
}
Collections.sort(fixes, new Comparator<Fix>() {
fixes.sort(new Comparator<Fix>() {

@Override
public int compare(Fix o1, Fix o2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void setSortColumn(int col) {
}

private void sort() {
Collections.sort(modelIndex,comp);
modelIndex.sort(comp);
fireTableDataChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private String describeCompletion(String caretLine, String text, int caretOffset
sb.append("\n");

// Sort to make test more stable
Collections.sort(proposals, new Comparator<CompletionProposal>() {
proposals.sort(new Comparator<CompletionProposal>() {

public int compare(CompletionProposal p1, CompletionProposal p2) {
// Smart items first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.netbeans.modules.web.jsf.editor.facelets.JsfNamespaceComparator;
import org.netbeans.modules.web.jsf.editor.hints.LibraryDeclarationChecker;
import org.netbeans.modules.web.jsfapi.api.JsfSupport;
import org.netbeans.modules.web.jsfapi.api.JsfVersion;
import org.netbeans.modules.web.jsfapi.api.Library;
import org.netbeans.modules.web.jsfapi.api.NamespaceUtils;

Expand Down Expand Up @@ -126,7 +125,7 @@ private List<VariantItem> getSortedVariants(String prefix) {

// Add all other variants
List<String> sortedList = new ArrayList<>(supportedLibraries.keySet());
Collections.sort(sortedList, JsfNamespaceComparator.getInstance());
sortedList.sort(JsfNamespaceComparator.getInstance());

sortedList.stream()
.filter(not(namespacesForPrefix::contains))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import static java.util.function.Predicate.not;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -491,7 +490,7 @@ public static void completeXMLNSAttribute(CompletionContext context, List<Comple

//xml namespace completion for facelets namespaces
List<String> otherNamespaces = new ArrayList<>(jsfs.getLibraries().keySet());
Collections.sort(otherNamespaces, JsfNamespaceComparator.getInstance());
otherNamespaces.sort(JsfNamespaceComparator.getInstance());

int otherNamespaceSortPriority = 20;
for (String namespace : otherNamespaces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -79,7 +78,7 @@ public static <N,E,P> void performLayout( GraphPinScene<N, E, P> graph, boolean
}
Point index = new Point();
ArrayList<N> connected = new ArrayList<N> (node2connected.get(node));
Collections.sort(connected, new Comparator<N>() {
connected.sort(new Comparator<N>() {
public int compare(N node1, N node2) {
return node2connected.get(node1).size() - node2connected.get(node2).size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static PageFlowSceneElement getNextSelectableElement(PageFlowScene scene,

PageFlowSceneElement nextElement = null;
if (!sortedElements.isEmpty()) {
Collections.sort(sortedElements, new SceneElementComparator(scene));
sortedElements.sort(new SceneElementComparator(scene));
if (reverse) {
Collections.reverse(sortedElements);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -91,7 +90,7 @@ public static List<Template> getTemplates(TemplateType tt) {
result.add(new Template(folder.getName(), getLocalizedName(folder), (Integer) position));
}
}
Collections.sort(result, TEMPLATE_COMPARATOR);
result.sort(TEMPLATE_COMPARATOR);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ public void run() {
JsfVersion jsfVersion = JsfVersionUtils.forClasspath(content);
LibraryItem item = jsfVersion != null ? new LibraryItem(library, jsfVersion) : new LibraryItem(library, JsfVersion.JSF_1_1);
jsfLibraries.add(item);
Collections.sort(jsfLibraries, new Comparator<LibraryItem>() {
jsfLibraries.sort(new Comparator<LibraryItem>() {
@Override
public int compare(LibraryItem li1, LibraryItem li2) {
return li1.getLibrary().getDisplayName().compareTo(li2.getLibrary().getDisplayName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ protected boolean createKeys( List<FileObject> keys ) {
result = true;
}
keys.addAll( myKeys );
Collections.sort( keys, COMPARATOR);
keys.sort(COMPARATOR);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.swing.Action;
import javax.swing.SwingUtilities;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import org.apache.tools.ant.module.api.support.ActionUtils;
Expand Down Expand Up @@ -84,7 +82,6 @@
import org.openide.execution.ExecutorTask;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.FileUtil;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
Expand Down Expand Up @@ -232,7 +229,7 @@ private void updateKeys() {
}
}
if ( keys != null ){
Collections.sort(keys, WsdlOperationComparator.getInstance() );
keys.sort(WsdlOperationComparator.getInstance());
setKeys(keys);
}
else {
Expand Down Expand Up @@ -409,8 +406,7 @@ private AnnotationMirror getWebMethodAnnotation(
keys[0] = Collections.emptyList();
}
else {
Collections.sort( (List<WebOperationInfo>)keys[0],
OPERATION_INFO_COMPARATOR);
((List<WebOperationInfo>)keys[0]).sort(OPERATION_INFO_COMPARATOR);
}
setKeys(keys[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*
* @author mkuchtiak
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlOperation;
Expand Down Expand Up @@ -55,7 +54,7 @@ private void updateKeys() {
setKeys(Collections.<WsdlOperation>emptyList() );
}
else {
Collections.sort( keys , WsdlOperationComparator.getInstance());
keys.sort(WsdlOperationComparator.getInstance());
setKeys(keys);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void run() {

public List<ErrorDescription> getProblems(FileObject fileToTest) {
problems = AnnotationHolder.getInstance(fileToTest).getErrors();
Collections.sort(problems, new Comparator<ErrorDescription>() {
problems.sort(new Comparator<ErrorDescription>() {

public int compare(ErrorDescription o1, ErrorDescription o2) {
return o1.toString().compareTo(o2.toString());
Expand Down Expand Up @@ -238,7 +238,7 @@ public List<Fix> getFixes(FileObject fileToTest) {
for (ErrorDescription errorDescription : getProblems(fileToTest)) {
fixes.addAll(errorDescription.getFixes().getFixes());
}
Collections.sort(fixes, new Comparator<Fix>() {
fixes.sort(new Comparator<Fix>() {

public int compare(Fix o1, Fix o2) {
return o1.getText().compareTo(o2.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
*/
package org.netbeans.modules.websvc.rest.nodes;

import org.netbeans.modules.websvc.rest.support.Utils;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import java.beans.PropertyChangeListener;
import java.io.IOException;
Expand All @@ -39,7 +35,6 @@
import org.netbeans.modules.websvc.rest.model.api.RestServicesMetadata;
import org.netbeans.modules.websvc.rest.model.api.RestServicesModel;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;



Expand Down Expand Up @@ -72,7 +67,7 @@ public Void run(RestServicesMetadata metadata) throws IOException {
(HttpMethod) method));
}
}
Collections.sort( keys , COMPARATOR );
keys.sort(COMPARATOR);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -70,7 +69,7 @@ public Void run(RestServicesMetadata metadata) throws IOException {
locators.add((SubResourceLocator)method);
}
}
Collections.sort( locators , COMPARATOR );
locators.sort(COMPARATOR);
for (SubResourceLocator locator : locators){
list.add( new SubResourceLocatorNode(project,
className , locator));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected final void doGetAllInstancesReal() {
providers.remove(w);
}
}
Collections.sort(providers, comparator); // ?
providers.sort(comparator); // ?
LOG.info("Iterating full");
for (ServerWizardProvider wizard : providers.toArray(new ServerWizardProvider[0])) {
System.setProperty(propPrefix() + ++cnt, wizard.getDisplayName());
Expand All @@ -77,7 +77,7 @@ protected final void doGetAllInstancesErgo() {
providers.remove(w);
}
}
Collections.sort(providers, comparator);
providers.sort(comparator);
LOG.info("Iterating ergo");
for (ServerWizardProvider wizard : providers) {
String name = System.getProperty(propPrefix() + ++cnt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.netbeans.modules.gradle.api.execute.GradleExecConfiguration;
import org.netbeans.modules.gradle.customizer.CustomActionMapping;
import org.netbeans.modules.gradle.execute.ConfigurableActionProvider;
import org.netbeans.spi.project.ProjectConfigurationProvider;
import org.openide.awt.NotificationDisplayer;
import org.openide.awt.NotificationDisplayer.Priority;
import org.openide.util.NbBundle;
Expand Down Expand Up @@ -207,7 +206,7 @@ public void save() throws IOException {
String s = entry.getKey();
ActionsHolder h = entry.getValue();
List<ActionMapping> actions = new ArrayList<>(h.customActions.values());
Collections.sort(actions, ActionMapping::compareTo);
actions.sort(ActionMapping::compareTo);
try {
ActionPersistenceUtils.writeActions(project.getProjectDirectory(), s, actions);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private static Pair<Object[], Object> brandedOptions(boolean allowPermanent) {
);
}
List<Object> ordered = new ArrayList<>(options.keySet());
Collections.sort(ordered, (a, b) -> options.get(a) - options.get(b));
ordered.sort((a, b) -> options.get(a) - options.get(b));
ordered.add(DialogDescriptor.CANCEL_OPTION);
return Pair.of(ordered.toArray(new Object[ordered.size()]), def);
};
Expand Down
Loading

0 comments on commit a8efe63

Please sign in to comment.