From 134d5554abcc5ac47ba1927385faff66b3f25ffa Mon Sep 17 00:00:00 2001 From: Capelier-Marla Date: Thu, 15 Dec 2022 16:29:27 +0100 Subject: [PATCH] big refactor to use enum and be upgradable --- .../fr/packageviewer/DistributionEnum.java | 63 ++++++++ src/main/java/fr/packageviewer/Main.java | 61 +------- src/main/java/fr/packageviewer/Searcher.java | 136 ++++++------------ 3 files changed, 114 insertions(+), 146 deletions(-) create mode 100644 src/main/java/fr/packageviewer/DistributionEnum.java diff --git a/src/main/java/fr/packageviewer/DistributionEnum.java b/src/main/java/fr/packageviewer/DistributionEnum.java new file mode 100644 index 0000000..6e4ac7b --- /dev/null +++ b/src/main/java/fr/packageviewer/DistributionEnum.java @@ -0,0 +1,63 @@ +package fr.packageviewer; + +import java.util.ArrayList; +import java.util.List; + +import fr.packageviewer.distribution.ArchDistribution; +import fr.packageviewer.distribution.Distribution; +import fr.packageviewer.distribution.FedoraDistribution; + +/** + * Enum containing contructors for each distribution to get them by their name + * @author Capelier-Marla + */ +public enum DistributionEnum { + ARCH("arch", new ArchDistribution()), + FEDORA("fedora", new FedoraDistribution()), + ; + + private String name; + private Distribution distributionConstructor; + + /** + * Contructor for enums + * @param name + * @param distributionConstructor + * @author Capelier-Marla + */ + DistributionEnum(String name, Distribution distributionConstructor) { + this.name = name; + this.distributionConstructor = distributionConstructor; + } + + /** + * Get the distribution instance for the distribution requested in String + * @param name name of the distribution requested + * @return the instance of the distribution requested + * @author Capelier-Marla + */ + public static Distribution getDistributionContructorByName(String name) { + // loop for all ditributions stored in enum + for(var distrib : values()) { + // return the instance if it's the same as enum name + if(distrib.name.equals(name)) { + return distrib.distributionConstructor; + } + } + return null; + } + + /** + * Get all distribution instances available in this enum + * @return the set of distribution instances + */ + public static List getAllDistributionsInstances() { + // create the set that will be returned + List result = new ArrayList<>(); + // add all the distribution instances in the set + for(var ditrib : values()) { + result.add(ditrib.distributionConstructor); + } + return result; + } +} diff --git a/src/main/java/fr/packageviewer/Main.java b/src/main/java/fr/packageviewer/Main.java index b328b9d..954d017 100644 --- a/src/main/java/fr/packageviewer/Main.java +++ b/src/main/java/fr/packageviewer/Main.java @@ -1,75 +1,28 @@ package fr.packageviewer; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Scanner; import fr.packageviewer.pack.SearchedPackage; public class Main { - - /** - * Check if the given distribution is supported - * @param distribution distribution name - * @return the distribution wanted if found, or null if there is none, or stop the program if it is not supported - * @author Capelier-Marla - */ - private static String processDistribution(String distribution) { - // check if the user asked a distribution - if(distribution == null || distribution.length() == 0) { - return null; - } - // create a map with the distribution names - Map distributionMap = new HashMap(); - distributionMap.put("ARCH", "archlinux"); - distributionMap.put("ARCHLINUX", "archlinux"); - distributionMap.put("FEDORA", "fedora"); - distribution = distribution.toUpperCase(); - - // check if we support the distribtion - if(distributionMap.containsKey(distribution)) { - // give the distribution name - return distributionMap.get(distribution); - } else { - // stop the program as the user want a non-supported distribution - System.out.println("Cette ditribution n'a pas été trouvée"); - System.exit(0); - return null; - } - } - - public static void main(String[] args) { // send the command line arguments to the parser ArgParse.parseArguments(args); String packet = ArgParse.getPacket(); String distribution = ArgParse.getDistribution(); - distribution = processDistribution(distribution); - // the list of packages we will show later - List packets = new ArrayList<>(); - - // distribution is null when no distribution is specified - // else we get the list of packages from the distribution + // we create an object to search the packages in the distribution + Searcher searcher; if(distribution == null) { - packets = Searcher.searchForAll(packet); + searcher = new Searcher(); } else { - switch (distribution) { - case "archlinux": - packets = Searcher.searchForArch(packet); - break; - case "fedora": - packets = Searcher.searchForFedora(packet); - break; - default: - System.out.println("Error: Unknown"); - System.exit(0); - break; - } + searcher = new Searcher(distribution); } + // we get the packages list + List packets = searcher.searchPackages(packet); + // list all packages in reverse order for (int i = packets.size(); i-- > 0; ) { SearchedPackage searchedPacket = packets.get(i); diff --git a/src/main/java/fr/packageviewer/Searcher.java b/src/main/java/fr/packageviewer/Searcher.java index 6264163..08bcd41 100644 --- a/src/main/java/fr/packageviewer/Searcher.java +++ b/src/main/java/fr/packageviewer/Searcher.java @@ -1,114 +1,66 @@ package fr.packageviewer; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import fr.packageviewer.distribution.ArchDistribution; import fr.packageviewer.distribution.Distribution; -import fr.packageviewer.distribution.FedoraDistribution; import fr.packageviewer.pack.SearchedPackage; -import fr.packageviewer.pack.Package; public class Searcher { - public static List searchForAll(String packet) { - // init distribution to search in it - Distribution arch = new ArchDistribution(); - Distribution fedora = new FedoraDistribution(); - // search for the package in the distribution - Future> archPackages = arch.searchPackage(packet); - Future> fedoraPackages = fedora.searchPackage(packet); - // init the list of packages that will be returned - List archResult = new ArrayList<>(); - List fedoraResult = new ArrayList<>(); - // try to get the searched packages to return it after - try { - archResult = archPackages.get(); - fedoraResult = fedoraPackages.get(); - } catch (Exception e) { - /* TODO: handle exception */ - } - archResult.addAll(fedoraResult); - return archResult; + + private String distributionName = null; + + public Searcher() {} + + public Searcher(String distributionName) { + this.distributionName = distributionName; + } + + public void setDistribution(String distribution) { + this.distributionName = distribution; } /** - * Search for the specified package in Arch repositories - * @param packet the package to search - * @return The list of packages that match the name + * Get the list of all packages in the distribution setted before + * @param packageName the name of the package wanted + * @return the list of all packages found * @author Capelier-Marla */ - public static List searchForArch(String packet) { - // init distribution to search in it - Distribution arch = new ArchDistribution(); - // search for the package in the distribution - Future> packages = arch.searchPackage(packet); - // init the list of packages that will be returned - List result = new ArrayList<>(); - // try to get the searched packages to return it after - try { - result = packages.get(); - } catch (Exception e) { - /* TODO: handle exception */ - } - return result; - } + public List searchPackages(String packageName) { - /** - * Search for the specified package in Fedora repositories - * @param packet the package to search - * @return The list of packages that match the name - * @author Capelier-Marla - */ - public static List searchForFedora(String packet) { - // init distribution to search in it - Distribution fedora = new FedoraDistribution(); - // search for the package in the distribution - Future> packages = fedora.searchPackage(packet); - // init the list of packages that will be returned - List result = new ArrayList<>(); - // try to get the searched packages to return it after - try { - result = packages.get(); - } catch (Exception e) { - /* TODO: handle exception */ + // we add all instanced constructors in a list, only one if defined at creation of the object + List distributions; + if(distributionName == null) { + distributions = DistributionEnum.getAllDistributionsInstances(); + } else { + distributions = Collections.singletonList(DistributionEnum.getDistributionContructorByName(distributionName)); + if(distributions.get(0) == null) { + System.out.println("Distribution non trouvée"); + System.exit(0); + } } - return result; - } + // this is the list we will return containing all packages + List allPackages = new ArrayList<>(); + // this contains all future list of packages to get them after + List>> listFuturePackagesList = new ArrayList<>(); - /** - * Search for the specific package in the specific distribution, we need the searchedPackage to get informations about it like it name and distribution - * @param pSearchedPackage the package to search for - * @return a complete package with its dependencies - * @author Capelier-Marla - */ - public static Package getPackage(SearchedPackage pSearchedPackage) { - // we get the name of the distribution of the package - String distName = pSearchedPackage.getDistribution(); - // we create a distribution object - Distribution distribution; - switch (distName) { - case "archlinux": - distribution = new ArchDistribution(); - break; - case "fedora": - distribution = new FedoraDistribution(); - break; - default: - System.out.println("Error: Unknown"); - System.exit(1); - return null; + // we add all future packages in a list + for (Distribution distribution : distributions) { + listFuturePackagesList.add(distribution.searchPackage(packageName)); } - // create the futue package we'll get from searching - Future packet = distribution.getPackageTree(pSearchedPackage.getName(), 4); - // object containing the package we're looking for - Package result; - try { - result = packet.get(); - } catch (Exception e) { - return null; + + // we get all packages waiting for them to be received + for(Future> futurePackageList : listFuturePackagesList ) { + try { + List tempList = futurePackageList.get(); + allPackages.addAll(tempList); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } } - // return the package - return result; + return allPackages; } }