Reputation: 6435
I have a method where upon calling a FilenameFilter
is passed to (as anonymous inner class). Now, if a variable is set, I want to extend the FilenameFilter
. Example:
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
}
Should become:
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt") || new File(dir, name).isDirectory();
}
}
How can I get the filtering from the first filter and add the directory check? (I don't want to implement the FilenameFilter
with directories upon calling the method, because it might not be needed at all)
Solution based upon accepted answer. filenameFilter
is the instance passed to the method:
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return filenameFilter.accept(dir, name) || new File(dir, name).isDirectory();
}
};
Upvotes: 1
Views: 213
Reputation: 135992
You should extends the first filter:
class FilenameFilter1 implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
}
class FilenameFilter2 extends FilenameFilter1 {
@Override
public boolean accept(File dir, String name) {
return super.accept(dir, name) && new File(dir, name).isDirectory();
}
}
Upvotes: 1