Skip to content

Commit b3d01d8

Browse files
committed
Moved install-related fields out of DownloadableContribution
Those fields have a slightly different meaning on each object that extends DownloadableContribution and having them grouped in DownloadableContribution only increase confusion in change of a (very) tiny code reuse. Moreover: - the `readOnly` field has been renamed to `builtIn` - predicates have been replaced by lambdas - DownloadableContributionBuiltInAtTheBottomComparator has been replaced with a singleton instance
1 parent 0042a30 commit b3d01d8

16 files changed

+144
-236
lines changed

app/src/cc/arduino/contributions/BuiltInCoreIsNewerCheck.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@
2929

3030
package cc.arduino.contributions;
3131

32-
import cc.arduino.contributions.filters.BuiltInPredicate;
33-
import cc.arduino.contributions.filters.InstalledPredicate;
34-
import cc.arduino.contributions.packages.ContributedPackage;
3532
import cc.arduino.contributions.packages.ContributedPlatform;
3633
import processing.app.Base;
3734
import processing.app.BaseNoGui;
3835
import processing.app.I18n;
3936
import processing.app.PreferencesData;
4037

4138
import javax.swing.*;
42-
import java.util.Collection;
4339
import java.util.List;
40+
import java.util.Optional;
4441
import java.util.stream.Collectors;
4542

4643
import static processing.app.I18n.tr;
@@ -67,13 +64,21 @@ private void builtInPackageIsNewerCheck() throws InterruptedException {
6764
return;
6865
}
6966

70-
List<ContributedPlatform> contributedPlatforms = BaseNoGui.indexer.getPackages().stream().map(ContributedPackage::getPlatforms).flatMap(Collection::stream).collect(Collectors.toList());
67+
List<ContributedPlatform> contributedPlatforms = BaseNoGui.indexer
68+
.getPackages().stream() //
69+
.map(pack -> pack.getPlatforms()) //
70+
.flatMap(platfs -> platfs.stream()) //
71+
.collect(Collectors.toList());
7172

72-
List<ContributedPlatform> installedBuiltInPlatforms = contributedPlatforms.stream().filter(new InstalledPredicate()).filter(new BuiltInPredicate()).collect(Collectors.toList());
73-
if (installedBuiltInPlatforms.size() != 1) {
73+
Optional<ContributedPlatform> mayInstalledBuiltIn = contributedPlatforms
74+
.stream() //
75+
.filter(p -> p.isInstalled()) //
76+
.filter(p -> p.isBuiltIn()) //
77+
.findFirst();
78+
if (!mayInstalledBuiltIn.isPresent()) {
7479
return;
7580
}
76-
final ContributedPlatform installedBuiltIn = installedBuiltInPlatforms.get(0);
81+
final ContributedPlatform installedBuiltIn = mayInstalledBuiltIn.get();
7782

7883
ContributedPlatform installedNotBuiltIn = BaseNoGui.indexer.getInstalled(installedBuiltIn.getParentPackage().getName(), installedBuiltIn.getArchitecture());
7984
if (installedNotBuiltIn == null) {

app/src/cc/arduino/contributions/packages/ui/ContributedPlatformReleases.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import java.util.List;
3535
import java.util.stream.Collectors;
3636

37-
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
38-
import cc.arduino.contributions.filters.InstalledPredicate;
3937
import cc.arduino.contributions.packages.ContributedPackage;
4038
import cc.arduino.contributions.packages.ContributedPlatform;
4139

@@ -72,10 +70,9 @@ public void add(ContributedPlatform platform) {
7270

7371
public ContributedPlatform getInstalled() {
7472
List<ContributedPlatform> installedReleases = releases.stream()
75-
.filter(new InstalledPredicate()).collect(Collectors.toList());
76-
Collections
77-
.sort(installedReleases,
78-
new DownloadableContributionBuiltInAtTheBottomComparator());
73+
.filter(p -> p.isInstalled()) //
74+
.collect(Collectors.toList());
75+
Collections.sort(installedReleases, ContributedPlatform.BUILTIN_AS_LAST);
7976

8077
if (installedReleases.isEmpty()) {
8178
return null;

app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242

4343
import cc.arduino.contributions.DownloadableContributionVersionComparator;
4444
import cc.arduino.contributions.VersionComparator;
45-
import cc.arduino.contributions.filters.BuiltInPredicate;
46-
import cc.arduino.contributions.filters.InstalledPredicate;
4745
import cc.arduino.contributions.packages.ContributedPlatform;
4846
import cc.arduino.contributions.ui.InstallerTableCell;
4947
import cc.arduino.utils.ReverseComparator;
@@ -86,11 +84,13 @@ public Component getTableCellEditorComponent(JTable table, Object _value,
8684
final ContributedPlatform installed = value.getInstalled();
8785

8886
List<ContributedPlatform> releases = new LinkedList<>(value.releases);
89-
List<ContributedPlatform> uninstalledReleases = releases.stream()
90-
.filter(new InstalledPredicate().negate()).collect(Collectors.toList());
87+
List<ContributedPlatform> uninstalledReleases = releases.stream() //
88+
.filter(p -> !p.isInstalled()) //
89+
.collect(Collectors.toList());
9190

92-
List<ContributedPlatform> installedBuiltIn = releases.stream()
93-
.filter(new InstalledPredicate()).filter(new BuiltInPredicate())
91+
List<ContributedPlatform> installedBuiltIn = releases.stream() //
92+
.filter(p -> p.isInstalled()) //
93+
.filter(p -> p.isBuiltIn()) //
9494
.collect(Collectors.toList());
9595

9696
if (installed != null && !installedBuiltIn.contains(installed)) {

app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellJPanel.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void update(JTable parentTable, Object value, boolean isSelected,
170170
upgradable = false;
171171
} else {
172172
installable = false;
173-
removable = !installed.isReadOnly() && !hasBuiltInRelease;
173+
removable = !installed.isBuiltIn() && !hasBuiltInRelease;
174174
upgradable = new DownloadableContributionVersionComparator()
175175
.compare(selected, installed) > 0;
176176
}
@@ -187,7 +187,7 @@ void update(JTable parentTable, Object value, boolean isSelected,
187187

188188
String desc = "<html><body>";
189189
desc += "<b>" + selected.getName() + "</b>";
190-
if (installed != null && installed.isReadOnly()) {
190+
if (installed != null && installed.isBuiltIn()) {
191191
desc += " Built-In ";
192192
}
193193

app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected InstallerTableCell createCellEditor() {
7272
@Override
7373
protected void onInstall(ContributedPlatform selected,
7474
ContributedPlatform installed) {
75-
if (selected.isReadOnly()) {
75+
if (selected.isBuiltIn()) {
7676
onRemovePressed(installed, false);
7777
} else {
7878
onInstallPressed(selected, installed);
@@ -166,7 +166,7 @@ public void onInstallPressed(final ContributedPlatform platformToInstall,
166166
List<String> errors = new LinkedList<>();
167167
try {
168168
setProgressVisible(true, tr("Installing..."));
169-
if (platformToRemove != null && !platformToRemove.isReadOnly()) {
169+
if (platformToRemove != null && !platformToRemove.isBuiltIn()) {
170170
errors.addAll(installer.remove(platformToRemove));
171171
}
172172
errors.addAll(installer.install(platformToInstall, this::setProgress));

app/src/processing/app/Base.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ public Base(String[] args) throws Exception {
320320

321321
ContributedPlatform installed = indexer.getInstalled(boardToInstallParts[0], boardToInstallParts[1]);
322322

323-
if (!selected.isReadOnly()) {
323+
if (!selected.isBuiltIn()) {
324324
contributionInstaller.install(selected, progressListener);
325325
}
326326

327-
if (installed != null && !installed.isReadOnly()) {
327+
if (installed != null && !installed.isBuiltIn()) {
328328
contributionInstaller.remove(installed);
329329
}
330330

arduino-core/src/cc/arduino/contributions/DownloadableContribution.java

+4-42
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@
3535

3636
public abstract class DownloadableContribution {
3737

38-
// XXX: maybe installed fields should not be here but in UserLibrary and ContributedPlatform?
39-
private boolean installed;
40-
private File installedFolder;
41-
42-
private boolean downloaded;
43-
private File downloadedFile;
44-
4538
public abstract String getUrl();
4639

4740
public abstract String getVersion();
@@ -52,6 +45,8 @@ public abstract class DownloadableContribution {
5245

5346
public abstract String getArchiveFileName();
5447

48+
private boolean downloaded;
49+
5550
public boolean isDownloaded() {
5651
return downloaded;
5752
}
@@ -60,6 +55,8 @@ public void setDownloaded(boolean downloaded) {
6055
this.downloaded = downloaded;
6156
}
6257

58+
private File downloadedFile;
59+
6360
public File getDownloadedFile() {
6461
return downloadedFile;
6562
}
@@ -68,46 +65,11 @@ public void setDownloadedFile(File downloadedFile) {
6865
this.downloadedFile = downloadedFile;
6966
}
7067

71-
public boolean isInstalled() {
72-
return installed;
73-
}
74-
75-
public void setInstalled(boolean installed) {
76-
this.installed = installed;
77-
}
78-
79-
public File getInstalledFolder() {
80-
return installedFolder;
81-
}
82-
83-
public void setInstalledFolder(File installedFolder) {
84-
this.installedFolder = installedFolder;
85-
}
86-
87-
private boolean readOnly;
88-
89-
public boolean isReadOnly() {
90-
return readOnly;
91-
}
92-
93-
public void setReadOnly(boolean readOnly) {
94-
this.readOnly = readOnly;
95-
}
96-
9768
public String getParsedVersion() {
9869
Version version = VersionHelper.valueOf(getVersion());
9970
if (version == null) {
10071
return null;
10172
}
10273
return version.toString();
10374
}
104-
105-
@Override
106-
public String toString() {
107-
String res = "";
108-
if (installed) {
109-
res += "installed on " + installedFolder.getAbsolutePath() + " (" + getSize() + " bytes)";
110-
}
111-
return res;
112-
}
11375
}

arduino-core/src/cc/arduino/contributions/DownloadableContributionBuiltInAtTheBottomComparator.java

-43
This file was deleted.

arduino-core/src/cc/arduino/contributions/filters/BuiltInPredicate.java

-43
This file was deleted.

arduino-core/src/cc/arduino/contributions/filters/InstalledPredicate.java

-43
This file was deleted.

0 commit comments

Comments
 (0)