1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Predicates: extracting anonymous inner classes for better reuse

This commit is contained in:
Federico Fissore 2015-05-05 11:56:56 +02:00
parent 9b58812d1d
commit de95ef6c41
7 changed files with 78 additions and 27 deletions

View File

@ -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<DownloadableContribution> {
private final String version;
public DownloadableContributionWithVersionPredicate(String version) {
this.version = version;
}
@Override
public boolean apply(DownloadableContribution contribution) {
return version.equals(contribution.getParsedVersion());
}
}

View File

@ -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<ContributedLibrary> getLibraries();
public List<ContributedLibrary> find(final String name) {
return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new Predicate<ContributedLibrary>() {
@Override
public boolean apply(ContributedLibrary contributedLibrary) {
return name.equals(contributedLibrary.getName());
}
}));
return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new LibraryWithNamePredicate(name)));
}
public ContributedLibrary find(String name, String version) {

View File

@ -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<ContributedLibrary> {
private final String name;
public LibraryWithNamePredicate(String name) {
this.name = name;
}
@Override
public boolean apply(ContributedLibrary contributedLibrary) {
return name.equals(contributedLibrary.getName());
}
}

View File

@ -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<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new Predicate<File>() {
@Override
public boolean apply(File file) {
return file.isFile() && file.exists() && file.canRead() && file.canExecute();
}
});
Collection<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new FileExecutablePredicate());
if (postInstallScripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs());

View File

@ -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<ContributedPlatform> platforms = Collections2.filter(aPackage.getPlatforms(), new Predicate<ContributedPlatform>() {
@Override
public boolean apply(ContributedPlatform contributedPlatform) {
return platformArch.equals(contributedPlatform.getArchitecture());
}
});
Collection<ContributedPlatform> platforms = Collections2.filter(aPackage.getPlatforms(), new PlatformArchitecturePredicate(platformArch));
return Lists.newLinkedList(platforms);
}
@ -79,12 +75,7 @@ public abstract class ContributionsIndex {
return null;
}
Collection<ContributedPlatform> platforms = Collections2.filter(platformsByName, new Predicate<ContributedPlatform>() {
@Override
public boolean apply(ContributedPlatform contributedPlatform) {
return platformVersion.equals(contributedPlatform.getParsedVersion());
}
});
Collection<ContributedPlatform> platforms = Collections2.filter(platformsByName, new DownloadableContributionWithVersionPredicate(platformVersion));
if (platforms.isEmpty()) {
return null;
}

View File

@ -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<ContributedPlatform> {
private final String platformArch;
public PlatformArchitecturePredicate(String platformArch) {
this.platformArch = platformArch;
}
@Override
public boolean apply(ContributedPlatform contributedPlatform) {
return platformArch.equals(contributedPlatform.getArchitecture());
}
}

View File

@ -0,0 +1,14 @@
package cc.arduino.filters;
import com.google.common.base.Predicate;
import java.io.File;
public class FileExecutablePredicate implements Predicate<File> {
@Override
public boolean apply(File file) {
return file.isFile() && file.exists() && file.canRead() && file.canExecute();
}
}