Reduce the print footprint by replacing the anonymous class with singleton. But you need to reorganize the design more

So, I have this method that runs repeatedly.

public static boolean isReady(String dirPath, int numPdfInPrintJob){
    File dir = new File(dirPath);
    String[] fileList = dir.list(new FilenameFilter(){
        public boolean accept(File file, String filename) {
            return (filename.toLowerCase().endsWith(".pdf"));
        }
    });
    if(fileList.length >= numPdfInPrintJob) return true;
    else return false;  
}

This method uses anonymous classthat will create a new instance FilenameFilterwith each call and call this method many times. So I want to do it anonymous classin singleton. So my initial thought was to create a new class singletonthat looks like this:

public class PdfFileNameFilter implements FilenameFilter{
    private PdfFileNameFilter(){} //non-instantible

    //guarantee to only have one instance at all time
    public static final PdfFileNameFilter INSTANCE = new PdfFileNameFilter();

    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(".pdf"));
    }
}

Can I reorganize this a little more. I also need to do ZipFileNameFilterand possibly many different file extensions. Do not want to create a class for each filter. I need to reorganize this project a little more. Maybe interfacecome to a place somewhere here.

+5
6

, , ,

private static final FilenameFilter PDF_FILES = new FilenameFilter(){
    public boolean accept(File file, String filename) {
        return (filename.toLowerCase().endsWith(".pdf"));
    }
}

, -

public enum PdfFileNameFilter implements FilenameFilter {
    INSTANCE;

    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(".pdf"));
    }
}
+6

, , .

private static final FilenameFilter PDF_FILTER = new FilenameFilter() {
    public boolean accept(File file, String filename) {
        return (filename.toLowerCase().endsWith(".pdf"));
    }
}

public static boolean isReady(String dirPath, int numPdfInPrintJob){
    File dir = new File(dirPath);
    String[] fileList = dir.list(pdfFilter);
    if(fileList.length >= numPdfInPrintJob) return true;
    else return false;  
}

, : , singleton , , - .

+4

enum:

public enum ExtensionFilter implements FilenameFilter {
    PDF(".pdf"),
    ZIP(".zip");

    private final String extension;

    private ExtensionFilter(String extension) {
        this.extension = extension;
    }

    @Override
    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(extension));
    }
}

:

dir.list(ExtensionFilter.PDF)

, :

for ( FilenameFilter fileNameFilter : ExtensionFilter.values() ) {
    ....
}

vararg , :

public enum ExtensionFilter implements FilenameFilter {
    PDF,
    ZIP(".zip", ".jar", ".war", ".ear");

    private final String[] extensions;

    private ExtensionFilter(String... extensions) {
        if (extensions.length == 0) {
            extensions = new String[] {"." + name().toLowerCase()};
        }
        this.extensions = extensions;
    }

    @Override
    public boolean accept(File dir, String name) {
        for (String extension : extensions) {
            if (name.toLowerCase().endsWith(extension)) {
                return true;
            }
        }
        return false;
    }
}
+3

public class ExtensionFileNameFilter implements FilenameFilter{
    private String extension;
    private ExtensionFileNameFilter (String extension){this.extension=extension;} 

    public static final ExtensionFileNameFilter PDFINSTANCE = new ExtensionFileNameFilter (".pdf");
    public static final ExtensionFileNameFilter ZIPINSTANCE = new ExtensionFileNameFilter (".zip");
    //add instances as you need

    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(extension));
    }
}
+1

.

, , .

, , ( yoru , ), , ; YAGNI.

, , FilenameFilters, . , , , - . .

TDD - . , , , , . , , , , .

+1

For fun, this alternative implementation acceptwill run much faster in a test case. It does not create new stateful objects or transfer other service data String.toLowerCasethat is not required for your case.

  public boolean accept(File file, String filename) {
    int offset = s.length() - 4;
    if (offset >= 0) {
      if (s.charAt(offset) == '.') {
        offset += 1;
        if (s.regionMatches(offset, "pdf", 0, 3)) {
          return true;
        } else if (s.regionMatches(offset, "PDF", 0, 3)) { 
          return true;
        }
      }
    }
    return false;
  }

If it was an execution access point, and you were looking for optimization, this could help.

+1
source

All Articles