Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FielNameFilter - how does accept () get called ???  RSS feed

 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem statement - I want to look in a directory and list all the files in it which are .java files.
here's one way to do it ( many java books I've looked at contain very similar examples):
from Grant Palmer's "Java Programmer's Reference" (Wrox Press 2000):

My questions:
I understand that we call for a new object in class "MyFilter" to be constructed with this line "String[] fileList = fobj.list( new MyFilter("java") );
How does the accept method,in the MyFilter class, get called and run when this line is executed ???
------------------------------------------------
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File class calls it, in code for the list(FilenameFilter) method. You can look at the source code for this method in src.zip (or src.jar in some JDK versions).
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
The File class calls it, in code for the list(FilenameFilter) method. You can look at the source code for this method in src.zip (or src.jar in some JDK versions).

Thanks Jim - I will do that.
When I looked at the jdk documentation for the class, it wasn't clear if or how that happened. I'm wondering if this is a common occurrence in Java?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by david eberhardt:
I'm wondering if this is a common occurrence in Java?

It's a reoccuring pattern in object oriented designs in general. It even has its own name: the Strategy Design Pattern (see http://c2.com/cgi/wiki?StrategyPattern).
Another prominent example from the Java API is the java.util.Comparator interface.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ilja and Jim.
I did unpack the source files for the io.* package and looked over the File class code for the list(FilenameFilter) method.
Now I see it!
This tip will help me to get used to looking at more source code that is part of the APIs that come with the JDK.
Much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!