big refactor to use enum and be upgradable
This commit is contained in:
parent
19517d5b6c
commit
134d5554ab
63
src/main/java/fr/packageviewer/DistributionEnum.java
Normal file
63
src/main/java/fr/packageviewer/DistributionEnum.java
Normal file
@ -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<Distribution> getAllDistributionsInstances() {
|
||||
// create the set that will be returned
|
||||
List<Distribution> result = new ArrayList<>();
|
||||
// add all the distribution instances in the set
|
||||
for(var ditrib : values()) {
|
||||
result.add(ditrib.distributionConstructor);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -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<String, String> distributionMap = new HashMap<String, String>();
|
||||
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<SearchedPackage> 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<SearchedPackage> packets = searcher.searchPackages(packet);
|
||||
|
||||
// list all packages in reverse order
|
||||
for (int i = packets.size(); i-- > 0; ) {
|
||||
SearchedPackage searchedPacket = packets.get(i);
|
||||
|
@ -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<SearchedPackage> 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<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;
|
||||
|
||||
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<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;
|
||||
}
|
||||
public List<SearchedPackage> 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<SearchedPackage> searchForFedora(String packet) {
|
||||
// init distribution to search in it
|
||||
Distribution fedora = new FedoraDistribution();
|
||||
// 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 */
|
||||
// we add all instanced constructors in a list, only one if defined at creation of the object
|
||||
List<Distribution> 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<SearchedPackage> allPackages = new ArrayList<>();
|
||||
// this contains all future list of packages to get them after
|
||||
List<Future<List<SearchedPackage>>> 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<Package> 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<List<SearchedPackage>> futurePackageList : listFuturePackagesList ) {
|
||||
try {
|
||||
List<SearchedPackage> tempList = futurePackageList.get();
|
||||
allPackages.addAll(tempList);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// return the package
|
||||
return result;
|
||||
return allPackages;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user