1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-15 12:29:26 +01:00

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.
This commit is contained in:
Cristian Maglie 2017-12-17 21:02:18 +01:00
parent 212825eb55
commit 482b905a62
7 changed files with 41 additions and 34 deletions

View File

@ -37,6 +37,7 @@ import cc.arduino.contributions.ui.FilteredAbstractTableModel;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ContributedLibraryReleases { public class ContributedLibraryReleases {
@ -76,15 +77,15 @@ public class ContributedLibraryReleases {
selected = getLatest(); selected = getLatest();
} }
public ContributedLibrary getInstalled() { public Optional<ContributedLibrary> getInstalled() {
List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList());
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
if (installedReleases.isEmpty()) { if (installedReleases.isEmpty()) {
return null; return Optional.empty();
} }
return installedReleases.get(0); return Optional.of(installedReleases.get(0));
} }
public ContributedLibrary getLatest() { public ContributedLibrary getLatest() {

View File

@ -36,6 +36,7 @@ import java.awt.Component;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.swing.JComboBox; import javax.swing.JComboBox;
@ -85,7 +86,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
setEnabled(true); setEnabled(true);
final ContributedLibrary installed = editorValue.getInstalled(); final Optional<ContributedLibrary> mayInstalled = editorValue.getInstalled();
List<ContributedLibrary> releases = editorValue.getReleases().stream() List<ContributedLibrary> releases = editorValue.getReleases().stream()
.filter(new OnlyUpstreamReleasePredicate()) .filter(new OnlyUpstreamReleasePredicate())
@ -97,7 +98,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
.filter(new InstalledPredicate()).filter(new BuiltInPredicate()) .filter(new InstalledPredicate()).filter(new BuiltInPredicate())
.collect(Collectors.toList()); .collect(Collectors.toList());
if (installed != null && !installedBuiltIn.contains(installed)) { if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) {
uninstalledReleases.addAll(installedBuiltIn); uninstalledReleases.addAll(installedBuiltIn);
} }
@ -111,8 +112,8 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>(); final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>();
uninstalledReleases.stream().forEach(input -> { uninstalledReleases.stream().forEach(input -> {
if (installed == null if (!mayInstalled.isPresent()
|| VersionComparator.greaterThan(installed, input)) { || VersionComparator.greaterThan(mayInstalled.get(), input)) {
uninstalledPreviousReleases.add(input); uninstalledPreviousReleases.add(input);
} else { } else {
uninstalledNewerReleases.add(input); uninstalledNewerReleases.add(input);
@ -122,18 +123,18 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem);
editorCell.downgradeChooser editorCell.downgradeChooser
.setVisible(installed != null .setVisible(mayInstalled.isPresent()
&& (!uninstalledPreviousReleases.isEmpty() && (!uninstalledPreviousReleases.isEmpty()
|| uninstalledNewerReleases.size() > 1)); || uninstalledNewerReleases.size() > 1));
editorCell.downgradeButton editorCell.downgradeButton
.setVisible(installed != null .setVisible(mayInstalled.isPresent()
&& (!uninstalledPreviousReleases.isEmpty() && (!uninstalledPreviousReleases.isEmpty()
|| uninstalledNewerReleases.size() > 1)); || uninstalledNewerReleases.size() > 1));
editorCell.versionToInstallChooser.removeAllItems(); editorCell.versionToInstallChooser.removeAllItems();
uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem);
editorCell.versionToInstallChooser editorCell.versionToInstallChooser
.setVisible(installed == null && uninstalledReleases.size() > 1); .setVisible(!mayInstalled.isPresent() && uninstalledReleases.size() > 1);
editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3
return editorCell; return editorCell;
@ -153,7 +154,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
} }
protected void onInstall(ContributedLibrary selected, protected void onInstall(ContributedLibrary selected,
ContributedLibrary installed) { Optional<ContributedLibrary> mayInstalled) {
// Empty // Empty
} }

View File

@ -7,6 +7,7 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Insets; import java.awt.Insets;
import java.util.Optional;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
@ -116,16 +117,16 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
return; return;
ContributedLibrary selected = releases.getSelected(); ContributedLibrary selected = releases.getSelected();
ContributedLibrary installed = releases.getInstalled(); Optional<ContributedLibrary> mayInstalled = releases.getInstalled();
boolean installable, upgradable; boolean installable, upgradable;
if (installed == null) { if (!mayInstalled.isPresent()) {
installable = true; installable = true;
upgradable = false; upgradable = false;
} else { } else {
installable = false; installable = false;
upgradable = new DownloadableContributionVersionComparator() upgradable = new DownloadableContributionVersionComparator()
.compare(selected, installed) > 0; .compare(selected, mayInstalled.get()) > 0;
} }
if (installable) { if (installable) {
installButton.setText(tr("Install")); installButton.setText(tr("Install"));
@ -151,7 +152,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
// Library name... // Library name...
desc += format("<b>{0}</b>", name); desc += format("<b>{0}</b>", name);
if (installed != null && installed.isReadOnly()) { if (mayInstalled.isPresent() && mayInstalled.get().isReadOnly()) {
desc += " Built-In "; desc += " Built-In ";
} }
@ -162,8 +163,8 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
} }
// ...version. // ...version.
if (installed != null) { if (mayInstalled.isPresent()) {
String installedVer = installed.getParsedVersion(); String installedVer = mayInstalled.get().getParsedVersion();
if (installedVer == null) { if (installedVer == null) {
desc += " " + tr("Version unknown"); desc += " " + tr("Version unknown");
} else { } else {
@ -172,7 +173,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
} }
desc += "</font>"; desc += "</font>";
if (installed != null) { if (mayInstalled.isPresent()) {
desc += " <strong><font color=\"#00979D\">INSTALLED</font></strong>"; desc += " <strong><font color=\"#00979D\">INSTALLED</font></strong>";
} }

View File

@ -38,6 +38,7 @@ import java.awt.event.ActionListener;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
import javax.swing.Box; import javax.swing.Box;
@ -80,11 +81,11 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
protected InstallerTableCell createCellEditor() { protected InstallerTableCell createCellEditor() {
return new ContributedLibraryTableCellEditor() { return new ContributedLibraryTableCellEditor() {
@Override @Override
protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) { protected void onInstall(ContributedLibrary selectedLibrary, Optional<ContributedLibrary> mayInstalledLibrary) {
if (selectedLibrary.isReadOnly()) { if (selectedLibrary.isReadOnly() && mayInstalledLibrary.isPresent()) {
onRemovePressed(installedLibrary); onRemovePressed(mayInstalledLibrary.get());
} else { } else {
onInstallPressed(selectedLibrary, installedLibrary); onInstallPressed(selectedLibrary, mayInstalledLibrary);
} }
} }
@ -212,12 +213,12 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
installerThread.start(); installerThread.start();
} }
public void onInstallPressed(final ContributedLibrary lib, final ContributedLibrary replaced) { public void onInstallPressed(final ContributedLibrary lib, final Optional<ContributedLibrary> mayReplaced) {
clearErrorMessage(); clearErrorMessage();
installerThread = new Thread(() -> { installerThread = new Thread(() -> {
try { try {
setProgressVisible(true, tr("Installing...")); setProgressVisible(true, tr("Installing..."));
installer.install(lib, replaced, this::setProgress); installer.install(lib, mayReplaced, this::setProgress);
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
//getContribModel().updateLibrary(lib); //getContribModel().updateLibrary(lib);
} catch (Exception e) { } catch (Exception e) {

View File

@ -358,11 +358,11 @@ public class Base {
System.exit(1); System.exit(1);
} }
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]); Optional<ContributedLibrary> mayInstalled = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
if (selected.isReadOnly()) { if (selected.isReadOnly() && mayInstalled.isPresent()) {
libraryInstaller.remove(installed, progressListener); libraryInstaller.remove(mayInstalled.get(), progressListener);
} else { } else {
libraryInstaller.install(selected, installed, progressListener); libraryInstaller.install(selected, mayInstalled, progressListener);
} }
} }

View File

@ -91,14 +91,14 @@ public abstract class LibrariesIndex {
return types; return types;
} }
public ContributedLibrary getInstalled(String name) { public Optional<ContributedLibrary> getInstalled(String name) {
List<ContributedLibrary> installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList()); List<ContributedLibrary> installedReleases = find(name).stream().filter(new InstalledPredicate()).collect(Collectors.toList());
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
if (installedReleases.isEmpty()) { if (installedReleases.isEmpty()) {
return null; return Optional.empty();
} }
return installedReleases.get(0); return Optional.of(installedReleases.get(0));
} }
} }

View File

@ -43,6 +43,7 @@ import processing.app.helpers.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Optional;
import static processing.app.I18n.tr; import static processing.app.I18n.tr;
@ -82,7 +83,7 @@ public class LibraryInstaller {
rescanLibraryIndex(progress, progressListener); rescanLibraryIndex(progress, progressListener);
} }
public synchronized void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception { public synchronized void install(ContributedLibrary lib, Optional<ContributedLibrary> mayReplacedLib, ProgressListener progressListener) throws Exception {
if (lib.isInstalled()) { if (lib.isInstalled()) {
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion())); System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
return; return;
@ -119,7 +120,9 @@ public class LibraryInstaller {
// Step 3: Remove replaced library and move installed one to the correct location // Step 3: Remove replaced library and move installed one to the correct location
// TODO: Fix progress bar... // TODO: Fix progress bar...
remove(replacedLib, progressListener); if (mayReplacedLib.isPresent()) {
remove(mayReplacedLib.get(), progressListener);
}
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_")); File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
tmpFolder.renameTo(destFolder); tmpFolder.renameTo(destFolder);
progress.stepDone(); progress.stepDone();
@ -129,7 +132,7 @@ public class LibraryInstaller {
} }
public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException { public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
if (lib == null || lib.isReadOnly()) { if (lib.isReadOnly()) {
return; return;
} }