Skip to content

Commit 8793a6d

Browse files
committed
Fix libraries comparator logic
Previously it could lead to contract violations for example consider this: A = Library{ Name:"A", Types: ["Sensors"] } B = Library{ Name:"B", Types: null } C = Library{ Name:"C", Types: ["Arduino"] } it results in: A<B (because B has Types==null and compare("A","B")<0) B<C (because B has Types==null and compare("B","C")<0) C<A (becuase C has Types=="Arduino" and the comparator returns -1) This commit fix this behavior
1 parent 678420c commit 8793a6d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryReleasesComparator.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import cc.arduino.contributions.libraries.ContributedLibrary;
3333
import cc.arduino.contributions.libraries.ContributedLibraryReleases;
3434

35+
import java.util.Arrays;
3536
import java.util.Comparator;
37+
import java.util.List;
3638

3739
public class ContributedLibraryReleasesComparator implements Comparator<ContributedLibraryReleases> {
3840

@@ -47,9 +49,11 @@ public int compare(ContributedLibraryReleases o1, ContributedLibraryReleases o2)
4749
ContributedLibrary lib1 = o1.getLatest();
4850
ContributedLibrary lib2 = o2.getLatest();
4951

50-
if (lib1.getTypes() == null || lib2.getTypes() == null) {
51-
return compareName(lib1, lib2);
52-
}
52+
List<String> types1 = lib1.getTypes();
53+
List<String> types2 = lib2.getTypes();
54+
if (types1 == null) types1 = Arrays.asList();
55+
if (types2 == null) types2 = Arrays.asList();
56+
5357
if (lib1.getTypes().contains(firstType) && lib2.getTypes().contains(firstType)) {
5458
return compareName(lib1, lib2);
5559
}

0 commit comments

Comments
 (0)