refactored searchers into a class

This commit is contained in:
Capelier-Marla 2022-12-14 14:38:30 +01:00
parent e1d40829b4
commit 2834168b14
2 changed files with 80 additions and 71 deletions

View File

@ -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<SearchedPackage> 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<List<SearchedPackage>> archPackages = arch.searchPackage(packet);
Future<List<SearchedPackage>> fedoraPackages = fedora.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> archResult = new ArrayList<>();
List<SearchedPackage> 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<SearchedPackage> searchForArch(String packet) {
// init distribution to search in it
Distribution arch = new ArchDistribution();
// search for the package in the distribution
Future<List<SearchedPackage>> packages = arch.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> 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<SearchedPackage> searchForFedora(String packet) {
// init distribution to search in it
Distribution fedora = new ArchDistribution();
// search for the package in the distribution
Future<List<SearchedPackage>> packages = fedora.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> 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<SearchedPackage> 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");

View File

@ -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<SearchedPackage> 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<List<SearchedPackage>> archPackages = arch.searchPackage(packet);
Future<List<SearchedPackage>> fedoraPackages = fedora.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> archResult = new ArrayList<>();
List<SearchedPackage> 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<SearchedPackage> searchForArch(String packet) {
// init distribution to search in it
Distribution arch = new ArchDistribution();
// search for the package in the distribution
Future<List<SearchedPackage>> packages = arch.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> 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<SearchedPackage> searchForFedora(String packet) {
// init distribution to search in it
Distribution fedora = new ArchDistribution();
// search for the package in the distribution
Future<List<SearchedPackage>> packages = fedora.searchPackage(packet);
// init the list of packages that will be returned
List<SearchedPackage> 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;
}
}