From 7bec57b5570729df0f1ce8e0e5ef9d39b377d652 Mon Sep 17 00:00:00 2001 From: manfromhuh Date: Wed, 18 Aug 2021 13:33:29 -0400 Subject: [PATCH] Code cleanup I have cleaned up a lot of the code. --- src/picture_lister/Picture_Lister.java | 55 ++++++++++++++------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/picture_lister/Picture_Lister.java b/src/picture_lister/Picture_Lister.java index 06380de..0264717 100644 --- a/src/picture_lister/Picture_Lister.java +++ b/src/picture_lister/Picture_Lister.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * @@ -38,8 +39,8 @@ public class Picture_Lister { } String picture_dir = args[0]; List file_format = new ArrayList<>(); - if (args.length > 1){//Check if any argumets other than the path have been passed. - for(int i = 1; i < args.length; i++){ + if (args.length > 1){//Check if any arguments other than the path have been passed. + for(int i = 1; i < args.length; i++){//Ignore IDE warning about manual sort. See end of file. file_format.add(args[i]); } } else {//The default formats to look for if none are given. @@ -49,36 +50,32 @@ public class Picture_Lister { System.out.println(picture_dir); //Creating a File object for directory File directoryPath = new File(picture_dir); - FilenameFilter picture_filter = new FilenameFilter() { - - @Override - public boolean accept(File dir, String name) { - if(name.lastIndexOf('.')>0) { - String[] include = file_format.toArray(new String[file_format.size()]); - // get last index for '.' char - int lastIndex = name.lastIndexOf('.'); - - // get extension - String str = name.substring(lastIndex); - - // match path name extension - for (String place_holder : include) { - if (str.equalsIgnoreCase(place_holder)) { - return true; - } + FilenameFilter picture_filter = (dir, name) -> { + if(name.lastIndexOf('.')>0) { + String[] include = file_format.toArray(new String[0]); + // get last index for '.' char + int lastIndex = name.lastIndexOf('.'); + + // get extension + String str = name.substring(lastIndex); + + // match path name extension + for (String place_holder : include) { + if (str.equalsIgnoreCase(place_holder)) { + return true; } } - - return false; - } - }; + } + + return false; + }; if (directoryPath.isDirectory()) { System.out.println("Listing directory...\n"); //List of all files and directories - File contents[] = directoryPath.listFiles(picture_filter); + File[] contents = directoryPath.listFiles(picture_filter); System.out.println("List of files and directories in the specified directory:"); - for (int i = 0; i < contents.length; i++) { - System.out.println(contents[i]); + for (File content : Objects.requireNonNull(contents)) { + System.out.println(content); } } else { System.out.println("Not a directory.\nExiting..."); @@ -87,3 +84,9 @@ public class Picture_Lister { } } + +/* + Some editors will complain about this being a manual Array to + Collection sort. This is done because the first member of the array + Doesn't belong. The first member of the array will always be a Path. + */ \ No newline at end of file