Skip to content

Commit

Permalink
SOLR-16318: replace 'Collections.sort(List, Comparator)' use (apache#…
Browse files Browse the repository at this point in the history
…1016)

Co-authored-by: Kevin Risden <[email protected]>
  • Loading branch information
cpoerschke and risdenk authored Oct 14, 2022
1 parent 2152ba2 commit 6988786
Show file tree
Hide file tree
Showing 34 changed files with 76 additions and 101 deletions.
3 changes: 3 additions & 0 deletions gradle/validation/forbidden-apis/defaults.all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ java.lang.System#currentTimeMillis()

@defaultMessage Use slf4j classes instead
java.util.logging.**

@defaultMessage Use List.sort(Comparator) instead of Collections.sort(List, Comparator) please.
java.util.Collections#sort(java.util.List, java.util.Comparator)
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public static String assignShard(DocCollection collection, Integer numShards) {
map.put(shardId, cnt);
}

Collections.sort(
shardIdNames,
shardIdNames.sort(
(String o1, String o2) -> {
Integer one = map.get(o1);
Integer two = map.get(o2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public boolean accept(File file) {
return; // nothing to do (no log message needed)

List<File> dirsList = Arrays.asList(oldIndexDirs);
Collections.sort(dirsList, Collections.reverseOrder());
dirsList.sort(Collections.reverseOrder());

int i = 0;
if (afterCoreReload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -287,7 +286,7 @@ private synchronized List<String> getCollections(SolrZkClient zkClient)
if (fromZk != null) cachedCollections.addAll(fromZk);

// sort the final merged set of collections
Collections.sort(cachedCollections, this);
cachedCollections.sort(this);
}

return cachedCollections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -292,7 +291,7 @@ private Float getFloat(SolrDocument doc) {
}
};

Collections.sort(l, c);
l.sort(c);

// Truncate list to maxSize
if (l.size() > maxSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ public void sort() {

if (dirty) {
if (facetFieldSort.equals(FacetParams.FACET_SORT_COUNT)) {
Collections.sort(this.explicitValues, new PivotFacetCountComparator());
this.explicitValues.sort(new PivotFacetCountComparator());
} else if (facetFieldSort.equals(FacetParams.FACET_SORT_INDEX)) {
Collections.sort(this.explicitValues, new PivotFacetValueComparator());
this.explicitValues.sort(new PivotFacetValueComparator());
}
dirty = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ private List<Long> resolveVersionRanges(String versionsStr, UpdateLog ulog) {
versionAvailable = recentUpdates.getVersions(ulog.getNumRecordsToKeep());
}
// sort versions
Collections.sort(versionAvailable, PeerSync.absComparator);
versionAvailable.sort(PeerSync.absComparator);

// This can be done with single pass over both ranges and versionsAvailable, that would require
// merging ranges. We currently use Set to ensure there are no duplicates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ protected Object doHighlightingByHighlighter(
if (frags.size() > 0) {
// sort such that the fragments with the highest score come first
if (!preserveMulti) {
Collections.sort(frags, (arg0, arg1) -> Float.compare(arg1.getScore(), arg0.getScore()));
frags.sort((arg0, arg1) -> Float.compare(arg1.getScore(), arg0.getScore()));
}

// Truncate list to hl.snippets, but not when hl.preserveMulti
Expand Down
3 changes: 1 addition & 2 deletions solr/core/src/java/org/apache/solr/schema/IndexSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1707,8 +1707,7 @@ public List<SimpleOrderedMap<Object>> getCopyFieldProperties(
SortedMap<String, List<CopyField>> sortedCopyFields = new TreeMap<>(copyFieldsMap);
for (List<CopyField> copyFields : sortedCopyFields.values()) {
copyFields = new ArrayList<>(copyFields);
Collections.sort(
copyFields,
copyFields.sort(
(cf1, cf2) -> {
// sources are all the same, just sorting by destination here
return cf1.getDestination().getName().compareTo(cf2.getDestination().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ public ProcessedFilter getProcessedFilter(DocSet setFilter, List<Query> queries)

// Set pf.postFilter
if (postFilters != null) {
Collections.sort(postFilters, sortByCost);
postFilters.sort(sortByCost);
for (int i = postFilters.size() - 1; i >= 0; i--) {
DelegatingCollector prev = pf.postFilter;
pf.postFilter = postFilters.get(i).getFilterCollector(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public void sortBuckets(final FacetRequest.FacetSort sort) {
int v = -Long.compare(o1.count, o2.count) * sortMul;
return v == 0 ? o1.bucketValue.compareTo(o2.bucketValue) : v;
};
Collections.sort(sortedBuckets, comparator);
sortedBuckets.sort(comparator);
} else if ("index".equals(sort.sortVariable)) {
comparator = (o1, o2) -> -o1.bucketValue.compareTo(o2.bucketValue) * sortMul;
Collections.sort(sortedBuckets, comparator);
sortedBuckets.sort(comparator);
} else {
final String key = sort.sortVariable;

Expand Down Expand Up @@ -136,7 +136,7 @@ public void sortBuckets(final FacetRequest.FacetSort sort) {
}
}
Collections.sort(lst);
Collections.sort(nulls, (o1, o2) -> o1.bucketValue.compareTo(o2.bucketValue));
nulls.sort((o1, o2) -> o1.bucketValue.compareTo(o2.bucketValue));

ArrayList<FacetBucket> out = new ArrayList<>(buckets.size());
for (SortVal sv : lst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -112,7 +111,7 @@ public PossibilityIterator(
} else {
// Needs to be in token-offset order so that the match-and-replace
// option for collations can work.
Collections.sort(rsp.corrections, new StartOffsetComparator());
rsp.corrections.sort(new StartOffsetComparator());
if (removeDuplicates.add(rsp)) {
rankedPossibilities.offer(rsp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private boolean iAmInChargeOfPeriodicDeletes() {
return false;
}
List<Slice> slices = new ArrayList<>(Arrays.asList(docCollection.getActiveSlicesArr()));
Collections.sort(slices, COMPARE_SLICES_BY_NAME);
slices.sort(COMPARE_SLICES_BY_NAME);
Replica firstSliceLeader = slices.get(0).getLeader();
if (null == firstSliceLeader) {
log.warn("Slice in charge of periodic deletes for {} does not currently have a leader", col);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.solr.update.processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -122,7 +121,7 @@ public void add(String content) {
}
profile.add(t);
}
Collections.sort(profile, new TokenComparator());
profile.sort(new TokenComparator());
StringBuilder newText = new StringBuilder();
it = profile.iterator();
while (it.hasNext()) {
Expand Down
6 changes: 2 additions & 4 deletions solr/core/src/test/org/apache/solr/TestGroupingSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -1708,7 +1707,7 @@ public void testRandomGrouping() throws Exception {

// first sort the docs in each group
for (Grp grp : groups.values()) {
Collections.sort(grp.docs, groupComparator);
grp.docs.sort(groupComparator);
}

// now sort the groups
Expand All @@ -1719,8 +1718,7 @@ public void testRandomGrouping() throws Exception {
}

List<Grp> sortedGroups = new ArrayList<>(groups.values());
Collections.sort(
sortedGroups,
sortedGroups.sort(
groupComparator == sortComparator
? createFirstDocComparator(sortComparator)
: createMaxDocComparator(sortComparator));
Expand Down
3 changes: 1 addition & 2 deletions solr/core/src/test/org/apache/solr/TestJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -374,7 +373,7 @@ public void testRandomJoin() throws Exception {
Set<Comparable> docs = join(fromDocs, pivot);
List<Doc> docList = new ArrayList<>(docs.size());
for (@SuppressWarnings({"rawtypes"}) Comparable id : docs) docList.add(model.get(id));
Collections.sort(docList, createComparator("_docid_", true, false, false, false));
docList.sort(createComparator("_docid_", true, false, false, false));
List<Object> sortedDocs = new ArrayList<>();
for (Doc doc : docList) {
if (sortedDocs.size() >= 10) break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testComparator() {
for (int i = 0; i < 10; i++) {
List<CountsForEachShard> copy = new ArrayList<>(inputCounts);
Collections.shuffle(copy, random());
Collections.sort(copy, CoreSorter.countsComparator);
copy.sort(CoreSorter.countsComparator);
for (int j = 0; j < copy.size(); j++) {
assertEquals(expectedCounts.get(j), copy.get(j));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.solr.SolrTestCaseJ4;
Expand Down Expand Up @@ -318,7 +317,7 @@ private List<Tuple> getTuples(final SolrParams params, String ofInterest) throws
}
}
tupleStream.close();
Collections.sort(tuples, (o1, o2) -> (o1.getString("id").compareTo(o2.getString("id"))));
tuples.sort((o1, o2) -> (o1.getString("id").compareTo(o2.getString("id"))));
return tuples;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.solr.handler.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -2432,16 +2431,16 @@ private void assertNullFacetTypeInsidePivot(String facetType, List<PivotField> p
// Useful to check for errors, orders lists and does toString() equality check
private void testOrderedPivotsStringEquality(
List<PivotField> expectedPlacePivots, List<PivotField> placePivots) {
Collections.sort(expectedPlacePivots, new PivotFieldComparator());
expectedPlacePivots.sort(new PivotFieldComparator());
for (PivotField expectedPivot : expectedPlacePivots) {
if (expectedPivot.getPivot() != null) {
Collections.sort(expectedPivot.getPivot(), new PivotFieldComparator());
expectedPivot.getPivot().sort(new PivotFieldComparator());
}
}
Collections.sort(placePivots, new PivotFieldComparator());
placePivots.sort(new PivotFieldComparator());
for (PivotField pivot : placePivots) {
if (pivot.getPivot() != null) {
Collections.sort(pivot.getPivot(), new PivotFieldComparator());
pivot.getPivot().sort(new PivotFieldComparator());
}
}
assertEquals(expectedPlacePivots.toString(), placePivots.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,18 +655,18 @@ void expectedValsAsStrings(final FieldInfo info, List<String> valsAsStrings) {
switch (testFieldType.getSolrTypeClass()) {
case "solr.TrieIntField":
case "solr.TrieLongField":
Collections.sort(valsAsStrings, Comparator.comparingInt(Integer::parseInt));
valsAsStrings.sort(Comparator.comparingInt(Integer::parseInt));
break;
case "solr.IntPointField":
case "solr.LongPointField":
Collections.sort(valsAsStrings, Comparator.comparingLong(Long::parseLong));
valsAsStrings.sort(Comparator.comparingLong(Long::parseLong));
break;

case "solr.TrieFloatField":
case "solr.FloatPointField":
case "solr.TrieDoubleField":
case "solr.DoublePointField":
Collections.sort(valsAsStrings, Comparator.comparingDouble(Double::parseDouble));
valsAsStrings.sort(Comparator.comparingDouble(Double::parseDouble));
break;

case "solr.TrieDateField":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -279,8 +278,7 @@ public void merge(ResponseBuilder rb, ShardRequest sreq) {
} // end for-each-doc-in-response
} // end for-each-response

Collections.sort(
shardDocs,
shardDocs.sort(
(o1, o2) -> {
if (o1.score < o2.score) {
return 1;
Expand Down Expand Up @@ -601,8 +599,7 @@ public void merge(ResponseBuilder rb, ShardRequest sreq) {
} // end for-each-doc-in-response
} // end for-each-response

Collections.sort(
shardDocs,
shardDocs.sort(
(o1, o2) -> {
if (o1.score < o2.score) {
return 1;
Expand Down Expand Up @@ -723,8 +720,7 @@ public int topDocsSize() {
}

public TopDocs topDocs() {
Collections.sort(
list,
list.sort(
new Comparator<>() {
public int compare(ScoreDoc s1, ScoreDoc s2) {
if (s1.score == s2.score) {
Expand Down Expand Up @@ -785,8 +781,7 @@ public int topDocsSize() {
}

public TopDocs topDocs() {
Collections.sort(
list,
list.sort(
new Comparator<>() {
public int compare(ScoreDoc s1, ScoreDoc s2) {
if (s1.score == s2.score) {
Expand Down
4 changes: 1 addition & 3 deletions solr/core/src/test/org/apache/solr/search/TestSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -344,8 +343,7 @@ public void collect(int doc) throws IOException {

searcher.search(query, myCollector);

Collections.sort(
collectedDocs,
collectedDocs.sort(
(o1, o2) -> {
String v1 = o1.val == null ? nullRep : o1.val;
String v2 = o2.val == null ? nullRep : o2.val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ public void testComplex() throws Exception {
for (int i = 0; i < honda_model_counts.length - 1; i++) {
idx.add(i);
}
Collections.sort(
idx,
idx.sort(
(o1, o2) -> {
int cmp = honda_model_counts[o2] - honda_model_counts[o1];
return cmp == 0 ? o1 - o2 : cmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -321,7 +320,7 @@ public void testRandomJoin() throws Exception {
Set<Comparable> docs = join(fromDocs, pivot);
List<Doc> docList = new ArrayList<Doc>(docs.size());
for (Comparable id : docs) docList.add(model.get(id));
Collections.sort(docList, createComparator("_docid_", true, false, false, false));
docList.sort(createComparator("_docid_", true, false, false, false));
List sortedDocs = new ArrayList();
for (Doc doc : docList) {
if (sortedDocs.size() >= 10) break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ private void testRandomStringSort(SortField.Type type) throws Exception {
}

// Compute expected results:
Collections.sort(
f.matchValues,
f.matchValues.sort(
new Comparator<BytesRef>() {
@Override
public int compare(BytesRef a, BytesRef b) {
Expand Down
Loading

0 comments on commit 6988786

Please sign in to comment.