29
29
30
30
package cc .arduino .contributions .libraries ;
31
31
32
- import cc .arduino .contributions .DownloadableContributionBuiltInAtTheBottomComparator ;
33
32
import cc .arduino .contributions .VersionComparator ;
33
+ import processing .app .packages .UserLibraryFolder .Location ;
34
34
35
- import java .util .Collections ;
36
35
import java .util .LinkedList ;
37
36
import java .util .List ;
38
37
import java .util .Optional ;
39
- import java .util .stream .Collectors ;
40
38
41
39
public class ContributedLibraryReleases {
42
40
@@ -49,6 +47,10 @@ public ContributedLibraryReleases(ContributedLibrary library) {
49
47
add (library );
50
48
}
51
49
50
+ public ContributedLibraryReleases (List <ContributedLibrary > libraries ) {
51
+ libraries .forEach (this ::add );
52
+ }
53
+
52
54
public List <ContributedLibrary > getReleases () {
53
55
return releases ;
54
56
}
@@ -76,12 +78,16 @@ public void add(ContributedLibrary library) {
76
78
}
77
79
78
80
public Optional <ContributedLibrary > getInstalled () {
79
- List <ContributedLibrary > installedReleases = releases .stream ().filter (l -> l .isLibraryInstalled ()).collect (Collectors .toList ());
80
- if (installedReleases .isEmpty ()) {
81
- return Optional .empty ();
82
- }
83
- Collections .sort (installedReleases , new DownloadableContributionBuiltInAtTheBottomComparator ());
84
- return Optional .of (installedReleases .get (0 ));
81
+ return releases .stream () //
82
+ .filter (ContributedLibrary ::isLibraryInstalled ) //
83
+ .reduce ((x , y ) -> {
84
+ Location lx = x .getInstalledLibrary ().get ().getLocation ();
85
+ Location ly = y .getInstalledLibrary ().get ().getLocation ();
86
+ if (lx == ly ) {
87
+ return VersionComparator .max (x , y );
88
+ }
89
+ return lx == Location .SKETCHBOOK ? x : y ;
90
+ });
85
91
}
86
92
87
93
public ContributedLibrary getLatest () {
0 commit comments