1 /* 2 * This file is part of Arduino. 3 * 4 * Copyright 2014 Arduino LLC (http://www.arduino.cc/) 5 * 6 * Arduino is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 * 20 * As a special exception, you may use this file as part of a free software 21 * library without restriction. Specifically, if other files instantiate 22 * templates or use macros or inline functions from this file, or you compile 23 * this file and link it with other files to produce an executable, this 24 * file does not by itself cause the resulting executable to be covered by 25 * the GNU General Public License. This exception does not however 26 * invalidate any other reasons why the executable file might be covered by 27 * the GNU General Public License. 28 */ 29 30 package cc.arduino.contributions.packages; 31 32 import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; 33 import cc.arduino.contributions.filters.DownloadableContributionWithVersionPredicate; 34 import cc.arduino.contributions.filters.InstalledPredicate; 35 import cc.arduino.contributions.packages.filters.PlatformArchitecturePredicate; 36 37 import java.util.ArrayList; 38 import java.util.Collection; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.stream.Collectors; 42 43 public abstract class ContributionsIndex { 44 getPackages()45 public abstract List<ContributedPackage> getPackages(); 46 findPackage(String packageName)47 public ContributedPackage findPackage(String packageName) { 48 for (ContributedPackage pack : getPackages()) { 49 if (pack.getName().equals(packageName)) 50 return pack; 51 } 52 return null; 53 } 54 findPlatforms(String packageName, final String platformArch)55 public List<ContributedPlatform> findPlatforms(String packageName, final String platformArch) { 56 if (packageName == null || platformArch == null) { 57 return null; 58 59 } 60 ContributedPackage aPackage = findPackage(packageName); 61 if (aPackage == null) { 62 return null; 63 } 64 return aPackage.getPlatforms().stream().filter(new PlatformArchitecturePredicate(platformArch)).collect(Collectors.toList()); 65 } 66 findPlatform(String packageName, final String platformArch, final String platformVersion)67 public ContributedPlatform findPlatform(String packageName, final String platformArch, final String platformVersion) { 68 if (platformVersion == null) { 69 return null; 70 71 } 72 73 Collection<ContributedPlatform> platformsByName = findPlatforms(packageName, platformArch); 74 if (platformsByName == null) { 75 return null; 76 } 77 78 Collection<ContributedPlatform> platforms = platformsByName.stream().filter(new DownloadableContributionWithVersionPredicate(platformVersion)).collect(Collectors.toList()); 79 if (platforms.isEmpty()) { 80 return null; 81 } 82 83 return platforms.iterator().next(); 84 } 85 getInstalledPlatforms()86 public List<ContributedPlatform> getInstalledPlatforms() { 87 return getPlatforms().stream().filter(new InstalledPredicate()).collect(Collectors.toList()); 88 } 89 getInstalledPlatform(String packageName, String platformArch)90 public ContributedPlatform getInstalledPlatform(String packageName, String platformArch) { 91 List<ContributedPlatform> platforms = findPlatforms(packageName, platformArch); 92 if (platforms == null) { 93 return null; 94 } 95 List<ContributedPlatform> installedPlatforms = platforms.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); 96 Collections.sort(installedPlatforms, new DownloadableContributionBuiltInAtTheBottomComparator()); 97 98 if (installedPlatforms.isEmpty()) { 99 return null; 100 } 101 102 return installedPlatforms.get(0); 103 } 104 getPlatforms()105 private List<ContributedPlatform> getPlatforms() { 106 return getPackages().stream().map(ContributedPackage::getPlatforms).flatMap(Collection::stream).collect(Collectors.toList()); 107 } 108 109 private final List<String> categories = new ArrayList<>(); 110 getCategories()111 public List<String> getCategories() { 112 return categories; 113 } 114 fillCategories()115 public void fillCategories() { 116 categories.clear(); 117 for (ContributedPackage pack : getPackages()) { 118 pack.getPlatforms().stream() 119 .filter(platform -> !categories.contains(platform.getCategory())) 120 .forEach(platform -> categories.add(platform.getCategory())); 121 } 122 } 123 getPackage(String packageName)124 public ContributedPackage getPackage(String packageName) { 125 for (ContributedPackage pack : getPackages()) { 126 if (pack.getName().equals(packageName)) { 127 return pack; 128 } 129 } 130 return null; 131 } 132 133 @Override toString()134 public String toString() { 135 String res = ""; 136 res += "Categories: "; 137 for (String c : getCategories()) 138 res += "'" + c + "' "; 139 res += "\n"; 140 for (ContributedPackage pack : getPackages()) 141 res += pack + "\n"; 142 return res; 143 } 144 } 145