Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:46:22+00:00 2026-05-23T02:46:22+00:00

So I have this method that get executed repeatedly public static boolean isReady(String dirPath,

  • 0

So I have this method that get executed 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 using anonymous class that will create a new instance of FilenameFilter every time invoked and I invoke this method a lot. So I want to make this anonymous class into a singleton. So my initial thought is to create a new singleton class that look 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 refactor this a bit more. I need to do ZipFileNameFilter as well, and maybe many different file extension filter. Dont want to create a class for each filter. I need to refactor this design a bit more. Maybe interface come into place somewhere here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:46:23+00:00Added an answer on May 23, 2026 at 2:46 am

    If all you wanted to do was reduce memory usage you could have done

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

    If you want to create a singleton, the simplest way is

    public enum PdfFileNameFilter implements FilenameFilter {
        INSTANCE;
    
        public boolean accept(File dir, String name) {
            return (name.toLowerCase().endsWith(".pdf"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C#: I have a string variable that looks like this: string a = (true
Let's say I have an object MyObject that looks like this: public class MyObject
Say we have this method to make an ssh to another machine. How would
This is beyond both making sense and my control. That being said here is
I have found this example on StackOverflow: var people = new List<Person> { new
I have a new web app that is packaged as a WAR as part
I have a script that appends some rows to a table. One of the
I have a MVVM application in WPF. I have an event that gets dispatched
I have a list of custom objects called EntertainmentEvent: public class EntertainmentEvent { public
EDIT: this question is mostly closed and the only problems i have with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.