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;
 | 
					package fr.packageviewer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					 | 
				
			||||||
import java.util.HashMap;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					 | 
				
			||||||
import java.util.Scanner;
 | 
					import java.util.Scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import fr.packageviewer.pack.SearchedPackage;
 | 
					import fr.packageviewer.pack.SearchedPackage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class Main {
 | 
					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) {
 | 
						public static void main(String[] args) {
 | 
				
			||||||
		// send the command line arguments to the parser
 | 
							// send the command line arguments to the parser
 | 
				
			||||||
		ArgParse.parseArguments(args);
 | 
							ArgParse.parseArguments(args);
 | 
				
			||||||
		String packet = ArgParse.getPacket();
 | 
							String packet = ArgParse.getPacket();
 | 
				
			||||||
		String distribution = ArgParse.getDistribution();
 | 
							String distribution = ArgParse.getDistribution();
 | 
				
			||||||
		distribution = processDistribution(distribution);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// the list of packages we will show later
 | 
							// we create an object to search the packages in the distribution
 | 
				
			||||||
		List<SearchedPackage> packets = new ArrayList<>();
 | 
							Searcher searcher;
 | 
				
			||||||
 | 
					 | 
				
			||||||
		// distribution is null when no distribution is specified
 | 
					 | 
				
			||||||
		// else we get the list of packages from the distribution
 | 
					 | 
				
			||||||
		if(distribution == null) {
 | 
							if(distribution == null) {
 | 
				
			||||||
			packets = Searcher.searchForAll(packet);
 | 
								searcher = new Searcher();
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			switch (distribution) {
 | 
								searcher = new Searcher(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;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// we get the packages list
 | 
				
			||||||
 | 
							List<SearchedPackage> packets = searcher.searchPackages(packet);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// list all packages in reverse order
 | 
							// list all packages in reverse order
 | 
				
			||||||
		for (int i = packets.size(); i-- > 0; ) {
 | 
							for (int i = packets.size(); i-- > 0; ) {
 | 
				
			||||||
			SearchedPackage searchedPacket = packets.get(i);
 | 
								SearchedPackage searchedPacket = packets.get(i);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,114 +1,66 @@
 | 
				
			|||||||
package fr.packageviewer;
 | 
					package fr.packageviewer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Collections;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.concurrent.ExecutionException;
 | 
				
			||||||
import java.util.concurrent.Future;
 | 
					import java.util.concurrent.Future;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import fr.packageviewer.distribution.ArchDistribution;
 | 
					 | 
				
			||||||
import fr.packageviewer.distribution.Distribution;
 | 
					import fr.packageviewer.distribution.Distribution;
 | 
				
			||||||
import fr.packageviewer.distribution.FedoraDistribution;
 | 
					 | 
				
			||||||
import fr.packageviewer.pack.SearchedPackage;
 | 
					import fr.packageviewer.pack.SearchedPackage;
 | 
				
			||||||
import fr.packageviewer.pack.Package;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class Searcher {
 | 
					public class Searcher {
 | 
				
			||||||
    public static List<SearchedPackage> searchForAll(String packet) {
 | 
					
 | 
				
			||||||
		// init distribution  to search in it
 | 
						private String distributionName = null;
 | 
				
			||||||
		Distribution arch = new ArchDistribution();
 | 
					
 | 
				
			||||||
		Distribution fedora = new FedoraDistribution();
 | 
						public Searcher() {}
 | 
				
			||||||
		// search for the package in the distribution
 | 
					
 | 
				
			||||||
		Future<List<SearchedPackage>> archPackages = arch.searchPackage(packet);
 | 
						public Searcher(String distributionName) {
 | 
				
			||||||
		Future<List<SearchedPackage>> fedoraPackages = fedora.searchPackage(packet);
 | 
							this.distributionName = distributionName;
 | 
				
			||||||
		// 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;
 | 
						public void setDistribution(String distribution) {
 | 
				
			||||||
 | 
							this.distributionName = distribution;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Search for the specified package in Arch repositories
 | 
						 * Get the list of all packages in the distribution setted before
 | 
				
			||||||
	 * @param packet the package to search
 | 
						 * @param packageName the name of the package wanted
 | 
				
			||||||
	 * @return The list of packages that match the name
 | 
						 * @return the list of all packages found
 | 
				
			||||||
	 * @author Capelier-Marla
 | 
						 * @author Capelier-Marla
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static List<SearchedPackage> searchForArch(String packet) {
 | 
						public List<SearchedPackage> searchPackages(String packageName) {
 | 
				
			||||||
		// init distribution  to search in it
 | 
					
 | 
				
			||||||
		Distribution arch = new ArchDistribution();
 | 
							// we add all instanced constructors in a list, only one if defined at creation of the object
 | 
				
			||||||
		// search for the package in the distribution
 | 
							List<Distribution> distributions;
 | 
				
			||||||
		Future<List<SearchedPackage>> packages = arch.searchPackage(packet);
 | 
							if(distributionName == null) {
 | 
				
			||||||
		// init the list of packages that will be returned
 | 
								distributions = DistributionEnum.getAllDistributionsInstances();
 | 
				
			||||||
		List<SearchedPackage> result = new ArrayList<>();
 | 
							} else {
 | 
				
			||||||
		// try to get the searched packages to return it after
 | 
								distributions = Collections.singletonList(DistributionEnum.getDistributionContructorByName(distributionName));
 | 
				
			||||||
		try {
 | 
								if(distributions.get(0) == null) {
 | 
				
			||||||
			result = packages.get();
 | 
									System.out.println("Distribution non trouvée");
 | 
				
			||||||
		} catch (Exception e) {
 | 
									System.exit(0);
 | 
				
			||||||
			/* TODO: handle exception */
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		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<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// we add all future packages in a list
 | 
				
			||||||
 | 
							for (Distribution distribution : distributions) {
 | 
				
			||||||
 | 
								listFuturePackagesList.add(distribution.searchPackage(packageName));
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
							// we get all packages waiting for them to be received
 | 
				
			||||||
	 * Search for the specified package in Fedora repositories
 | 
							for(Future<List<SearchedPackage>> futurePackageList : listFuturePackagesList ) {
 | 
				
			||||||
	 * @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 {
 | 
								try {
 | 
				
			||||||
			result = packages.get();
 | 
									List<SearchedPackage> tempList = futurePackageList.get();
 | 
				
			||||||
		} catch (Exception e) {
 | 
									allPackages.addAll(tempList);
 | 
				
			||||||
			/* TODO: handle exception */
 | 
								} catch (InterruptedException | ExecutionException e) {
 | 
				
			||||||
 | 
									e.printStackTrace();
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		return result;
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return allPackages;
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * 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;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// 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;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// return the package
 | 
					 | 
				
			||||||
		return result;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user