mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-13 10:29:35 +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:
parent
212825eb55
commit
482b905a62
@ -37,6 +37,7 @@ import cc.arduino.contributions.ui.FilteredAbstractTableModel;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ContributedLibraryReleases {
|
||||
@ -76,15 +77,15 @@ public class ContributedLibraryReleases {
|
||||
selected = getLatest();
|
||||
}
|
||||
|
||||
public ContributedLibrary getInstalled() {
|
||||
public Optional<ContributedLibrary> getInstalled() {
|
||||
List<ContributedLibrary> installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList());
|
||||
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
|
||||
|
||||
if (installedReleases.isEmpty()) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return installedReleases.get(0);
|
||||
return Optional.of(installedReleases.get(0));
|
||||
}
|
||||
|
||||
public ContributedLibrary getLatest() {
|
||||
|
@ -36,6 +36,7 @@ import java.awt.Component;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.swing.JComboBox;
|
||||
@ -85,7 +86,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
|
||||
|
||||
setEnabled(true);
|
||||
|
||||
final ContributedLibrary installed = editorValue.getInstalled();
|
||||
final Optional<ContributedLibrary> mayInstalled = editorValue.getInstalled();
|
||||
|
||||
List<ContributedLibrary> releases = editorValue.getReleases().stream()
|
||||
.filter(new OnlyUpstreamReleasePredicate())
|
||||
@ -97,7 +98,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
|
||||
.filter(new InstalledPredicate()).filter(new BuiltInPredicate())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (installed != null && !installedBuiltIn.contains(installed)) {
|
||||
if (mayInstalled.isPresent() && !installedBuiltIn.contains(mayInstalled.get())) {
|
||||
uninstalledReleases.addAll(installedBuiltIn);
|
||||
}
|
||||
|
||||
@ -111,8 +112,8 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
|
||||
final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>();
|
||||
|
||||
uninstalledReleases.stream().forEach(input -> {
|
||||
if (installed == null
|
||||
|| VersionComparator.greaterThan(installed, input)) {
|
||||
if (!mayInstalled.isPresent()
|
||||
|| VersionComparator.greaterThan(mayInstalled.get(), input)) {
|
||||
uninstalledPreviousReleases.add(input);
|
||||
} else {
|
||||
uninstalledNewerReleases.add(input);
|
||||
@ -122,18 +123,18 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
|
||||
uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem);
|
||||
|
||||
editorCell.downgradeChooser
|
||||
.setVisible(installed != null
|
||||
.setVisible(mayInstalled.isPresent()
|
||||
&& (!uninstalledPreviousReleases.isEmpty()
|
||||
|| uninstalledNewerReleases.size() > 1));
|
||||
editorCell.downgradeButton
|
||||
.setVisible(installed != null
|
||||
.setVisible(mayInstalled.isPresent()
|
||||
&& (!uninstalledPreviousReleases.isEmpty()
|
||||
|| uninstalledNewerReleases.size() > 1));
|
||||
|
||||
editorCell.versionToInstallChooser.removeAllItems();
|
||||
uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem);
|
||||
editorCell.versionToInstallChooser
|
||||
.setVisible(installed == null && uninstalledReleases.size() > 1);
|
||||
.setVisible(!mayInstalled.isPresent() && uninstalledReleases.size() > 1);
|
||||
|
||||
editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3
|
||||
return editorCell;
|
||||
@ -153,7 +154,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell {
|
||||
}
|
||||
|
||||
protected void onInstall(ContributedLibrary selected,
|
||||
ContributedLibrary installed) {
|
||||
Optional<ContributedLibrary> mayInstalled) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Insets;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
@ -116,16 +117,16 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
|
||||
return;
|
||||
|
||||
ContributedLibrary selected = releases.getSelected();
|
||||
ContributedLibrary installed = releases.getInstalled();
|
||||
Optional<ContributedLibrary> mayInstalled = releases.getInstalled();
|
||||
|
||||
boolean installable, upgradable;
|
||||
if (installed == null) {
|
||||
if (!mayInstalled.isPresent()) {
|
||||
installable = true;
|
||||
upgradable = false;
|
||||
} else {
|
||||
installable = false;
|
||||
upgradable = new DownloadableContributionVersionComparator()
|
||||
.compare(selected, installed) > 0;
|
||||
.compare(selected, mayInstalled.get()) > 0;
|
||||
}
|
||||
if (installable) {
|
||||
installButton.setText(tr("Install"));
|
||||
@ -151,7 +152,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
|
||||
|
||||
// Library name...
|
||||
desc += format("<b>{0}</b>", name);
|
||||
if (installed != null && installed.isReadOnly()) {
|
||||
if (mayInstalled.isPresent() && mayInstalled.get().isReadOnly()) {
|
||||
desc += " Built-In ";
|
||||
}
|
||||
|
||||
@ -162,8 +163,8 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
|
||||
}
|
||||
|
||||
// ...version.
|
||||
if (installed != null) {
|
||||
String installedVer = installed.getParsedVersion();
|
||||
if (mayInstalled.isPresent()) {
|
||||
String installedVer = mayInstalled.get().getParsedVersion();
|
||||
if (installedVer == null) {
|
||||
desc += " " + tr("Version unknown");
|
||||
} else {
|
||||
@ -172,7 +173,7 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
|
||||
}
|
||||
desc += "</font>";
|
||||
|
||||
if (installed != null) {
|
||||
if (mayInstalled.isPresent()) {
|
||||
desc += " <strong><font color=\"#00979D\">INSTALLED</font></strong>";
|
||||
}
|
||||
|
||||
|
@ -38,6 +38,7 @@ import java.awt.event.ActionListener;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import javax.swing.Box;
|
||||
@ -80,11 +81,11 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
|
||||
protected InstallerTableCell createCellEditor() {
|
||||
return new ContributedLibraryTableCellEditor() {
|
||||
@Override
|
||||
protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) {
|
||||
if (selectedLibrary.isReadOnly()) {
|
||||
onRemovePressed(installedLibrary);
|
||||
protected void onInstall(ContributedLibrary selectedLibrary, Optional<ContributedLibrary> mayInstalledLibrary) {
|
||||
if (selectedLibrary.isReadOnly() && mayInstalledLibrary.isPresent()) {
|
||||
onRemovePressed(mayInstalledLibrary.get());
|
||||
} else {
|
||||
onInstallPressed(selectedLibrary, installedLibrary);
|
||||
onInstallPressed(selectedLibrary, mayInstalledLibrary);
|
||||
}
|
||||
}
|
||||
|
||||
@ -212,12 +213,12 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
|
||||
installerThread.start();
|
||||
}
|
||||
|
||||
public void onInstallPressed(final ContributedLibrary lib, final ContributedLibrary replaced) {
|
||||
public void onInstallPressed(final ContributedLibrary lib, final Optional<ContributedLibrary> mayReplaced) {
|
||||
clearErrorMessage();
|
||||
installerThread = new Thread(() -> {
|
||||
try {
|
||||
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
|
||||
//getContribModel().updateLibrary(lib);
|
||||
} catch (Exception e) {
|
||||
|
@ -358,11 +358,11 @@ public class Base {
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
|
||||
if (selected.isReadOnly()) {
|
||||
libraryInstaller.remove(installed, progressListener);
|
||||
Optional<ContributedLibrary> mayInstalled = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
|
||||
if (selected.isReadOnly() && mayInstalled.isPresent()) {
|
||||
libraryInstaller.remove(mayInstalled.get(), progressListener);
|
||||
} else {
|
||||
libraryInstaller.install(selected, installed, progressListener);
|
||||
libraryInstaller.install(selected, mayInstalled, progressListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,14 +91,14 @@ public abstract class LibrariesIndex {
|
||||
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());
|
||||
Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator());
|
||||
|
||||
if (installedReleases.isEmpty()) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return installedReleases.get(0);
|
||||
return Optional.of(installedReleases.get(0));
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ import processing.app.helpers.FileUtils;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Optional;
|
||||
|
||||
import static processing.app.I18n.tr;
|
||||
|
||||
@ -82,7 +83,7 @@ public class LibraryInstaller {
|
||||
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()) {
|
||||
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
|
||||
return;
|
||||
@ -119,7 +120,9 @@ public class LibraryInstaller {
|
||||
|
||||
// Step 3: Remove replaced library and move installed one to the correct location
|
||||
// TODO: Fix progress bar...
|
||||
remove(replacedLib, progressListener);
|
||||
if (mayReplacedLib.isPresent()) {
|
||||
remove(mayReplacedLib.get(), progressListener);
|
||||
}
|
||||
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
|
||||
tmpFolder.renameTo(destFolder);
|
||||
progress.stepDone();
|
||||
@ -129,7 +132,7 @@ public class LibraryInstaller {
|
||||
}
|
||||
|
||||
public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
|
||||
if (lib == null || lib.isReadOnly()) {
|
||||
if (lib.isReadOnly()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user