Skip to content

Commit 482b905

Browse files
committed
Using Optional<T> semantics for 'replacedLib' in LibraryInstaller
Optional<T> helps to not forget to check about nullness where it is needed. This commit should be equivalent and shouln't fix any bug, BTW the Optional<T> semantic turns out to be useful in the next commits. Possibly all nullable values will be replaced by Optional in the future.
1 parent 212825e commit 482b905

File tree

7 files changed

+41
-34
lines changed

7 files changed

+41
-34
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collections;
3838
import java.util.LinkedList;
3939
import java.util.List;
40+
import java.util.Optional;
4041
import java.util.stream.Collectors;
4142

4243
public class ContributedLibraryReleases {
@@ -76,15 +77,15 @@ public void add(ContributedLibrary library) {
7677
selected = getLatest();
7778
}
7879

79-
public ContributedLibrary getInstalled() {
80+
public Optional<ContributedLibrary> getInstalled() {
8081
List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList());
8182
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
8283

8384
if (installedReleases.isEmpty()) {
84-
return null;
85+
return Optional.empty();
8586
}
8687

87-
return installedReleases.get(0);
88+
return Optional.of(installedReleases.get(0));
8889
}
8990

9091
public ContributedLibrary getLatest() {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Collections;
3737
import java.util.LinkedList;
3838
import java.util.List;
39+
import java.util.Optional;
3940
import java.util.stream.Collectors;
4041

4142
import javax.swing.JComboBox;
@@ -85,7 +86,7 @@ public Component getTableCellEditorComponent(JTable table, Object value,
8586

8687
setEnabled(true);
8788

88-
final ContributedLibrary installed = editorValue.getInstalled();
89+
final Optional<ContributedLibrary> mayInstalled = editorValue.getInstalled();
8990

9091
List<ContributedLibrary> releases = editorValue.getReleases().stream()
9192
.filter(new OnlyUpstreamReleasePredicate())
@@ -97,7 +98,7 @@ public Component getTableCellEditorComponent(JTable table, Object value,
9798
.filter(new InstalledPredicate()).filter(new BuiltInPredicate())
9899
.collect(Collectors.toList());
99100

100-
if (installed != null && !installedBuiltIn.contains(installed)) {
101+
if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) {
101102
uninstalledReleases.addAll(installedBuiltIn);
102103
}
103104

@@ -111,8 +112,8 @@ public Component getTableCellEditorComponent(JTable table, Object value,
111112
final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>();
112113

113114
uninstalledReleases.stream().forEach(input -> {
114-
if (installed == null
115-
|| VersionComparator.greaterThan(installed, input)) {
115+
if (!mayInstalled.isPresent()
116+
|| VersionComparator.greaterThan(mayInstalled.get(), input)) {
116117
uninstalledPreviousReleases.add(input);
117118
} else {
118119
uninstalledNewerReleases.add(input);
@@ -122,18 +123,18 @@ public Component getTableCellEditorComponent(JTable table, Object value,
122123
uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem);
123124

124125
editorCell.downgradeChooser
125-
.setVisible(installed != null
126+
.setVisible(mayInstalled.isPresent()
126127
&& (!uninstalledPreviousReleases.isEmpty()
127128
|| uninstalledNewerReleases.size() > 1));
128129
editorCell.downgradeButton
129-
.setVisible(installed != null
130+
.setVisible(mayInstalled.isPresent()
130131
&& (!uninstalledPreviousReleases.isEmpty()
131132
|| uninstalledNewerReleases.size() > 1));
132133

133134
editorCell.versionToInstallChooser.removeAllItems();
134135
uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem);
135136
editorCell.versionToInstallChooser
136-
.setVisible(installed == null && uninstalledReleases.size() > 1);
137+
.setVisible(!mayInstalled.isPresent() && uninstalledReleases.size() > 1);
137138

138139
editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3
139140
return editorCell;
@@ -153,7 +154,7 @@ protected void onRemove(ContributedLibrary selected) {
153154
}
154155

155156
protected void onInstall(ContributedLibrary selected,
156-
ContributedLibrary installed) {
157+
Optional<ContributedLibrary> mayInstalled) {
157158
// Empty
158159
}
159160

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.awt.Component;
88
import java.awt.Dimension;
99
import java.awt.Insets;
10+
import java.util.Optional;
1011

1112
import javax.swing.Box;
1213
import javax.swing.BoxLayout;
@@ -116,16 +117,16 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
116117
return;
117118

118119
ContributedLibrary selected = releases.getSelected();
119-
ContributedLibrary installed = releases.getInstalled();
120+
Optional<ContributedLibrary> mayInstalled = releases.getInstalled();
120121

121122
boolean installable, upgradable;
122-
if (installed == null) {
123+
if (!mayInstalled.isPresent()) {
123124
installable = true;
124125
upgradable = false;
125126
} else {
126127
installable = false;
127128
upgradable = new DownloadableContributionVersionComparator()
128-
.compare(selected, installed) > 0;
129+
.compare(selected, mayInstalled.get()) > 0;
129130
}
130131
if (installable) {
131132
installButton.setText(tr("Install"));
@@ -151,7 +152,7 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
151152

152153
// Library name...
153154
desc += format("<b>{0}</b>", name);
154-
if (installed != null && installed.isReadOnly()) {
155+
if (mayInstalled.isPresent() && mayInstalled.get().isReadOnly()) {
155156
desc += " Built-In ";
156157
}
157158

@@ -162,8 +163,8 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
162163
}
163164

164165
// ...version.
165-
if (installed != null) {
166-
String installedVer = installed.getParsedVersion();
166+
if (mayInstalled.isPresent()) {
167+
String installedVer = mayInstalled.get().getParsedVersion();
167168
if (installedVer == null) {
168169
desc += " " + tr("Version unknown");
169170
} else {
@@ -172,7 +173,7 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
172173
}
173174
desc += "</font>";
174175

175-
if (installed != null) {
176+
if (mayInstalled.isPresent()) {
176177
desc += " <strong><font color=\"#00979D\">INSTALLED</font></strong>";
177178
}
178179

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collection;
3939
import java.util.Collections;
4040
import java.util.LinkedList;
41+
import java.util.Optional;
4142
import java.util.function.Predicate;
4243

4344
import javax.swing.Box;
@@ -80,11 +81,11 @@ protected TableCellRenderer createCellRenderer() {
8081
protected InstallerTableCell createCellEditor() {
8182
return new ContributedLibraryTableCellEditor() {
8283
@Override
83-
protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) {
84-
if (selectedLibrary.isReadOnly()) {
85-
onRemovePressed(installedLibrary);
84+
protected void onInstall(ContributedLibrary selectedLibrary, Optional<ContributedLibrary> mayInstalledLibrary) {
85+
if (selectedLibrary.isReadOnly() && mayInstalledLibrary.isPresent()) {
86+
onRemovePressed(mayInstalledLibrary.get());
8687
} else {
87-
onInstallPressed(selectedLibrary, installedLibrary);
88+
onInstallPressed(selectedLibrary, mayInstalledLibrary);
8889
}
8990
}
9091

@@ -212,12 +213,12 @@ protected void onUpdatePressed() {
212213
installerThread.start();
213214
}
214215

215-
public void onInstallPressed(final ContributedLibrary lib, final ContributedLibrary replaced) {
216+
public void onInstallPressed(final ContributedLibrary lib, final Optional<ContributedLibrary> mayReplaced) {
216217
clearErrorMessage();
217218
installerThread = new Thread(() -> {
218219
try {
219220
setProgressVisible(true, tr("Installing..."));
220-
installer.install(lib, replaced, this::setProgress);
221+
installer.install(lib, mayReplaced, this::setProgress);
221222
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
222223
//getContribModel().updateLibrary(lib);
223224
} catch (Exception e) {

app/src/processing/app/Base.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ public Base(String[] args) throws Exception {
358358
System.exit(1);
359359
}
360360

361-
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
362-
if (selected.isReadOnly()) {
363-
libraryInstaller.remove(installed, progressListener);
361+
Optional<ContributedLibrary> mayInstalled = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
362+
if (selected.isReadOnly() && mayInstalled.isPresent()) {
363+
libraryInstaller.remove(mayInstalled.get(), progressListener);
364364
} else {
365-
libraryInstaller.install(selected, installed, progressListener);
365+
libraryInstaller.install(selected, mayInstalled, progressListener);
366366
}
367367
}
368368

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ public List<String> getTypes() {
9191
return types;
9292
}
9393

94-
public ContributedLibrary getInstalled(String name) {
94+
public Optional<ContributedLibrary> getInstalled(String name) {
9595
List<ContributedLibrary> installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList());
9696
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
9797

9898
if (installedReleases.isEmpty()) {
99-
return null;
99+
return Optional.empty();
100100
}
101101

102-
return installedReleases.get(0);
102+
return Optional.of(installedReleases.get(0));
103103
}
104104
}

arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.io.File;
4444
import java.io.IOException;
4545
import java.net.URL;
46+
import java.util.Optional;
4647

4748
import static processing.app.I18n.tr;
4849

@@ -82,7 +83,7 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
8283
rescanLibraryIndex(progress, progressListener);
8384
}
8485

85-
public synchronized void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception {
86+
public synchronized void install(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener) throws Exception {
8687
if (lib.isInstalled()) {
8788
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
8889
return;
@@ -119,7 +120,9 @@ public synchronized void install(ContributedLibrary lib, ContributedLibrary repl
119120

120121
// Step 3: Remove replaced library and move installed one to the correct location
121122
// TODO: Fix progress bar...
122-
remove(replacedLib, progressListener);
123+
if (mayReplacedLib.isPresent()) {
124+
remove(mayReplacedLib.get(), progressListener);
125+
}
123126
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
124127
tmpFolder.renameTo(destFolder);
125128
progress.stepDone();
@@ -129,7 +132,7 @@ public synchronized void install(ContributedLibrary lib, ContributedLibrary repl
129132
}
130133

131134
public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
132-
if (lib == null || lib.isReadOnly()) {
135+
if (lib.isReadOnly()) {
133136
return;
134137
}
135138

0 commit comments

Comments
 (0)