1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +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.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.filters.InstalledPredicate; 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 com.google.common.collect.Collections2;
import java.util.*; import java.util.*;
@ -41,12 +41,7 @@ public abstract class LibrariesIndex {
public abstract List<ContributedLibrary> getLibraries(); public abstract List<ContributedLibrary> getLibraries();
public List<ContributedLibrary> find(final String name) { public List<ContributedLibrary> find(final String name) {
return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new Predicate<ContributedLibrary>() { return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new LibraryWithNamePredicate(name)));
@Override
public boolean apply(ContributedLibrary contributedLibrary) {
return name.equals(contributedLibrary.getName());
}
}));
} }
public ContributedLibrary find(String name, String version) { 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; package cc.arduino.contributions.packages;
import cc.arduino.contributions.GPGDetachedSignatureVerifier; import cc.arduino.contributions.GPGDetachedSignatureVerifier;
import cc.arduino.filters.FileExecutablePredicate;
import cc.arduino.utils.ArchiveExtractor; import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress; import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress; import cc.arduino.utils.Progress;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.Executor; import org.apache.commons.exec.Executor;
@ -158,12 +158,7 @@ public class ContributionInstaller {
} }
private void executePostInstallScriptIfAny(File folder) throws IOException { private void executePostInstallScriptIfAny(File folder) throws IOException {
Collection<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new Predicate<File>() { Collection<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new FileExecutablePredicate());
@Override
public boolean apply(File file) {
return file.isFile() && file.exists() && file.canRead() && file.canExecute();
}
});
if (postInstallScripts.isEmpty()) { if (postInstallScripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs()); String[] subfolders = folder.list(new OnlyDirs());

View File

@ -29,9 +29,10 @@
package cc.arduino.contributions.packages; package cc.arduino.contributions.packages;
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
import cc.arduino.contributions.filters.DownloadableContributionWithVersionPredicate;
import cc.arduino.contributions.filters.InstalledPredicate; import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.packages.filters.PlatformArchitecturePredicate;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -59,12 +60,7 @@ public abstract class ContributionsIndex {
if (aPackage == null) { if (aPackage == null) {
return null; return null;
} }
Collection<ContributedPlatform> platforms = Collections2.filter(aPackage.getPlatforms(), new Predicate<ContributedPlatform>() { Collection<ContributedPlatform> platforms = Collections2.filter(aPackage.getPlatforms(), new PlatformArchitecturePredicate(platformArch));
@Override
public boolean apply(ContributedPlatform contributedPlatform) {
return platformArch.equals(contributedPlatform.getArchitecture());
}
});
return Lists.newLinkedList(platforms); return Lists.newLinkedList(platforms);
} }
@ -79,12 +75,7 @@ public abstract class ContributionsIndex {
return null; return null;
} }
Collection<ContributedPlatform> platforms = Collections2.filter(platformsByName, new Predicate<ContributedPlatform>() { Collection<ContributedPlatform> platforms = Collections2.filter(platformsByName, new DownloadableContributionWithVersionPredicate(platformVersion));
@Override
public boolean apply(ContributedPlatform contributedPlatform) {
return platformVersion.equals(contributedPlatform.getParsedVersion());
}
});
if (platforms.isEmpty()) { if (platforms.isEmpty()) {
return null; 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();
}
}