From de95ef6c41e9e8cfb1406c5261ede36a586b9002 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Tue, 5 May 2015 11:56:56 +0200 Subject: [PATCH] Predicates: extracting anonymous inner classes for better reuse --- ...dableContributionWithVersionPredicate.java | 18 ++++++++++++++++++ .../libraries/LibrariesIndex.java | 9 ++------- .../filters/LibraryWithNamePredicate.java | 19 +++++++++++++++++++ .../packages/ContributionInstaller.java | 9 ++------- .../packages/ContributionsIndex.java | 17 ++++------------- .../PlatformArchitecturePredicate.java | 19 +++++++++++++++++++ .../filters/FileExecutablePredicate.java | 14 ++++++++++++++ 7 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 arduino-core/src/cc/arduino/contributions/filters/DownloadableContributionWithVersionPredicate.java create mode 100644 arduino-core/src/cc/arduino/contributions/libraries/filters/LibraryWithNamePredicate.java create mode 100644 arduino-core/src/cc/arduino/contributions/packages/filters/PlatformArchitecturePredicate.java create mode 100644 arduino-core/src/cc/arduino/filters/FileExecutablePredicate.java diff --git a/arduino-core/src/cc/arduino/contributions/filters/DownloadableContributionWithVersionPredicate.java b/arduino-core/src/cc/arduino/contributions/filters/DownloadableContributionWithVersionPredicate.java new file mode 100644 index 000000000..499fc85e4 --- /dev/null +++ b/arduino-core/src/cc/arduino/contributions/filters/DownloadableContributionWithVersionPredicate.java @@ -0,0 +1,18 @@ +package cc.arduino.contributions.filters; + +import cc.arduino.contributions.packages.DownloadableContribution; +import com.google.common.base.Predicate; + +public class DownloadableContributionWithVersionPredicate implements Predicate { + private final String version; + + public DownloadableContributionWithVersionPredicate(String version) { + this.version = version; + } + + @Override + public boolean apply(DownloadableContribution contribution) { + return version.equals(contribution.getParsedVersion()); + } + +} diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java index 483de6dd3..cb91caf35 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java @@ -31,7 +31,7 @@ package cc.arduino.contributions.libraries; import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; import cc.arduino.contributions.filters.InstalledPredicate; -import com.google.common.base.Predicate; +import cc.arduino.contributions.libraries.filters.LibraryWithNamePredicate; import com.google.common.collect.Collections2; import java.util.*; @@ -41,12 +41,7 @@ public abstract class LibrariesIndex { public abstract List getLibraries(); public List find(final String name) { - return new LinkedList(Collections2.filter(getLibraries(), new Predicate() { - @Override - public boolean apply(ContributedLibrary contributedLibrary) { - return name.equals(contributedLibrary.getName()); - } - })); + return new LinkedList(Collections2.filter(getLibraries(), new LibraryWithNamePredicate(name))); } public ContributedLibrary find(String name, String version) { diff --git a/arduino-core/src/cc/arduino/contributions/libraries/filters/LibraryWithNamePredicate.java b/arduino-core/src/cc/arduino/contributions/libraries/filters/LibraryWithNamePredicate.java new file mode 100644 index 000000000..9ed021ed8 --- /dev/null +++ b/arduino-core/src/cc/arduino/contributions/libraries/filters/LibraryWithNamePredicate.java @@ -0,0 +1,19 @@ +package cc.arduino.contributions.libraries.filters; + +import cc.arduino.contributions.libraries.ContributedLibrary; +import com.google.common.base.Predicate; + +public class LibraryWithNamePredicate implements Predicate { + + private final String name; + + public LibraryWithNamePredicate(String name) { + this.name = name; + } + + @Override + public boolean apply(ContributedLibrary contributedLibrary) { + return name.equals(contributedLibrary.getName()); + } + +} diff --git a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java index 4abd4801a..c083193ee 100644 --- a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java +++ b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java @@ -29,10 +29,10 @@ package cc.arduino.contributions.packages; import cc.arduino.contributions.GPGDetachedSignatureVerifier; +import cc.arduino.filters.FileExecutablePredicate; import cc.arduino.utils.ArchiveExtractor; import cc.arduino.utils.MultiStepProgress; import cc.arduino.utils.Progress; -import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.Executor; @@ -158,12 +158,7 @@ public class ContributionInstaller { } private void executePostInstallScriptIfAny(File folder) throws IOException { - Collection postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new Predicate() { - @Override - public boolean apply(File file) { - return file.isFile() && file.exists() && file.canRead() && file.canExecute(); - } - }); + Collection postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new FileExecutablePredicate()); if (postInstallScripts.isEmpty()) { String[] subfolders = folder.list(new OnlyDirs()); diff --git a/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java b/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java index ecb8fad64..1804e719f 100644 --- a/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java +++ b/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java @@ -29,9 +29,10 @@ package cc.arduino.contributions.packages; import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; +import cc.arduino.contributions.filters.DownloadableContributionWithVersionPredicate; import cc.arduino.contributions.filters.InstalledPredicate; +import cc.arduino.contributions.packages.filters.PlatformArchitecturePredicate; import com.google.common.base.Function; -import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -59,12 +60,7 @@ public abstract class ContributionsIndex { if (aPackage == null) { return null; } - Collection platforms = Collections2.filter(aPackage.getPlatforms(), new Predicate() { - @Override - public boolean apply(ContributedPlatform contributedPlatform) { - return platformArch.equals(contributedPlatform.getArchitecture()); - } - }); + Collection platforms = Collections2.filter(aPackage.getPlatforms(), new PlatformArchitecturePredicate(platformArch)); return Lists.newLinkedList(platforms); } @@ -79,12 +75,7 @@ public abstract class ContributionsIndex { return null; } - Collection platforms = Collections2.filter(platformsByName, new Predicate() { - @Override - public boolean apply(ContributedPlatform contributedPlatform) { - return platformVersion.equals(contributedPlatform.getParsedVersion()); - } - }); + Collection platforms = Collections2.filter(platformsByName, new DownloadableContributionWithVersionPredicate(platformVersion)); if (platforms.isEmpty()) { return null; } diff --git a/arduino-core/src/cc/arduino/contributions/packages/filters/PlatformArchitecturePredicate.java b/arduino-core/src/cc/arduino/contributions/packages/filters/PlatformArchitecturePredicate.java new file mode 100644 index 000000000..378ca0358 --- /dev/null +++ b/arduino-core/src/cc/arduino/contributions/packages/filters/PlatformArchitecturePredicate.java @@ -0,0 +1,19 @@ +package cc.arduino.contributions.packages.filters; + +import cc.arduino.contributions.packages.ContributedPlatform; +import com.google.common.base.Predicate; + +public class PlatformArchitecturePredicate implements Predicate { + + private final String platformArch; + + public PlatformArchitecturePredicate(String platformArch) { + this.platformArch = platformArch; + } + + @Override + public boolean apply(ContributedPlatform contributedPlatform) { + return platformArch.equals(contributedPlatform.getArchitecture()); + } + +} diff --git a/arduino-core/src/cc/arduino/filters/FileExecutablePredicate.java b/arduino-core/src/cc/arduino/filters/FileExecutablePredicate.java new file mode 100644 index 000000000..39e7f030c --- /dev/null +++ b/arduino-core/src/cc/arduino/filters/FileExecutablePredicate.java @@ -0,0 +1,14 @@ +package cc.arduino.filters; + +import com.google.common.base.Predicate; + +import java.io.File; + +public class FileExecutablePredicate implements Predicate { + + @Override + public boolean apply(File file) { + return file.isFile() && file.exists() && file.canRead() && file.canExecute(); + } + +}