Skip to content

Commit 8c6e397

Browse files
committed
Refactored ContributedLibraryReleases
Mostly simplified and improved readability.
1 parent 5cc66e9 commit 8c6e397

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

Diff for: arduino-core/src/cc/arduino/contributions/libraries/ContributedLibraryReleases.java

+21-25
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,43 @@
4040

4141
public class ContributedLibraryReleases {
4242

43-
private final ContributedLibrary library;
44-
private final List<ContributedLibrary> releases;
45-
private final List<String> versions;
46-
47-
private ContributedLibrary selected;
43+
private List<ContributedLibrary> releases = new LinkedList<>();
44+
private List<String> versions = new LinkedList<>();
45+
private ContributedLibrary latest = null;
46+
private ContributedLibrary selected = null;
4847

4948
public ContributedLibraryReleases(ContributedLibrary library) {
50-
this.library = library;
51-
this.versions = new LinkedList<>();
52-
this.releases = new LinkedList<>();
53-
this.selected = null;
5449
add(library);
5550
}
5651

5752
public ContributedLibrary getLibrary() {
58-
return library;
53+
return latest;
5954
}
6055

6156
public List<ContributedLibrary> getReleases() {
6257
return releases;
6358
}
6459

6560
public boolean shouldContain(ContributedLibrary lib) {
66-
return lib.getName().equals(library.getName());
61+
if (latest == null) {
62+
return true;
63+
}
64+
return lib.getName().equals(latest.getName());
6765
}
6866

6967
public void add(ContributedLibrary library) {
68+
if (latest == null) {
69+
latest = library;
70+
}
7071
releases.add(library);
7172
String version = library.getParsedVersion();
7273
if (version != null) {
7374
versions.add(version);
7475
}
75-
selected = getLatest();
76+
if (VersionComparator.greaterThan(version, latest.getParsedVersion())) {
77+
latest = library;
78+
}
79+
selected = latest;
7680
}
7781

7882
public Optional<ContributedLibrary> getInstalled() {
@@ -85,25 +89,17 @@ public Optional<ContributedLibrary> getInstalled() {
8589
}
8690

8791
public ContributedLibrary getLatest() {
88-
List<ContributedLibrary> rels = new LinkedList<>(releases);
89-
final VersionComparator versionComparator = new VersionComparator();
90-
Collections.sort(rels, (x, y) -> versionComparator.compare(x.getParsedVersion(), y.getParsedVersion()));
91-
92-
if (rels.isEmpty()) {
93-
return null;
94-
}
95-
96-
return rels.get(rels.size() - 1);
92+
return latest;
9793
}
9894

9995
public ContributedLibrary getSelected() {
10096
return selected;
10197
}
10298

103-
public void select(ContributedLibrary value) {
104-
for (ContributedLibrary plat : releases) {
105-
if (plat == value) {
106-
selected = plat;
99+
public void select(ContributedLibrary lib) {
100+
for (ContributedLibrary r : releases) {
101+
if (r == lib) {
102+
selected = r;
107103
return;
108104
}
109105
}

0 commit comments

Comments
 (0)