Merge pull request #41 from ThomasRubini/reformat

This commit is contained in:
Thomas Rubini 2022-12-15 20:17:22 +01:00 committed by GitHub
commit 737b23f5a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 545 additions and 535 deletions

View File

@ -5,6 +5,7 @@ import com.beust.jcommander.JCommander;
/**
* Class to parse the command line arguments given by the user
*
* @author Capelier-Marla
*/
public class ArgParse {
@ -15,8 +16,9 @@ public class ArgParse {
/**
* Get the command line argument given by the user, parse it with the parser and store it in the corresponding variable
* @author Capelier-Marla
*
* @param args the command line arguments given by the user
* @author Capelier-Marla
*/
static void parseArguments(String[] args) {
// create JCommander and CommandLineParams objects
@ -27,7 +29,7 @@ public class ArgParse {
try {
// parse the argument from list of String
jCommander.parse(args);
if(params.help) {
if (params.help) {
jCommander.setProgramName("PackageViewer");
jCommander.usage();
System.exit(0);
@ -48,8 +50,9 @@ public class ArgParse {
/**
* Get the distribution name. If the user didn't give any or if we didn't parse it, return null
* @author Capelier-Marla
*
* @return String: the distribution name
* @author Capelier-Marla
*/
public static String getDistribution() {
return distribution;
@ -57,8 +60,9 @@ public class ArgParse {
/**
* Get the packet name, this one isn't optional
* @author Capelier-Marla
*
* @return String: the packet name
* @author Capelier-Marla
*/
public static String getPacket() {
return packet;

View File

@ -4,6 +4,7 @@ import com.beust.jcommander.Parameter;
/**
* Class to store and get the command line arguments given by the user
*
* @author Capelier-Marla
*/
public class CommandLineParams {

View File

@ -9,6 +9,7 @@ import java.util.List;
/**
* Enum containing distribution information to get them by their name
*
* @author Capelier-Marla
*/
public enum DistributionEnum {
@ -21,6 +22,7 @@ public enum DistributionEnum {
/**
* Constructor for enums
*
* @param name the name of the distribution
* @param distributionConstructor the instance of the distribution
* @author Capelier-Marla
@ -32,15 +34,16 @@ public enum DistributionEnum {
/**
* 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 getDistributionConstructorByName(String name) {
// loop for all distributions stored in enum
for(var distrib : values()) {
for (var distrib : values()) {
// return the instance if it's the same as enum name
if(distrib.name.equals(name)) {
if (distrib.name.equals(name)) {
return distrib.distributionConstructor;
}
}
@ -49,13 +52,14 @@ public enum DistributionEnum {
/**
* Get all distribution instances available in this enum
*
* @return the list 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 distrib : values()) {
for (var distrib : values()) {
result.add(distrib.distributionConstructor);
}
return result;

View File

@ -20,6 +20,7 @@ public class Searcher {
/**
* Get the list of all packages in the distribution set before
*
* @param packageName the name of the package wanted
* @return the list of all packages found
* @author Capelier-Marla
@ -28,11 +29,11 @@ public class Searcher {
// we add all instanced constructors in a list, only one if defined at creation of the object
List<Distribution> distributions;
if(distributionName == null) {
if (distributionName == null) {
distributions = DistributionEnum.getAllDistributionsInstances();
} else {
distributions = Collections.singletonList(DistributionEnum.getDistributionConstructorByName(distributionName));
if(distributions.get(0) == null) {
if (distributions.get(0) == null) {
System.out.println("Distribution non trouvée");
System.exit(0);
}
@ -48,7 +49,7 @@ public class Searcher {
}
// we get all packages waiting for them to be received
for(Future<List<SearchedPackage>> futurePackageList : listFuturePackagesList ) {
for (Future<List<SearchedPackage>> futurePackageList : listFuturePackagesList) {
try {
List<SearchedPackage> tempList = futurePackageList.get();
allPackages.addAll(tempList);
@ -61,7 +62,7 @@ public class Searcher {
public Package getPackage(SearchedPackage packetInput) {
if(distributionName == null) {
if (distributionName == null) {
distributionName = packetInput.getDistribution();
}
String packageName = packetInput.getName();

View File

@ -81,7 +81,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
),
dependenciesNames
));
}).exceptionally(error->{
}).exceptionally(error -> {
error.printStackTrace();
logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error));
futureResult.complete(null);
@ -126,7 +126,7 @@ public class FedoraDistribution extends AsyncRequestsParser implements Distribut
String name = searchResultJson.getString("name");
// do not include fork projects in the list
if(!name.startsWith("fork/")){
if (!name.startsWith("fork/")) {
// add package into to list
searchedPackagesList.add(new SearchedPackage(

View File

@ -7,5 +7,6 @@ import java.util.List;
public interface Frontend {
SearchedPackage askUserToChoosePackage(List<SearchedPackage> packets);
void showPackageTree(Package packet, int depth);
}

View File

@ -2,15 +2,11 @@ package fr.packageviewer.frontend;
public class FrontendFactory {
public static Frontend get(String name){
switch(name){
case "terminal":{
public static Frontend get(String name) {
if (name.equals("terminal")) {
return new FrontendTerminal();
}
default:{
throw new IllegalArgumentException("Invalid frontend");
}
}
}
}

View File

@ -6,10 +6,11 @@ import fr.packageviewer.pack.SearchedPackage;
import java.util.List;
import java.util.Scanner;
public class FrontendTerminal implements Frontend{
public class FrontendTerminal implements Frontend {
/**
* Check if the String given is a number
*
* @param i the String given
* @return true if the String is a number
* @author Capelier-Marla
@ -33,11 +34,11 @@ public class FrontendTerminal implements Frontend{
searchedPacket.getDistribution(),
searchedPacket.getRepo(),
searchedPacket.getName(),
searchedPacket.getVersion()==null?"":searchedPacket.getVersion(),
searchedPacket.getVersion() == null ? "" : searchedPacket.getVersion(),
searchedPacket.getDescription());
}
System.out.printf("Pick a package to see in details (0-%s) : ", packets.size()-1);
System.out.printf("Pick a package to see in details (0-%s) : ", packets.size() - 1);
Scanner input = new Scanner(System.in);
// we create vars for the loop
@ -49,9 +50,9 @@ public class FrontendTerminal implements Frontend{
packetNumberString = input.nextLine();
// reset notValid to false, we set it in true only if something is wrong
notValid = false;
if(isNumeric(packetNumberString)) {
if (isNumeric(packetNumberString)) {
packetNumber = Integer.parseInt(packetNumberString);
if(packetNumber < 0 || packetNumber >= packets.size()) {
if (packetNumber < 0 || packetNumber >= packets.size()) {
// this number is too big or too small
System.out.println("Enter a valid number");
notValid = true;
@ -61,7 +62,7 @@ public class FrontendTerminal implements Frontend{
System.out.println("Enter a valid number");
notValid = true;
}
} while(notValid);
} while (notValid);
input.close();
return packets.get(packetNumber);
@ -75,7 +76,7 @@ public class FrontendTerminal implements Frontend{
packet.getVersion(),
packet.getDescription());
for (Package dep : packet.getDeps()) {
showPackageTree(dep, depth+1);
showPackageTree(dep, depth + 1);
}
}

View File

@ -18,7 +18,6 @@ import java.util.logging.Logger;
*
* @author R.Thomas
* @version 1.0
*
*/
public abstract class AsyncRequestsParser {
/**

View File

@ -14,19 +14,21 @@ import java.util.concurrent.Future;
public abstract class DistroTest<T extends Distribution> {
protected abstract T createInstance();
protected List<SearchedPackage> helperSearchPackage(String packageName){
protected List<SearchedPackage> helperSearchPackage(String packageName) {
Distribution distribution = createInstance();
Future<List<SearchedPackage>> future = distribution.searchPackage(packageName);
try{
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
protected Package helperGetPackageTree(String packageName, int depth){
protected Package helperGetPackageTree(String packageName, int depth) {
Distribution distribution = createInstance();
Future<Package> future = distribution.getPackageTree(packageName, depth);
try{
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
@ -34,7 +36,7 @@ public abstract class DistroTest<T extends Distribution> {
}
@Test
public void testBasicQueryDoNotFail(){
public void testBasicQueryDoNotFail() {
helperGetPackageTree("bash", 0);
}
@ -56,7 +58,7 @@ public abstract class DistroTest<T extends Distribution> {
public void testQueryWithDepth1hasOneLevelOfDeps() {
Package pack = helperGetPackageTree("bash", 1);
Assertions.assertNotEquals(pack.getDeps().size(), 0);
for(Package dep : pack.getDeps()){
for (Package dep : pack.getDeps()) {
Assertions.assertEquals(dep.getDeps().size(), 0);
}
}
@ -83,10 +85,11 @@ public abstract class DistroTest<T extends Distribution> {
public void testThatBashSearchReturnsResults() {
Assertions.assertNotEquals(helperSearchPackage("bash").size(), 0);
}
@Test
public void testThatBashSearchContainsBash() {
for(SearchedPackage pack : helperSearchPackage("bash")){
if(pack.getName().equals("bash")){
for (SearchedPackage pack : helperSearchPackage("bash")) {
if (pack.getName().equals("bash")) {
Assertions.assertTrue(true);
return;
}