From 2834168b14eaa077e9ed3e41796f7db553044d73 Mon Sep 17 00:00:00 2001 From: Capelier-Marla Date: Wed, 14 Dec 2022 14:38:30 +0100 Subject: [PATCH] refactored searchers into a class --- src/main/java/fr/packageviewer/Main.java | 75 ++----------------- src/main/java/fr/packageviewer/Searcher.java | 76 ++++++++++++++++++++ 2 files changed, 80 insertions(+), 71 deletions(-) create mode 100644 src/main/java/fr/packageviewer/Searcher.java diff --git a/src/main/java/fr/packageviewer/Main.java b/src/main/java/fr/packageviewer/Main.java index 8e1b5d3..f870f4a 100644 --- a/src/main/java/fr/packageviewer/Main.java +++ b/src/main/java/fr/packageviewer/Main.java @@ -4,10 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.Future; -import fr.packageviewer.distribution.ArchDistribution; -import fr.packageviewer.distribution.Distribution; import fr.packageviewer.pack.SearchedPackage; public class Main { @@ -43,71 +40,6 @@ public class Main { } - private static List searchForAll(String packet) { - // init distribution to search in it - Distribution arch = new ArchDistribution(); - Distribution fedora = new ArchDistribution(); - // 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; - } - - /** - * Search for the specified package in Arch repositories - * @param packet the package to search - * @return The list of packages that match the name - * @author Capelier-Marla - */ - private 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; - } - - /** - * 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 - */ - private static List searchForFedora(String packet) { - // init distribution to search in it - Distribution fedora = new ArchDistribution(); - // 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 */ - } - return result; - } - public static void main(String[] args) { // send the command line arguments to the parser ArgParse.parseArguments(args); @@ -122,15 +54,16 @@ public class Main { List packets = new ArrayList<>(); // distribution is null when no distribution is specified + // else we get the list of packages from the distribution if(distribution == null) { - packets = searchForAll(packet); + packets = Searcher.searchForAll(packet); } else { switch (distribution) { case "archlinux": - packets = searchForArch(packet); + packets = Searcher.searchForArch(packet); break; case "fedora": - packets = searchForFedora(packet); + packets = Searcher.searchForFedora(packet); break; default: System.out.println("Error: Unknown"); diff --git a/src/main/java/fr/packageviewer/Searcher.java b/src/main/java/fr/packageviewer/Searcher.java new file mode 100644 index 0000000..f28b3ae --- /dev/null +++ b/src/main/java/fr/packageviewer/Searcher.java @@ -0,0 +1,76 @@ +package fr.packageviewer; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Future; + +import fr.packageviewer.distribution.ArchDistribution; +import fr.packageviewer.distribution.Distribution; +import fr.packageviewer.pack.SearchedPackage; + +public class Searcher { + public static List searchForAll(String packet) { + // init distribution to search in it + Distribution arch = new ArchDistribution(); + Distribution fedora = new ArchDistribution(); + // 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; + } + + /** + * Search for the specified package in Arch repositories + * @param packet the package to search + * @return The list of packages that match the name + * @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; + } + + /** + * 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 ArchDistribution(); + // 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 */ + } + return result; + } +}